[251] | 1 | <?php |
---|
[259] | 2 | /** |
---|
| 3 | * Needs conf: |
---|
| 4 | * webroot lang_path lang seiten_id languages |
---|
| 5 | * |
---|
| 6 | **/ |
---|
[301] | 7 | |
---|
| 8 | require_once dirname(__FILE__).'/messages.php'; |
---|
[347] | 9 | require_once dirname(__FILE__).'/logging.php'; |
---|
[301] | 10 | |
---|
[254] | 11 | class t29Menu { |
---|
| 12 | public $conf; |
---|
[259] | 13 | public $xml; |
---|
[347] | 14 | public $log; // just for convenience |
---|
[251] | 15 | |
---|
[301] | 16 | // Bevor es eine ordentliche Dev-Moeglichkeit gibt: Der magische |
---|
| 17 | // Schalter zum Ausblenden der Geraeteseiten im Menue |
---|
| 18 | const hide_geraete_seiten = true; |
---|
| 19 | |
---|
[259] | 20 | // jeweils relativ zum lang_path |
---|
| 21 | const navigation_file = 'navigation.xml'; |
---|
[255] | 22 | const news_file = 'news.php'; |
---|
[254] | 23 | |
---|
[259] | 24 | // xpath queries to the navigation elements |
---|
| 25 | const horizontal_menu = '/html/nav[@class="horizontal"]'; |
---|
| 26 | const sidebar_menu = '/html/nav[@class="side"]'; |
---|
| 27 | |
---|
[254] | 28 | function __construct($conf_array) { |
---|
| 29 | $this->conf = $conf_array; |
---|
[347] | 30 | $this->log = t29Log::get(); // just for convenience |
---|
[275] | 31 | |
---|
[301] | 32 | // create a message object if not given |
---|
| 33 | if(!isset($this->conf['msg'])) |
---|
| 34 | $this->conf['msg'] = new t29Messages($this->conf['lang']); |
---|
| 35 | |
---|
[275] | 36 | // libxml: don't raise errors while parsing. |
---|
| 37 | // will fetch them with libxml_get_errors later. |
---|
| 38 | //libxml_use_internal_errors(true); |
---|
[259] | 39 | |
---|
| 40 | // load xml file |
---|
| 41 | $this->xml = simplexml_load_file($this->conf['webroot'].$this->conf['lang_path'] . '/' . self::navigation_file); |
---|
[275] | 42 | if($this->xml_is_defective()) { |
---|
| 43 | trigger_error("Kann Navigationsdatei nicht verwenden, da das XML nicht sauber ist. Bitte reparieren!"); |
---|
| 44 | } |
---|
[251] | 45 | } |
---|
[259] | 46 | |
---|
[275] | 47 | function xml_is_defective() { |
---|
| 48 | // check if return value of simplexml_load_file was false, |
---|
| 49 | // which means parse error. |
---|
| 50 | return $this->xml === FALSE; |
---|
| 51 | } |
---|
| 52 | |
---|
[259] | 53 | ///////////////////// NEWS EXTRACTION |
---|
[254] | 54 | function load_news_data() { |
---|
[255] | 55 | $newsfile = $this->conf['webroot'].$this->conf['lang_path']."/".self::news_file; |
---|
[254] | 56 | $newsdir = dirname(realpath($newsfile)); |
---|
| 57 | // include path wird ignoriert wenn include relativ ist, was in der |
---|
| 58 | // eingebundenen Datei der Fall ist |
---|
| 59 | // set_include_path( get_include_path(). PATH_SEPARATOR . dirname($newsfile)); |
---|
| 60 | $pwd = getcwd(); chdir($newsdir); |
---|
[255] | 61 | include(self::news_file); |
---|
[254] | 62 | chdir($pwd); |
---|
| 63 | return $neues_menu; |
---|
[251] | 64 | } |
---|
| 65 | |
---|
[254] | 66 | function convert_news_data() { |
---|
| 67 | require $this->conf['lib'].'/spyc.php'; |
---|
| 68 | $data = Spyc::YAMLLoad($this->load_news_data()); |
---|
[291] | 69 | $fields = array('titel', 'text', 'link', /*'bild'*/); |
---|
[251] | 70 | |
---|
[254] | 71 | $news_ul_content = ''; |
---|
| 72 | foreach($data as $e) { |
---|
| 73 | if(!array_reduce(array_map(function($x) use ($fields,$e){ return isset($e[$x]); }, $fields), |
---|
| 74 | function($a,$b){ return $a && $b;}, true)) { |
---|
[347] | 75 | $li = "<li><a href='#'>Fehler in Formatierung!<em>Dieser Menüeintrag ist falsch formatiert</em></a></li>"; |
---|
| 76 | $this->log->WARN("<h5>Neuigkeiten-Menü: Fehler in Formatierung</h5><p>Ein Eintrag im Neuigkeiten-Menü ist falsch formatiert."); |
---|
[254] | 77 | } else { |
---|
[260] | 78 | $url = ($e['link']{0} == '#' ? $this->conf['lang_path'].'/'.self::news_file : '').$e['link']; |
---|
[291] | 79 | $li = "<li><a href='$url'>$e[titel]<span class='hidden'>: </span><em>$e[text]</em></a></li>"; |
---|
[254] | 80 | } |
---|
| 81 | $news_ul_content .= "\t".$li."\n"; |
---|
| 82 | } |
---|
[251] | 83 | |
---|
[254] | 84 | return $news_ul_content; |
---|
| 85 | } |
---|
[259] | 86 | |
---|
| 87 | ///////////////////// RETURN INFOS ABOUT SEITEN_ID LINK |
---|
[279] | 88 | |
---|
| 89 | /** |
---|
| 90 | * Find the corresponding XML node in the navigation tree for the link |
---|
| 91 | * with given $seiten_id or the current given seiten_id in the configuration |
---|
| 92 | * array. |
---|
| 93 | * This method is used in get_link_navigation_class, etc. for resolving |
---|
| 94 | * the XML element from the string. They can be used with the XML node, too, |
---|
| 95 | * and this behaviour is passed throught by this method, so if you call |
---|
| 96 | * this with an SimpleXMLElement as argument, it behaves like an identity |
---|
| 97 | * function and just does nothing. |
---|
| 98 | * (This is used in template.php for caching the found xml element and saving |
---|
| 99 | * several xpath querys on get_* calls) |
---|
| 100 | * |
---|
| 101 | * @param $seiten_id Either a string, or nothing (defaults to conf) or SimpleXMLElement |
---|
| 102 | * @returns SimpleXMLElement or null if link not found |
---|
| 103 | **/ |
---|
| 104 | function get_link($seiten_id=false) { |
---|
[275] | 105 | if($this->xml_is_defective()) { |
---|
| 106 | return null; |
---|
| 107 | } |
---|
[259] | 108 | if(!$seiten_id) $seiten_id = $this->conf['seiten_id']; |
---|
[279] | 109 | // convenience: If you found your link already. |
---|
| 110 | if($seiten_id instanceof SimpleXMLElement) return $seiten_id; |
---|
[254] | 111 | |
---|
[259] | 112 | $matches = $this->xml->xpath("//a[@seiten_id='$seiten_id']"); |
---|
| 113 | if($matches && count($matches)) { |
---|
| 114 | // strip the first one |
---|
| 115 | return $matches[0]; |
---|
| 116 | } |
---|
[279] | 117 | return null; |
---|
[259] | 118 | } |
---|
[279] | 119 | |
---|
| 120 | /** |
---|
| 121 | * Get navigation list membership (horizontal or side) of a link |
---|
| 122 | * @see get_link for parameters |
---|
| 123 | * @returns String of <nav> class where this link is affiliated to |
---|
| 124 | **/ |
---|
| 125 | function get_link_navigation_class($seiten_id=false) { |
---|
| 126 | $link = $this->get_link($seiten_id); |
---|
| 127 | if(!$link) return null; |
---|
| 128 | |
---|
| 129 | // navigation list membership |
---|
| 130 | $nav = $link->xpath("ancestor::nav"); |
---|
| 131 | return strval($nav[0]['class']); // cast SimpleXMLElement |
---|
| 132 | } |
---|
[259] | 133 | |
---|
[279] | 134 | /** |
---|
| 135 | * Get list of parental ul classes (u2, u3, geraete, ...) |
---|
| 136 | * @see get_link for parameters |
---|
| 137 | * @returns array with individual class names as strings |
---|
| 138 | **/ |
---|
| 139 | function get_link_ul_classes($seiten_id=false) { |
---|
| 140 | $link = $this->get_link($seiten_id); |
---|
| 141 | if(!$link) return array(); |
---|
| 142 | |
---|
| 143 | // direct parental ul classes |
---|
| 144 | $ul = $link->xpath("ancestor::ul"); |
---|
| 145 | $parent_ul = array_pop($ul); |
---|
| 146 | return explode(' ',$parent_ul['class']); |
---|
| 147 | } |
---|
| 148 | |
---|
[259] | 149 | ///////////////////// INTER LANGUAGE DETECTION |
---|
| 150 | /** |
---|
| 151 | * @param seiten_id Get interlanguage link for that seiten_id or default. |
---|
| 152 | **/ |
---|
| 153 | function get_interlanguage_link($seiten_id=false) { |
---|
| 154 | if(!$seiten_id) $seiten_id = $this->conf['seiten_id']; |
---|
| 155 | |
---|
| 156 | $interlinks = array(); // using a loop instead of mappings since php is stupid |
---|
| 157 | foreach($this->conf['languages'] as $lang => $lconf) { |
---|
| 158 | $foreign_menu = new t29Menu(array( |
---|
| 159 | 'webroot' => $this->conf['webroot'], |
---|
| 160 | 'seiten_id' => $this->conf['seiten_id'], |
---|
| 161 | 'languages' => $this->conf['languages'], |
---|
| 162 | 'lang' => $lang, |
---|
| 163 | 'lang_path' => $lconf[1], |
---|
| 164 | )); |
---|
| 165 | |
---|
[279] | 166 | $link = $foreign_menu->get_link($seiten_id); |
---|
[259] | 167 | $interlinks[$lang] = $link; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | return $interlinks; |
---|
| 171 | } |
---|
| 172 | |
---|
[289] | 173 | // helper methods |
---|
| 174 | |
---|
| 175 | /** Check if a simplexml element has an attribute. Lightweight operation |
---|
| 176 | * over the DOM. |
---|
| 177 | * @returns boolean |
---|
| 178 | **/ |
---|
| 179 | public static function dom_has_attribute($simplexml_element, $attribute_name) { |
---|
[259] | 180 | $dom = dom_import_simplexml($simplexml_element); // is a fast operation |
---|
[289] | 181 | return $dom->hasAttribute($attribute_name); |
---|
[254] | 182 | } |
---|
[259] | 183 | |
---|
[289] | 184 | public static function dom_prepend_attribute($simplexml_element, $attribute_name, $content, $seperator='') { |
---|
| 185 | if(!is_array($simplexml_element)) $simplexml_element = array($simplexml_element); |
---|
| 186 | foreach($simplexml_element as $e) |
---|
| 187 | $e[$attribute_name] = $content . (self::dom_has_attribute($e, $attribute_name) ? ($seperator.$e[$attribute_name]) : ''); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | public static function dom_append_attribute($simplexml_element, $attribute_name, $content, $seperator='') { |
---|
| 191 | if(!is_array($simplexml_element)) $simplexml_element = array($simplexml_element); |
---|
| 192 | foreach($simplexml_element as $e) |
---|
| 193 | $e[$attribute_name] = (self::dom_has_attribute($e, $attribute_name) ? ($e[$attribute_name].$seperator) : '') . $content; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | * Appends a (CSS) class to a simplexml element, seperated by whitespace. Just an alias. |
---|
| 198 | **/ |
---|
| 199 | public static function dom_add_class($simplexml_element, $value) { |
---|
| 200 | self::dom_append_attribute($simplexml_element, 'class', $value, ' '); |
---|
| 201 | } |
---|
| 202 | |
---|
[259] | 203 | public static function dom_new_link($href, $label) { |
---|
| 204 | return new SimpleXMLElement(sprintf('<a href="%s">%s</a>', $href, $label)); |
---|
| 205 | } |
---|
[254] | 206 | |
---|
[278] | 207 | |
---|
[259] | 208 | ///////////////////// MENU ACTIVE LINK DETECTION |
---|
| 209 | /** |
---|
| 210 | * @arg $xpath_menu_selection one of the horizontal_menu / sidebar_menu consts. |
---|
[347] | 211 | * @arg $host Instance of t29Host which can be used for link rewriting if given. |
---|
[259] | 212 | **/ |
---|
[347] | 213 | function print_menu($xpath_menu_selection, $host=null) { |
---|
[275] | 214 | if($this->xml_is_defective()) { |
---|
| 215 | print "The Menu file is broken."; |
---|
| 216 | return false; |
---|
| 217 | } |
---|
[254] | 218 | $seiten_id = $this->conf['seiten_id']; |
---|
[301] | 219 | $_ = $this->conf['msg']->get_shorthand_returner(); |
---|
| 220 | |
---|
[259] | 221 | // find wanted menu |
---|
| 222 | $xml = $this->xml->xpath($xpath_menu_selection); |
---|
| 223 | if(!$xml) { |
---|
| 224 | print "Menu <i>$xpath_menu_selection</i> not found!"; |
---|
| 225 | return false; |
---|
| 226 | } |
---|
| 227 | $xml = $xml[0]; // just take the first result (should only one result be present) |
---|
| 228 | |
---|
[254] | 229 | // aktuelle Seite anmarkern und Hierarchie hochgehen |
---|
| 230 | // (<ul><li>bla<ul><li>bla<ul><li>hierbin ich <- hochgehen.) |
---|
| 231 | $current_a = $xml->xpath("//a[@seiten_id='$seiten_id']"); |
---|
| 232 | if(count($current_a)) { |
---|
| 233 | $current_li = $current_a[0]->xpath("parent::li"); |
---|
[289] | 234 | self::dom_add_class($current_li[0], 'current'); |
---|
[301] | 235 | self::dom_prepend_attribute($current_a, 'title', $_('nav-hierarchy-current'), ': '); |
---|
[289] | 236 | |
---|
| 237 | $actives = $current_li[0]->xpath("ancestor-or-self::li"); |
---|
| 238 | array_walk($actives, function($i) { t29Menu::dom_add_class($i, 'active'); }); |
---|
| 239 | |
---|
| 240 | $ancestors = $current_li[0]->xpath("ancestor::li"); |
---|
[301] | 241 | foreach($ancestors as $i) |
---|
| 242 | t29Menu::dom_prepend_attribute($i->xpath("./a[1]"), 'title', $_('nav-hierarchy-ancestor'), ': '); |
---|
[254] | 243 | } |
---|
| 244 | |
---|
| 245 | // Seiten-IDs (ungueltiges HTML) ummoddeln |
---|
| 246 | $all_ids = $xml->xpath("//a[@seiten_id]"); |
---|
| 247 | foreach($all_ids as $a) { |
---|
| 248 | $a['id'] = "sidebar_link_".$a['seiten_id']; |
---|
| 249 | // umweg ueber DOM um Node zu loeschen |
---|
| 250 | $adom = dom_import_simplexml($a); |
---|
| 251 | $adom->removeAttribute('seiten_id'); |
---|
| 252 | } |
---|
[278] | 253 | |
---|
| 254 | // Geraete-Seiten entfernen |
---|
[301] | 255 | if(self::hide_geraete_seiten) { |
---|
| 256 | $geraete_uls = $xml->xpath("//ul[contains(@class, 'geraete')]"); |
---|
| 257 | foreach($geraete_uls as $ul) { |
---|
| 258 | $uld = dom_import_simplexml($ul); |
---|
| 259 | $uld->parentNode->removeChild($uld); |
---|
| 260 | } |
---|
[278] | 261 | } |
---|
[347] | 262 | |
---|
| 263 | // alle Links mittels t29Host umwandeln (idR .php-Endung entfernen), |
---|
| 264 | // falls erwuenscht |
---|
| 265 | if($host) { |
---|
| 266 | $links = $xml->xpath("//a[@href]"); |
---|
| 267 | foreach($links as $a) |
---|
| 268 | $a['href'] = $host->rewrite_link($a['href']); |
---|
| 269 | } |
---|
[254] | 270 | |
---|
[259] | 271 | if($xpath_menu_selection == self::horizontal_menu) { |
---|
[254] | 272 | # inject news |
---|
| 273 | $news_ul_content = $this->convert_news_data(); |
---|
| 274 | $magic_comment = '<!--# INSERT_NEWS #-->'; |
---|
| 275 | $menu = $xml->ul->asXML(); |
---|
| 276 | print str_replace($magic_comment, $news_ul_content, $menu); |
---|
| 277 | } else { |
---|
| 278 | print $xml->ul->asXML(); |
---|
| 279 | } |
---|
[251] | 280 | } |
---|
| 281 | |
---|
[259] | 282 | ///////////////////// PAGE RELATIONS |
---|
| 283 | /** |
---|
| 284 | * Usage: |
---|
| 285 | * foreach(get_page_relations() as $a) { |
---|
| 286 | * echo "Link $a going to $a[href]"; |
---|
| 287 | * } |
---|
[299] | 288 | * |
---|
| 289 | * Hinweis: |
---|
| 290 | * Wenn Element (etwa prev) nicht existent, nicht null zurueckgeben, |
---|
| 291 | * sondern Element gar nicht zurueckgeben (aus hash loeschen). |
---|
| 292 | * |
---|
[259] | 293 | * @param $seiten_id A seiten_id string or nothing for taking the current active string |
---|
| 294 | * @returns an array(prev=>..., next=>...) or empty array, elements are SimpleXML a links |
---|
| 295 | **/ |
---|
| 296 | function get_page_relations($seiten_id=false) { |
---|
[275] | 297 | if($this->xml_is_defective()) |
---|
| 298 | return array(); // cannot construct relations due to bad XML file |
---|
[259] | 299 | if(!$seiten_id) $seiten_id = $this->conf['seiten_id']; |
---|
| 300 | |
---|
| 301 | $xml = $this->xml->xpath(self::sidebar_menu); |
---|
| 302 | if(!$xml) { print "<i>Sidebar not found</i>"; return; } |
---|
| 303 | $sidebar = $xml[0]; |
---|
| 304 | |
---|
| 305 | $return = array(); |
---|
[254] | 306 | $current_a = $sidebar->xpath("//a[@seiten_id='$seiten_id']"); |
---|
| 307 | if(count($current_a)) { |
---|
[299] | 308 | // wenn aktuelle seite eine geraeteseite ist |
---|
| 309 | if(in_array('geraete', $this->get_link_ul_classes($seiten_id))) { |
---|
[301] | 310 | // pfad: a ->li->ul.geraete->li->li/a |
---|
| 311 | $geraetelink = $current_a[0]->xpath("../../../a"); |
---|
| 312 | if(count($geraetelink)) |
---|
| 313 | $return['prev'] = $geraetelink[0]; |
---|
| 314 | $return['next'] = null; // kein Link nach vorne |
---|
[299] | 315 | } else { |
---|
| 316 | foreach(array( |
---|
| 317 | "prev" => "preceding::a[@seiten_id]", |
---|
| 318 | "next" => "following::a[@seiten_id]") as $rel => $xpath) { |
---|
| 319 | $nodes = $current_a[0]->xpath($xpath); |
---|
| 320 | foreach($rel == "prev" ? array_reverse($nodes) : $nodes as $link) { |
---|
| 321 | $is_geraete = count($link->xpath("ancestor::ul[contains(@class, 'geraete')]")); |
---|
| 322 | if($is_geraete) continue; // skip geraete links |
---|
| 323 | $return[$rel] = $link; |
---|
| 324 | break; // just take the first matching element |
---|
| 325 | } |
---|
| 326 | } // end for prev next |
---|
| 327 | } // end if geraete |
---|
[254] | 328 | } else { |
---|
[259] | 329 | // TODO PENDING: Der Fall tritt derzeit niemals ein, da das XML |
---|
| 330 | // sich dann doch irgendwie auf alles bezieht ($sidebar = alles) und |
---|
| 331 | // ueberall gesucht wird. Ist aber okay. oder? |
---|
| 332 | $return['start'] = t29Menu::dom_new_link('#', 'bla'); |
---|
[254] | 333 | } |
---|
[301] | 334 | |
---|
| 335 | // Linkliste aufarbeiten: Nullen rausschmeissen (nur deko) und |
---|
| 336 | // Links *klonen*, denn sie werden durch print_menu sonst veraendert |
---|
| 337 | // ("Übergeordnete Kategorie der aktuellen Seite" steht dann drin) |
---|
| 338 | // und wir wollen sie unveraendert haben. |
---|
| 339 | foreach($return as $key => $node) { |
---|
| 340 | if(!$node) { |
---|
| 341 | unset($return[$key]); |
---|
| 342 | continue; |
---|
| 343 | } |
---|
| 344 | $dn = dom_import_simplexml($node); |
---|
| 345 | $dnc = simplexml_import_dom($dn->cloneNode(true)); |
---|
| 346 | $return[$key] = $dnc; |
---|
| 347 | } |
---|
| 348 | |
---|
[259] | 349 | return $return; |
---|
[254] | 350 | } |
---|
[251] | 351 | |
---|
[254] | 352 | } // class |
---|