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

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

Weiterarbeit an t29v6 (5 Stunden):

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