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

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

Backend: Class cleanup, multi language support builtin, Message system (with experimental JSON connection). Template is ready to be deployed for multi language.

File size: 3.6 KB
Line 
1<?php
2
3class t29Menu {
4        public $conf;
5
6        const horizontal_menu = 'hauptnavigation.xml';
7        const sidebar_menu = 'sidebar.xml';
8        const news_file = 'news.php';
9
10        function __construct($conf_array) {
11                $this->conf = $conf_array;
12        }
13       
14        function load_news_data() {
15                $newsfile = $this->conf['webroot'].$this->conf['lang_path']."/".self::news_file;
16                $newsdir = dirname(realpath($newsfile));
17                // include path wird ignoriert wenn include relativ ist, was in der
18                // eingebundenen Datei der Fall ist
19                // set_include_path( get_include_path(). PATH_SEPARATOR . dirname($newsfile));
20                $pwd = getcwd(); chdir($newsdir);
21                include(self::news_file);
22                chdir($pwd);
23                return $neues_menu;
24        }
25
26        function convert_news_data() {
27                require $this->conf['lib'].'/spyc.php';
28                $data = Spyc::YAMLLoad($this->load_news_data());
29                $fields = array('titel', 'text', 'link', 'bild');
30
31                $news_ul_content = '';
32                foreach($data as $e) {
33                        if(!array_reduce(array_map(function($x) use ($fields,$e){ return isset($e[$x]); }, $fields),
34                                        function($a,$b){ return $a && $b;}, true)) {
35                                $li = "<li>Fehler in Formatierung!";
36                        } else {
37                                $url = ($e['link']{0} == '#' ? '/'.$this->conf['lang'].'/'.self::news_file : '').$e['link'];
38                                $li = "<li><a href='$url'><img src='$e[bild]' /> $e[titel]<span class='hidden'>: </span><em>$e[text]</em></a></li>";
39                        }
40                        $news_ul_content .= "\t".$li."\n";
41                }
42
43                return $news_ul_content;
44        }
45
46        // helper method
47        public static function dom_add_class($simplexml_element, $value) {
48                $dom = dom_import_simplexml($simplexml_element);
49                $simplexml_element['class'] = 
50                        ($dom->hasAttribute("class") ? ($simplexml_element['class'].' '):'').$value;
51        }
52
53        function print_menu($file) {
54                $seiten_id = $this->conf['seiten_id'];
55                $xml = simplexml_load_file($this->conf['webroot'].$this->conf['lang_path'] . '/' . $file);
56       
57                // aktuelle Seite anmarkern und Hierarchie hochgehen
58                // (<ul><li>bla<ul><li>bla<ul><li>hierbin ich <- hochgehen.)
59                $current_a = $xml->xpath("//a[@seiten_id='$seiten_id']");
60                if(count($current_a)) {
61                        $current_li = $current_a[0]->xpath("parent::li");
62                        $this->dom_add_class($current_li[0], "current");
63                        $ancestors = $current_li[0]->xpath("ancestor-or-self::li");
64                        array_walk($ancestors, create_function('$i', 't29Menu::dom_add_class($i, "active");'));
65                }
66
67                // Seiten-IDs (ungueltiges HTML) ummoddeln
68                $all_ids = $xml->xpath("//a[@seiten_id]");
69                foreach($all_ids as $a) {
70                        $a['id'] = "sidebar_link_".$a['seiten_id'];
71                        // umweg ueber DOM um Node zu loeschen
72                        $adom = dom_import_simplexml($a);
73                        $adom->removeAttribute('seiten_id');
74                }
75       
76                if($file == self::horizontal_menu) {
77                        # inject news
78                        $news_ul_content = $this->convert_news_data();
79                        $magic_comment = '<!--# INSERT_NEWS #-->';
80                        $menu = $xml->ul->asXML();
81                        print str_replace($magic_comment, $news_ul_content, $menu);
82                } else {
83                        print $xml->ul->asXML();
84                }
85        }
86
87        function print_relations() {
88                $seiten_id = $this->conf['seiten_id'];
89       
90                $sidebar = simplexml_load_file($this->conf['webroot'] . $this->conf['lang_path'] . '/' . self::sidebar_menu);
91                $current_a = $sidebar->xpath("//a[@seiten_id='$seiten_id']");
92                if(count($current_a)) {
93                        $prev = $current_a[0]->xpath("preceding::a[@seiten_id][1]");
94                        if(count($prev)) {
95                                $a = $prev[0];
96                                print "<li class='prev'><a href='$a[href]'>vorherige Seite <strong>$a</strong></a></li>";
97                        }
98                        $next = $current_a[0]->xpath("following::a[@seiten_id][1]");
99                        if(count($next)) {
100                                $a = $next[0];
101                                print "<li class='next'><a href='$a[href]'>nächste Seite <strong>$a</strong></a></li>";
102                        }
103                } else {
104                        print '<li class="start"><a href="#">Starte Führung <strong>Blabla</strong></a>';
105                }
106        }
107
108} // 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