1 | <?php |
---|
2 | /** |
---|
3 | * Needs conf: |
---|
4 | * webroot lang_path lang seiten_id languages |
---|
5 | * |
---|
6 | **/ |
---|
7 | class t29Menu { |
---|
8 | public $conf; |
---|
9 | public $xml; |
---|
10 | |
---|
11 | // jeweils relativ zum lang_path |
---|
12 | const navigation_file = 'navigation.xml'; |
---|
13 | const news_file = 'news.php'; |
---|
14 | |
---|
15 | // xpath queries to the navigation elements |
---|
16 | const horizontal_menu = '/html/nav[@class="horizontal"]'; |
---|
17 | const sidebar_menu = '/html/nav[@class="side"]'; |
---|
18 | |
---|
19 | function __construct($conf_array) { |
---|
20 | $this->conf = $conf_array; |
---|
21 | |
---|
22 | // libxml: don't raise errors while parsing. |
---|
23 | // will fetch them with libxml_get_errors later. |
---|
24 | //libxml_use_internal_errors(true); |
---|
25 | |
---|
26 | // load xml file |
---|
27 | $this->xml = simplexml_load_file($this->conf['webroot'].$this->conf['lang_path'] . '/' . self::navigation_file); |
---|
28 | if($this->xml_is_defective()) { |
---|
29 | trigger_error("Kann Navigationsdatei nicht verwenden, da das XML nicht sauber ist. Bitte reparieren!"); |
---|
30 | } |
---|
31 | } |
---|
32 | |
---|
33 | function xml_is_defective() { |
---|
34 | // check if return value of simplexml_load_file was false, |
---|
35 | // which means parse error. |
---|
36 | return $this->xml === FALSE; |
---|
37 | } |
---|
38 | |
---|
39 | ///////////////////// NEWS EXTRACTION |
---|
40 | function load_news_data() { |
---|
41 | $newsfile = $this->conf['webroot'].$this->conf['lang_path']."/".self::news_file; |
---|
42 | $newsdir = dirname(realpath($newsfile)); |
---|
43 | // include path wird ignoriert wenn include relativ ist, was in der |
---|
44 | // eingebundenen Datei der Fall ist |
---|
45 | // set_include_path( get_include_path(). PATH_SEPARATOR . dirname($newsfile)); |
---|
46 | $pwd = getcwd(); chdir($newsdir); |
---|
47 | include(self::news_file); |
---|
48 | chdir($pwd); |
---|
49 | return $neues_menu; |
---|
50 | } |
---|
51 | |
---|
52 | function convert_news_data() { |
---|
53 | require $this->conf['lib'].'/spyc.php'; |
---|
54 | $data = Spyc::YAMLLoad($this->load_news_data()); |
---|
55 | $fields = array('titel', 'text', 'link', 'bild'); |
---|
56 | |
---|
57 | $news_ul_content = ''; |
---|
58 | foreach($data as $e) { |
---|
59 | if(!array_reduce(array_map(function($x) use ($fields,$e){ return isset($e[$x]); }, $fields), |
---|
60 | function($a,$b){ return $a && $b;}, true)) { |
---|
61 | $li = "<li>Fehler in Formatierung!"; |
---|
62 | } else { |
---|
63 | $url = ($e['link']{0} == '#' ? $this->conf['lang_path'].'/'.self::news_file : '').$e['link']; |
---|
64 | $li = "<li><a href='$url'><img src='$e[bild]' /> $e[titel]<span class='hidden'>: </span><em>$e[text]</em></a></li>"; |
---|
65 | } |
---|
66 | $news_ul_content .= "\t".$li."\n"; |
---|
67 | } |
---|
68 | |
---|
69 | return $news_ul_content; |
---|
70 | } |
---|
71 | |
---|
72 | ///////////////////// RETURN INFOS ABOUT SEITEN_ID LINK |
---|
73 | function find_link($seiten_id=false) { |
---|
74 | if($this->xml_is_defective()) { |
---|
75 | return null; |
---|
76 | } |
---|
77 | if(!$seiten_id) $seiten_id = $this->conf['seiten_id']; |
---|
78 | |
---|
79 | $matches = $this->xml->xpath("//a[@seiten_id='$seiten_id']"); |
---|
80 | if($matches && count($matches)) { |
---|
81 | // strip the first one |
---|
82 | return $matches[0]; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | ///////////////////// INTER LANGUAGE DETECTION |
---|
87 | /** |
---|
88 | * @param seiten_id Get interlanguage link for that seiten_id or default. |
---|
89 | **/ |
---|
90 | function get_interlanguage_link($seiten_id=false) { |
---|
91 | if(!$seiten_id) $seiten_id = $this->conf['seiten_id']; |
---|
92 | |
---|
93 | $interlinks = array(); // using a loop instead of mappings since php is stupid |
---|
94 | foreach($this->conf['languages'] as $lang => $lconf) { |
---|
95 | $foreign_menu = new t29Menu(array( |
---|
96 | 'webroot' => $this->conf['webroot'], |
---|
97 | 'seiten_id' => $this->conf['seiten_id'], |
---|
98 | 'languages' => $this->conf['languages'], |
---|
99 | 'lang' => $lang, |
---|
100 | 'lang_path' => $lconf[1], |
---|
101 | )); |
---|
102 | |
---|
103 | $link = $foreign_menu->find_link($seiten_id); |
---|
104 | $interlinks[$lang] = $link; |
---|
105 | } |
---|
106 | |
---|
107 | return $interlinks; |
---|
108 | } |
---|
109 | |
---|
110 | // helper method |
---|
111 | public static function dom_add_class($simplexml_element, $value) { |
---|
112 | $dom = dom_import_simplexml($simplexml_element); // is a fast operation |
---|
113 | $simplexml_element['class'] = |
---|
114 | ($dom->hasAttribute("class") ? ($simplexml_element['class'].' '):'').$value; |
---|
115 | } |
---|
116 | |
---|
117 | public static function dom_new_link($href, $label) { |
---|
118 | return new SimpleXMLElement(sprintf('<a href="%s">%s</a>', $href, $label)); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | ///////////////////// MENU ACTIVE LINK DETECTION |
---|
123 | /** |
---|
124 | * @arg $xpath_menu_selection one of the horizontal_menu / sidebar_menu consts. |
---|
125 | **/ |
---|
126 | function print_menu($xpath_menu_selection) { |
---|
127 | if($this->xml_is_defective()) { |
---|
128 | print "The Menu file is broken."; |
---|
129 | return false; |
---|
130 | } |
---|
131 | $seiten_id = $this->conf['seiten_id']; |
---|
132 | |
---|
133 | // find wanted menu |
---|
134 | $xml = $this->xml->xpath($xpath_menu_selection); |
---|
135 | if(!$xml) { |
---|
136 | print "Menu <i>$xpath_menu_selection</i> not found!"; |
---|
137 | return false; |
---|
138 | } |
---|
139 | $xml = $xml[0]; // just take the first result (should only one result be present) |
---|
140 | |
---|
141 | // aktuelle Seite anmarkern und Hierarchie hochgehen |
---|
142 | // (<ul><li>bla<ul><li>bla<ul><li>hierbin ich <- hochgehen.) |
---|
143 | $current_a = $xml->xpath("//a[@seiten_id='$seiten_id']"); |
---|
144 | if(count($current_a)) { |
---|
145 | $current_li = $current_a[0]->xpath("parent::li"); |
---|
146 | $this->dom_add_class($current_li[0], "current"); |
---|
147 | $ancestors = $current_li[0]->xpath("ancestor-or-self::li"); |
---|
148 | array_walk($ancestors, create_function('$i', 't29Menu::dom_add_class($i, "active");')); |
---|
149 | } |
---|
150 | |
---|
151 | // Seiten-IDs (ungueltiges HTML) ummoddeln |
---|
152 | $all_ids = $xml->xpath("//a[@seiten_id]"); |
---|
153 | foreach($all_ids as $a) { |
---|
154 | $a['id'] = "sidebar_link_".$a['seiten_id']; |
---|
155 | // umweg ueber DOM um Node zu loeschen |
---|
156 | $adom = dom_import_simplexml($a); |
---|
157 | $adom->removeAttribute('seiten_id'); |
---|
158 | } |
---|
159 | |
---|
160 | // Geraete-Seiten entfernen |
---|
161 | $geraete_uls = $xml->xpath("//ul[contains(@class, 'geraete')]"); |
---|
162 | foreach($geraete_uls as $ul) { |
---|
163 | $uld = dom_import_simplexml($ul); |
---|
164 | $uld->parentNode->removeChild($uld); |
---|
165 | } |
---|
166 | |
---|
167 | if($xpath_menu_selection == self::horizontal_menu) { |
---|
168 | # inject news |
---|
169 | $news_ul_content = $this->convert_news_data(); |
---|
170 | $magic_comment = '<!--# INSERT_NEWS #-->'; |
---|
171 | $menu = $xml->ul->asXML(); |
---|
172 | print str_replace($magic_comment, $news_ul_content, $menu); |
---|
173 | } else { |
---|
174 | print $xml->ul->asXML(); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | ///////////////////// PAGE RELATIONS |
---|
179 | /** |
---|
180 | * Usage: |
---|
181 | * foreach(get_page_relations() as $a) { |
---|
182 | * echo "Link $a going to $a[href]"; |
---|
183 | * } |
---|
184 | * @param $seiten_id A seiten_id string or nothing for taking the current active string |
---|
185 | * @returns an array(prev=>..., next=>...) or empty array, elements are SimpleXML a links |
---|
186 | **/ |
---|
187 | function get_page_relations($seiten_id=false) { |
---|
188 | if($this->xml_is_defective()) |
---|
189 | return array(); // cannot construct relations due to bad XML file |
---|
190 | if(!$seiten_id) $seiten_id = $this->conf['seiten_id']; |
---|
191 | |
---|
192 | $xml = $this->xml->xpath(self::sidebar_menu); |
---|
193 | if(!$xml) { print "<i>Sidebar not found</i>"; return; } |
---|
194 | $sidebar = $xml[0]; |
---|
195 | |
---|
196 | |
---|
197 | $return = array(); |
---|
198 | $current_a = $sidebar->xpath("//a[@seiten_id='$seiten_id']"); |
---|
199 | if(count($current_a)) { |
---|
200 | foreach(array( |
---|
201 | "prev" => "preceding::a[@seiten_id]", |
---|
202 | "next" => "following::a[@seiten_id]") as $rel => $xpath) { |
---|
203 | $nodes = $current_a[0]->xpath($xpath); |
---|
204 | foreach($rel == "prev" ? array_reverse($nodes) : $nodes as $link) { |
---|
205 | $is_geraete = count($link->xpath("ancestor::ul[contains(@class, 'geraete')]")); |
---|
206 | if($is_geraete) continue; // skip geraete links |
---|
207 | $return[$rel] = $link; |
---|
208 | break; // just take the first matching element |
---|
209 | } |
---|
210 | } |
---|
211 | } else { |
---|
212 | // TODO PENDING: Der Fall tritt derzeit niemals ein, da das XML |
---|
213 | // sich dann doch irgendwie auf alles bezieht ($sidebar = alles) und |
---|
214 | // ueberall gesucht wird. Ist aber okay. oder? |
---|
215 | $return['start'] = t29Menu::dom_new_link('#', 'bla'); |
---|
216 | } |
---|
217 | return $return; |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | * Construct link class information as an array, containing: |
---|
222 | * - navigation list membership (in-nav-horizontal, in-nav-side) |
---|
223 | * - parental ul classes (in-u2, in-u3, in-geraete, ...) |
---|
224 | * @returns array with individual class names as strings |
---|
225 | **/ |
---|
226 | function get_link_classes($seiten_id=false) { |
---|
227 | if($this->xml_is_defective()) |
---|
228 | return array(); |
---|
229 | if(!$seiten_id) $seiten_id = $this->conf['seiten_id']; |
---|
230 | |
---|
231 | $link = $this->find_link($seiten_id); |
---|
232 | if(!$link) return array(); // if not found |
---|
233 | $classes = array(); |
---|
234 | |
---|
235 | // navigation list membership |
---|
236 | $nav = $link->xpath("ancestor::nav"); |
---|
237 | $nav_type = $nav[0]['class']; |
---|
238 | $classes[] = "in-nav-$nav_type"; |
---|
239 | |
---|
240 | // direct parental ul classes |
---|
241 | $ul = $link->xpath("ancestor::ul"); |
---|
242 | $parent_ul = array_pop($ul); |
---|
243 | foreach(explode(' ',$parent_ul['class']) as $c) |
---|
244 | $classes[] = "in-$c"; |
---|
245 | |
---|
246 | return $classes; |
---|
247 | } |
---|
248 | |
---|
249 | } // class |
---|