1 <?php
2 class Navigation {
3
4 private $struct;
5 private $output = "";
6 private $active = "";
7 private $found = false;
8
9 function __construct() {
10 $this->struct = simplexml_load_file(Controller::$root."data/navigation.xml");
11 }
12
13 14 15 16 17 18
19 function build($active=null,$struct=NULL,$level=0) {
20
21 if(is_null($struct)) $struct = $this->struct;
22 if(is_null($active)) $active = $this->getActive();
23 $build_flag = false;
24
25 $this->output .= "<ul id='lvl$level'>\n";
26 foreach($struct as $child) {
27
28 $href = isset($child['href']) ? $child['href'] : $this->firstChild($child);
29
30 $active_class = ($active == $href) ? " class='active'" : "";
31
32 $this->find($active,$child);
33
34 if($this->found) {
35 $build_flag = $child;
36 $active_class = " class='active'";
37 $this->found = false;
38 }
39 $qs = $this->addVars($child);
40 $this->output .= "\t<li><a href='{$href}{$qs}'{$active_class}>".$child['name']."</a></li>\n";
41
42 }
43 $this->output .= "</ul>\n";
44 if($build_flag) $this->build($active,$build_flag,$level+1);
45 }
46
47 48 49 50
51 private function firstChild($struct) {
52 if($struct->page[0]['href']) {
53 return $struct->page[0]['href'];
54 }
55 return $this->firstChild($struct->page[0]);
56 }
57
58 59 60 61 62
63 private function find($href,$struct) {
64
65 foreach($struct as $child) {
66 if(isset($child['href']) && $child['href'] == $href) {
67 $this->found = true;
68 } else {
69 $this->find($href,$child);
70 }
71 }
72 }
73
74 public function asList() {
75 $this->build();
76 return $this->output;
77 }
78
79 80 81 82
83 private function getActive() {
84 $path = $_SERVER['SCRIPT_NAME'];
85 return substr($path,strrpos($path,"/")+1,strlen($path));
86 }
87
88 private function addVars($page) {
89 if(isset($page['var'])) {
90 $vars = explode(",",$page['var']);
91 $data = array();
92 foreach($vars as $var) {
93 if(isset($_GET[$var]))
94 $data[$var] = $_GET[$var];
95 }
96 return "?".http_build_query($data);
97 }
98 return "";
99 }
100 }
101 ?>
102