1: <?php
2: namespace Module\Navigation;
3:
4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52:
53: class Builder {
54: 55: 56: 57:
58: private $current;
59:
60: 61: 62:
63: private $data;
64:
65: 66: 67:
68: private $_level = 0;
69:
70: 71: 72: 73:
74: public function __construct($json='') {
75: if (!is_object($json)) {
76: if (class_exists('\Sleepy\Hook')) {
77: $json = \Sleepy\Hook::addFilter('navigation_raw_json', $json);
78: }
79:
80: $json = json_decode($json);
81: }
82:
83: if (class_exists('\Sleepy\Hook')) {
84: $json = \Sleepy\Hook::addFilter('navigation_rendered_json', $json);
85: }
86:
87: $this->data = $json;
88: $this->setCurrent($_SERVER['SCRIPT_NAME']);
89: }
90:
91: 92: 93: 94: 95: 96:
97: private function hasActive($page) {
98:
99: if (isset($page->pages)) {
100: foreach ($page->pages as $subPage) {
101: if ($this->hasActive($subPage)) {
102: return 2;
103: }
104: }
105: }
106:
107:
108: if (substr($page->link, strlen($page->link) * -1) === $this->current) {
109: if (class_exists('\Sleepy\Hook')) {
110: \Sleepy\Hook::addAction('navigation_has_active');
111: }
112:
113: return 1;
114: }
115:
116:
117: if (class_exists('\Sleepy\Hook')) {
118: \Sleepy\Hook::addAction('navigation_no_active');
119: }
120:
121: return 0;
122: }
123:
124: 125: 126: 127: 128:
129: private function renderNav($pages, $class="") {
130: $this->_level = $this->_level + 1;
131: $buffer = array();
132:
133: if ($this->_level > 1) {
134: $class = "submenu " . $class;
135: } else {
136: $class = "menu " . $class;
137: }
138:
139: $class = trim($class);
140:
141: $buffer[] = "<ul class=\"{$class}\">";
142:
143: foreach ($pages as $page) {
144: if (class_exists('\Sleepy\Hook')) {
145: $page = \Sleepy\Hook::addFilter('navigation_page', $page);
146:
147: if (!empty($page->id)) {
148: $page = \Sleepy\Hook::addFilter('navigation_page_' . $page->id, $page);
149: }
150: }
151:
152: if (!isset($page->class)) {
153: $page->class = "";
154: }
155:
156: if (isset($page->pages)) {
157: $page->class = $page->class . " has-children";
158: }
159:
160: $active = $this->hasActive($page);
161: $classy = (!empty($page->class)) ? true : false;
162: $track = (!empty($page->track)) ? "data-track=\"{$page->track}\" " : "";
163: $id = (!empty($page->id)) ? "id=\"{$page->id}\" " : "";
164: $target = (!empty($page->target)) ? "target=\"{$page->target}\" " : "";
165: $href = (!empty($page->link)) ? "href=\"{$page->link}\" " : "";
166: $attributes = trim($id . $track . $target . $href);
167:
168: $buffer[] = "<li";
169:
170: if ($active || $classy) {
171: $buffer[] = " class=\"";
172:
173: switch ($active) {
174: case 1:
175: $page->class = $page->class . " active";
176: break;
177: case 2:
178: $page->class = $page->class . " active-child";
179: }
180:
181: $buffer[] = trim($page->class);
182:
183: $buffer[] = "\"";
184: }
185:
186: $buffer[] = ">";
187:
188: $buffer[] = "<a {$attributes}>{$page->title}</a>";
189:
190: if (isset($page->pages)) {
191: $buffer[] = $this->renderNav($page->pages);
192: }
193:
194: $buffer[] = "</li>";
195: }
196: $buffer[] = "</ul>";
197:
198: return implode("", $buffer);
199: }
200:
201: 202: 203: 204:
205: public function show($class="") {
206: $rendered = $this->renderNav($this->data->pages, $class);
207:
208: if (class_exists('\Sleepy\Hook')) {
209: $rendered = \Sleepy\Hook::addFilter('navigation_rendered', $rendered);
210: }
211:
212: return $rendered;
213: }
214:
215: 216: 217: 218:
219: public function setCurrent($string) {
220: $this->current = str_replace(@URLBASE, "/", str_replace("index.php", "", $string));
221:
222: if (class_exists('\Sleepy\Hook')) {
223: $this->current = \Sleepy\Hook::addFilter('navigation_current_page', $this->current);
224: }
225: }
226: }