1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Pages;
13
14 use ICanBoogie\Event;
15
16 use Brickrouge\Element;
17
18 class NavigationElement extends Element
19 {
20 public function __construct()
21 {
22
23 }
24
25 static protected function render_navigation_tree(array $tree, $depth=1)
26 {
27 $rc = '';
28
29 foreach ($tree as $branch)
30 {
31 $record = $branch->record;
32 $class = $record->css_class('-constructor -slug');
33
34 $rc .= $class ? '<li class="' . $class . '">' : '<li>';
35 $rc .= '<a href="' . $record->url . '">' . $record->label . '</a>';
36
37 if ($branch->children)
38 {
39 $rc .= static::render_navigation_tree($branch->children, $depth + 1);
40 }
41
42 $rc .= '</li>';
43 }
44
45 return '<ol class="' . ($depth == 1 ? 'nav' : 'dropdown-menu') . ' lv' . $depth . '">' . $rc . '</ol>';
46 }
47
48 static public function markup(array $args, \Patron\Engine $patron, $template)
49 {
50 global $core;
51
52 $page = $core->request->context->page;
53 $mode = $args['mode'];
54
55 if ($mode == 'leaf')
56 {
57 $node = $page;
58
59 while ($node)
60 {
61 if ($node->navigation_children)
62 {
63 break;
64 }
65
66 $node = $node->parent;
67 }
68
69 if (!$node)
70 {
71 return;
72 }
73
74 return $patron($template, $node);
75 }
76
77
78 $model = $core->models['pages'];
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 $depth = $args['depth'];
95
96 if ($args['from-level'])
97 {
98 $node = $page;
99 $from_level = $args['from-level'];
100
101
102
103
104
105
106 if ($node->depth < $from_level)
107 {
108 return;
109 }
110
111 while ($node->depth > $from_level)
112 {
113 $node = $node->parent;
114 }
115
116
117
118 $parentid = $node->nid;
119 }
120 else
121 {
122 $parentid = $args['parent'];
123
124 if (is_object($parentid))
125 {
126 $parentid = $parentid->nid;
127 }
128 else
129 {
130 if ($parentid && !is_numeric($parentid))
131 {
132 $parent = $model->find_by_path($parentid);
133
134 $parentid = $parent->nid;
135 }
136 }
137 }
138
139 $blueprint = $model->blueprint($page->siteid);
140 $min_child = $args['min-child'];
141
142 $subset = $blueprint->subset
143 (
144 $parentid, $depth === null ? null : $depth - 1, function($branch) use($min_child)
145 {
146 if ($min_child && count($branch->children) < $min_child)
147 {
148 return true;
149 }
150
151 return (!$branch->is_online || $branch->is_navigation_excluded || $branch->pattern);
152 }
153 );
154
155 $html = null;
156 $tree = $subset->tree;
157
158 if ($tree)
159 {
160 $subset->populate();
161
162 $html = $template ? $patron($template, $tree) : static::render_navigation_tree($tree);
163 }
164
165 new NavigationElement\AlterEvent(new self, $html, $page, $blueprint, $args);
166
167 return $html;
168 }
169 }
170
171 namespace Icybee\Modules\Pages\NavigationElement;
172
173 use Icybee\Modules\Pages\Blueprint;
174 use Icybee\Modules\Pages\NavigationElement;
175 use Icybee\Modules\Pages\Page;
176
177 class AlterEvent extends \ICanBoogie\Event
178 {
179 180 181 182 183
184 public $html;
185
186 187 188 189 190
191 public $page;
192
193 194 195 196 197
198 public $blueprint;
199
200 201 202 203 204
205 public $options;
206
207 208 209 210 211 212 213 214 215
216 public function __construct(NavigationElement $target, &$html, Page $page, Blueprint $blueprint, array $options)
217 {
218 $this->html = &$html;
219 $this->page = $page;
220 $this->blueprint = $blueprint;
221 $this->options = $options;
222
223 parent::__construct($target, 'alter');
224 }
225 }