source: t29-www/lib/menu.php @ 279

Last change on this file since 279 was 279, checked in by sven, 12 years ago

Design: Linkfarben, Footerrelationslinks, Einblendlogik von Relationen.

  • menu.php: Linkdetail-Getter umgeformt
  • template.php: Mehr Daten in bodyclasses und javascriptconfig durchgeben
  • a.go-Doppelfeile, mal testweise per content-before umgesetzt
  • Relationale Navigation huebscher Formatiert, nach dem Vorschlag von Intsar vom 23.04.2012
  • Inhalts-Linkfarben mit dunkler Linkfarbe
  • menu.js: Beam nur anzeigen wenn im Inhalt, ebenso mit Relationslinks (template.php)
  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
1<?php
2/**
3 * Needs conf:
4 *  webroot lang_path lang seiten_id languages
5 *
6 **/
7class 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       
74        /**
75         * Find the corresponding XML node in the navigation tree for the link
76         * with given $seiten_id or the current given seiten_id in the configuration
77         * array.
78         * This method is used in get_link_navigation_class, etc. for resolving
79         * the XML element from the string. They can be used with the XML node, too,
80         * and this behaviour is passed throught by this method, so if you call
81         * this with an SimpleXMLElement as argument, it behaves like an identity
82         * function and just does nothing.
83         * (This is used in template.php for caching the found xml element and saving
84         * several xpath querys on get_* calls)
85         *
86         * @param $seiten_id Either a string, or nothing (defaults to conf) or SimpleXMLElement
87         * @returns SimpleXMLElement or null if link not found
88         **/
89        function get_link($seiten_id=false) {
90                if($this->xml_is_defective()) {
91                        return null;
92                }
93                if(!$seiten_id) $seiten_id = $this->conf['seiten_id'];
94                // convenience: If you found your link already.
95                if($seiten_id instanceof SimpleXMLElement) return $seiten_id;
96
97                $matches = $this->xml->xpath("//a[@seiten_id='$seiten_id']");
98                if($matches && count($matches)) {
99                        // strip the first one
100                        return $matches[0];
101                }
102                return null;
103        }
104       
105        /**
106         * Get navigation list membership (horizontal or side) of a link
107         * @see get_link for parameters
108         * @returns String of <nav> class where this link is affiliated to
109         **/
110        function get_link_navigation_class($seiten_id=false) {
111                $link = $this->get_link($seiten_id);
112                if(!$link) return null;
113               
114                // navigation list membership
115                $nav = $link->xpath("ancestor::nav");
116                return strval($nav[0]['class']); // cast SimpleXMLElement
117        }
118
119        /**
120         * Get list of parental ul classes (u2, u3, geraete, ...)
121         * @see get_link for parameters
122         * @returns array with individual class names as strings
123         **/
124        function get_link_ul_classes($seiten_id=false) {
125                $link = $this->get_link($seiten_id);
126                if(!$link) return array();
127               
128                // direct parental ul classes
129                $ul = $link->xpath("ancestor::ul");
130                $parent_ul = array_pop($ul);
131                return explode(' ',$parent_ul['class']);
132        }
133
134
135        ///////////////////// INTER LANGUAGE DETECTION
136        /**
137         * @param seiten_id Get interlanguage link for that seiten_id or default.
138         **/
139        function get_interlanguage_link($seiten_id=false) {
140                if(!$seiten_id) $seiten_id = $this->conf['seiten_id'];
141               
142                $interlinks = array(); // using a loop instead of mappings since php is stupid
143                foreach($this->conf['languages'] as $lang => $lconf) {
144                        $foreign_menu = new t29Menu(array(
145                                'webroot' => $this->conf['webroot'],
146                                'seiten_id' => $this->conf['seiten_id'],
147                                'languages' => $this->conf['languages'],
148                                'lang' => $lang,
149                                'lang_path' => $lconf[1],
150                        ));
151
152                        $link = $foreign_menu->get_link($seiten_id);
153                        $interlinks[$lang] = $link;
154                }
155               
156                return $interlinks;
157        }
158
159        // helper method
160        public static function dom_add_class($simplexml_element, $value) {
161                $dom = dom_import_simplexml($simplexml_element); // is a fast operation
162                $simplexml_element['class'] = 
163                        ($dom->hasAttribute("class") ? ($simplexml_element['class'].' '):'').$value;
164        }
165       
166        public static function dom_new_link($href, $label) {
167                return new SimpleXMLElement(sprintf('<a href="%s">%s</a>', $href, $label));
168        }
169
170
171        ///////////////////// MENU ACTIVE LINK DETECTION
172        /**
173         * @arg $xpath_menu_selection  one of the horizontal_menu / sidebar_menu consts.
174         **/
175        function print_menu($xpath_menu_selection) {
176                if($this->xml_is_defective()) {
177                        print "The Menu file is broken.";
178                        return false;
179                }
180                $seiten_id = $this->conf['seiten_id'];
181
182                // find wanted menu
183                $xml = $this->xml->xpath($xpath_menu_selection);
184                if(!$xml) {
185                        print "Menu <i>$xpath_menu_selection</i> not found!";
186                        return false;
187                }
188                $xml = $xml[0]; // just take the first result (should only one result be present)
189
190                // aktuelle Seite anmarkern und Hierarchie hochgehen
191                // (<ul><li>bla<ul><li>bla<ul><li>hierbin ich <- hochgehen.)
192                $current_a = $xml->xpath("//a[@seiten_id='$seiten_id']");
193                if(count($current_a)) {
194                        $current_li = $current_a[0]->xpath("parent::li");
195                        $this->dom_add_class($current_li[0], "current");
196                        $ancestors = $current_li[0]->xpath("ancestor-or-self::li");
197                        array_walk($ancestors, create_function('$i', 't29Menu::dom_add_class($i, "active");'));
198                }
199
200                // Seiten-IDs (ungueltiges HTML) ummoddeln
201                $all_ids = $xml->xpath("//a[@seiten_id]");
202                foreach($all_ids as $a) {
203                        $a['id'] = "sidebar_link_".$a['seiten_id'];
204                        // umweg ueber DOM um Node zu loeschen
205                        $adom = dom_import_simplexml($a);
206                        $adom->removeAttribute('seiten_id');
207                }
208
209                // Geraete-Seiten entfernen
210                $geraete_uls = $xml->xpath("//ul[contains(@class, 'geraete')]");
211                foreach($geraete_uls as $ul) {
212                        $uld = dom_import_simplexml($ul);
213                        $uld->parentNode->removeChild($uld);
214                }
215       
216                if($xpath_menu_selection == self::horizontal_menu) {
217                        # inject news
218                        $news_ul_content = $this->convert_news_data();
219                        $magic_comment = '<!--# INSERT_NEWS #-->';
220                        $menu = $xml->ul->asXML();
221                        print str_replace($magic_comment, $news_ul_content, $menu);
222                } else {
223                        print $xml->ul->asXML();
224                }
225        }
226
227        ///////////////////// PAGE RELATIONS
228        /**
229         * Usage:
230         * foreach(get_page_relations() as $a) {
231         *    echo "Link $a going to $a[href]";
232         * }
233         * @param $seiten_id A seiten_id string or nothing for taking the current active string
234         * @returns an array(prev=>..., next=>...) or empty array, elements are SimpleXML a links
235         **/
236        function get_page_relations($seiten_id=false) {
237                if($this->xml_is_defective())
238                        return array(); // cannot construct relations due to bad XML file
239                if(!$seiten_id) $seiten_id = $this->conf['seiten_id'];
240               
241                $xml = $this->xml->xpath(self::sidebar_menu);
242                if(!$xml) { print "<i>Sidebar not found</i>"; return; }
243                $sidebar = $xml[0];
244               
245               
246                $return = array();
247                $current_a = $sidebar->xpath("//a[@seiten_id='$seiten_id']");
248                if(count($current_a)) {
249                        foreach(array(
250                          "prev" => "preceding::a[@seiten_id]",
251                          "next" => "following::a[@seiten_id]") as $rel => $xpath) {
252                                $nodes = $current_a[0]->xpath($xpath);
253                                foreach($rel == "prev" ? array_reverse($nodes) : $nodes as $link) {
254                                        $is_geraete = count($link->xpath("ancestor::ul[contains(@class, 'geraete')]"));
255                                        if($is_geraete) continue; // skip geraete links
256                                        $return[$rel] = $link;
257                                        break; // just take the first matching element
258                                }
259                        }
260                } else {
261                        // TODO PENDING: Der Fall tritt derzeit niemals ein, da das XML
262                        // sich dann doch irgendwie auf alles bezieht ($sidebar = alles) und
263                        // ueberall gesucht wird. Ist aber okay. oder?
264                        $return['start'] = t29Menu::dom_new_link('#', 'bla');
265                }
266                return $return;
267        }
268
269} // class
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License