/
NavigationBuilder.php
366 lines (337 loc) · 9.06 KB
/
NavigationBuilder.php
1
2
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/**
* NavigationBuilder
*
* Works best with Nette Framework
*
* This source file is subject to the New BSD License.
*
* @copyright Copyright (c) 2009 Karel Klima
* @license New BSD License
* @package Nette Extras
* @version NavigationBuilder 1.0, 2009-10-18
*/
/**
* NavigationBuilder control
*
* @author Karel Klima
* @copyright Copyright (c) 2009 Karel Klíma
* @package Nette Extras
*/
class XNavigationBuilder extends XNavigationNode
{
protected $translator;
public function __construct()
{
$this->items = new ArrayList();
$this->builder = $this;
}
/**
* Shortcut for Template::setFile();
* @param string $file
* @return NavigationBuilder
*/
public function setTemplate($file)
{
$this->template->setFile($file);
return $this;
}
/**
* Translates labels of added items
* @param ITranslator $translator
* @return NavigationBuilder
*/
public function setTranslator(ITranslator $translator)
{
$this->translator = $translator;
// Puts the translator to the template
$this->template->setTranslator($translator);
return $this;
}
/**
* Returns current translator
* @return FALSE|ITranslator
*/
public function getTranslator()
{
return ($this->translator instanceof ITranslator) ? $this->translator : FALSE;
}
/**
* Renders navigation
* @return void
*/
public function render()
{
// Sorts navigation items
$this->sort();
// Puts navigation items into the template
$this->template->items = $this->items;
if (!is_file($this->template->getFile())) {
$helpers = $this->template->getHelpers();
// Sets default template according to availability of ITranslator
if (isset($helpers['translate'])) {
$this->template->setFile(dirname(__FILE__) . '/template_translate.phtml');
} else {
$this->template->setFile(dirname(__FILE__) . '/template.phtml');
}
}
$this->template->render();
}
}
/**
* NavigationNode control, base of NavigationBuilder
*
* @author Karel Klima
* @copyright Copyright (c) 2009 Karel Klíma
* @package Nette Extras
*/
class XNavigationNode extends Component
{
const SORT_NONE = 0, // leaves items in order they are added
SORT_PRIORITY = 1, // sorts by priority
SORT_PRIORITY_INTEGER = 2, // converts priority to float and then sorts
SORT_PRIORITY_STRING = 3, // converts priority to string and then sorts
SORT_LABEL = 4; // sorts by label
/** @var string */
public $label;
/** @var string */
public $url;
/** @var mixed */
protected $priority;
/** @var mixed */
protected $defaultPriority = NULL;
/** @var string */
public $attributes = array();
/** @var int */
protected $sortBy = 0;
/** @var ArrayList */
public $items;
/** @var NavigationBuilder */
protected $builder;
/**
* Navigation item setup
* @param string $label
* @param string $url
*/
public function __construct(NavigationBuilder $builder, $label, $url = '#', $priority = NULL, $attributes = array(), $sortBy = 0, $defaultPriority = NULL)
{
// checks if given label is valid
if (!is_string($label) || empty($label)) {
$type = gettype($label);
throw new InvalidArgumentException("Label parameter must be be a string and must not be empty, '$label' of type $type given.");
}
// checks if given attributes are valid
if (!is_array($attributes)) {
$type = gettype($attributes);
throw new InvalidArgumentException("Attributes parameter must be an array, $type given");
}
// initiate the children container
$this->items = new ArrayList();
$this->builder = $builder;
$this->url = $url;
$this->label = $label;
$this->priority = $priority;
$this->defaultPriority = $defaultPriority;
$this->attributes = $attributes;
$this->sortBy($sortBy);
}
/**
* Adds an item to the navigation tree
* @param string $label name of item or NavigationNode object
* @param string $url
* @return NavigationNode
*/
public function add($label, $url = '#', $priority = NULL, $attributes = array(), $sortBy = NULL, $defaultPriority = NULL)
{
// sets default priority if not given
if ($priority == NULL) {
$priority = $this->defaultPriority;
}
// sets default priority for item's children if not given
if ($defaultPriority == NULL) {
$defaultPriority = $this->defaultPriority;
}
// sets default sorting policy for item's children if not given
if ($sortBy == NULL) {
$sortBy = $this->sortBy;
}
// creates the item
$this->items[] = new XNavigationNode($this->builder, $label, $url, $priority, $attributes, $sortBy, $defaultPriority);
return $this;
}
/**
* Gets an item from the navigation tree
* @param string $label
* @return NavigationNode|FALSE false if item not found
*/
public function get($label)
{
foreach ($this->items as $item) {
if ($item->label == $label) return $item;
}
return FALSE;
}
/**
* Removes an item (or items) from the navigation tree
* @param string $label
* @param string $url
* @return NavigationNode
*/
public function remove($label)
{
foreach ($this->items as $item) {
if ($item->label == $label) $this->items->remove($item);
}
return $this;
}
/**
* Defines how to sort item's childs
* @param int $flag
* @param bool $multilevel whether or not to apply sort to item's children
* @return NavigationNode
*/
public function sortBy($flag = self::SORT_NONE, $multiLevel = TRUE)
{
$possibleFlags = array(
self::SORT_NONE,
self::SORT_PRIORITY,
self::SORT_PRIORITY_INTEGER,
self::SORT_PRIORITY_STRING,
self::SORT_LABEL
);
if (!in_array($flag, $possibleFlags)) {
throw new InvalidArgumentException("Invalid sorting flag, '$flag' given");
}
$this->sortBy = $flag;
if ($multiLevel) {
foreach ($this->items as $item)
$item->sortBy($flag, $multiLevel);
}
return $this;
}
/**
* Sorts item's children right before rendering
*/
public function sort()
{
$items = (array) $this->items;
switch ($this->sortBy)
{
case self::SORT_NONE:
break;
case self::SORT_PRIORITY:
usort($items, array($this, 'comparePriority'));
break;
case self::SORT_PRIORITY_INTEGER:
usort($items, array($this, 'comparePriorityInteger'));
break;
case self::SORT_PRIORITY_STRING:
usort($items, array($this, 'comparePriorityString'));
break;
case self::SORT_LABEL:
usort($items, array($this, 'compareLabel'));
break;
}
$this->items->import($items);
foreach ($this->items as $item) {
$item->sort();
}
}
/**
* Helper for usort, compares priority values
* @param NavigationNode $item1
* @param NavigationNode $item2
* @param int $sortingMethod
* @return int;
*/
public function comparePriority(NavigationNode $item1, NavigationNode $item2, $sortingMethod = self::SORT_PRIORITY)
{
switch ($sortingMethod)
{
case self::SORT_PRIORITY:
$sortable = array($item1->priority, $item2->priority);
break;
case self::SORT_PRIORITY_INTEGER:
$sortable = array((float) $item1->priority, (float) $item2->priority);
break;
case self::SORT_PRIORITY_STRING:
$sortable = array(String::lower((string) $item1->priority), String::lower((string) $item2->priority));
break;
default:
throw new InvalidStateException("No match for sorting method");
}
if ($sortable[0] == $sortable[1]) return 0;
$sorted = $sortable;
sort($sorted);
return ($sorted[0] == $sortable[0]) ? -1 : 1;
}
/**
* Helper for usort, compares priority values numerically
* @param NavigationNode $item1
* @param NavigationNode $item2
* @return int
*/
public function comparePriorityInteger($item1, $item2)
{
return $this->comparePriority($item1, $item2, self::SORT_PRIORITY_INTEGER);
}
/**
* Helper for usort, compares priority values alphabetically
* @param NavigationNode $item1
* @param NavigationNode $item2
* @return int
*/
public function comparePriorityString($item1, $item2)
{
return $this->comparePriority($item1, $item2, self::SORT_PRIORITY_STRING);
}
/**
* Helper for usort, compares labels alphabetically
* Compares translations of labels if a translator is set
* @param NavigationNode $item1
* @param NavigationNode $item2
* @return int
*/
public function compareLabel(NavigationNode $item1, NavigationNode $item2)
{
$translator = $this->getBuilder()->getTranslator();
if ($translator instanceof ITranslator) {
$label1 = $translator->translate($item1->label);
$label2 = $translator->translate($item2->label);
} else {
$label1 = $item1->label;
$label2 = $item2->label;
}
$sortable = array(String::lower((string) $label1), String::lower((string) $label2));
if ($sortable[0] == $sortable[1]) return 0;
$sorted = $sortable;
sort($sorted);
return ($sorted[0] == $sortable[0]) ? -1 : 1;
}
/**
* Sets default priority
* @param mixed $value
* @return NavigationNode
*/
function setDefaultPriority($value)
{
$this->defaultPriority = $value;
return $this;
}
/**
* Gets current priority of an item
* @return mixed priority
*/
public function getPriority()
{
return $this->priority;
}
/**
* Gets the original NavigationBuilder instance
* @return NavigationBuilder
*/
public function getBuilder()
{
return $this->builder;
}
}