|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rudolf\Modules\Appearance\Menu; |
|
4
|
|
|
|
|
5
|
|
|
use Rudolf\Component\Helpers\Navigation\MenuItem; |
|
|
|
|
|
|
6
|
|
|
use Rudolf\Component\Helpers\Navigation\MenuItemCollection; |
|
7
|
|
|
|
|
8
|
|
|
class Navigation |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var int |
|
12
|
|
|
*/ |
|
13
|
|
|
private $rootID = 0; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $type; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var MenuItemCollection |
|
22
|
|
|
*/ |
|
23
|
|
|
private $menuItemsCollection; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
private $nesting; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set root ID. |
|
31
|
|
|
* |
|
32
|
|
|
* @param int $id ID of element to start create tree. Set 0 to create full tree |
|
33
|
|
|
*/ |
|
34
|
|
|
public function setRootID($id) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->rootID = (int) $id; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return int |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getRootID() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->rootID; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Menu type defined in menu_types table. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $type |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setType($type) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->type = $type; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getType() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->type; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Set items. |
|
67
|
|
|
* |
|
68
|
|
|
* @param MenuItemCollection $items |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setItems(MenuItemCollection $items) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->menuItemsCollection = $items; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getItems() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->menuItemsCollection->getByType($this->getType()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set generated menu code nesting. |
|
85
|
|
|
* |
|
86
|
|
|
* @param int $nesting |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setNesting($nesting) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->nesting = $nesting; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return mixed |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getNesting() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->nesting; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Put value is not empty. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $attribute |
|
105
|
|
|
* @param string|array $value |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
View Code Duplication |
private function isAttribute($attribute, $value) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
if (is_array($value)) { |
|
112
|
|
|
array_filter($value); |
|
113
|
|
|
$value = trim(implode(' ', $value)); |
|
114
|
|
|
|
|
115
|
|
|
return !empty($value) ? ' '.$attribute.'="'.$value.'"' : ''; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return (isset($value) and !empty($value)) ? ' '.$attribute.'="'.trim($value).'"' : ''; |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Menu creator. |
|
124
|
|
|
* |
|
125
|
|
|
* @link http://pastebin.com/GAFvSew4 |
|
126
|
|
|
* |
|
127
|
|
|
* @author J. Bruni - original author |
|
128
|
|
|
* |
|
129
|
|
|
* @return string|bool |
|
130
|
|
|
*/ |
|
131
|
|
|
public function create() |
|
132
|
|
|
{ |
|
133
|
|
|
$root_id = $this->getRootID(); |
|
134
|
|
|
$items = $this->getItems(); |
|
135
|
|
|
$nesting = $this->getNesting(); |
|
136
|
|
|
|
|
137
|
|
|
if (empty($items)) { |
|
138
|
|
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
View Code Duplication |
foreach ($items as $item) { |
|
|
|
|
|
|
142
|
|
|
if (null !== $item->getParentId()) { |
|
143
|
|
|
$children[$item->getParentId()][] = $item; |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// loop will be false if the root has no children (i.e., an empty menu!) |
|
148
|
|
|
$loop = !empty($children[$root_id]); |
|
149
|
|
|
|
|
150
|
|
|
// initializing $parent as the root |
|
151
|
|
|
$parent = $root_id; |
|
152
|
|
|
$parent_stack = array(); |
|
153
|
|
|
|
|
154
|
|
|
$html = []; |
|
155
|
|
|
|
|
156
|
|
|
// HTML wrapper for the menu (open) |
|
157
|
|
|
$html[] = '<ul>'; |
|
158
|
|
|
|
|
159
|
|
|
$html[] = !empty($before['first_root_li']) ? str_repeat("\t", $nesting + 1).$before['first_root_li'] : ''; |
|
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
// loop |
|
162
|
|
|
while ($loop && (($item = each($children[$parent])) || ($parent > $root_id))) { |
|
|
|
|
|
|
163
|
|
View Code Duplication |
if (is_object($item['value'])) { |
|
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @var MenuItem $obj |
|
166
|
|
|
*/ |
|
167
|
|
|
$obj = $item['value']; |
|
168
|
|
|
$item = [ |
|
169
|
|
|
'id' => $obj->getId(), |
|
170
|
|
|
'parent_id' => $obj->getParentId(), |
|
171
|
|
|
'title' => $obj->getTitle(), |
|
172
|
|
|
'slug' => $obj->getSlug(), |
|
173
|
|
|
'caption' => $obj->getCaption(), |
|
174
|
|
|
'position' => $obj->getPosition(), |
|
175
|
|
|
]; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
// HTML for menu item containing children (close) |
|
179
|
|
|
if ($item === false) { |
|
180
|
|
|
$parent = array_pop($parent_stack); |
|
181
|
|
|
$html[] = str_repeat("\t", (count($parent_stack) + 1) * 2 + $nesting).'</ul>'; |
|
182
|
|
|
$html[] = str_repeat("\t", (count($parent_stack) + 1) * 2 - 1 + $nesting).'</li>'; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
// HTML for menu item containing children (open) |
|
186
|
|
|
elseif (!empty($children[$item['id']])) { |
|
187
|
|
|
$tab = str_repeat("\t", (count($parent_stack) + 1) * 2 - 1 + $nesting); |
|
188
|
|
|
|
|
189
|
|
|
/* |
|
190
|
|
|
* <li> with <ul> |
|
191
|
|
|
*/ |
|
192
|
|
|
$html[] = sprintf( |
|
193
|
|
|
'%1$s'.'<li>%2$s - <a'.'%3$s'.' href="'.'%4$s'.'">%5$s</a> – pozycja: %6$s'. |
|
194
|
|
|
' <a href="/admin/appearance/menu/edit/%2$s" class="btn btn-primary btn-xs">Edytuj</a>'. |
|
195
|
|
|
' <a href="/admin/appearance/menu/del/%2$s" class="btn btn-danger btn-xs">Usuń</a>', |
|
196
|
|
|
# %1$s tabulation |
|
197
|
|
|
$tab, |
|
198
|
|
|
|
|
199
|
|
|
$item['id'], |
|
200
|
|
|
|
|
201
|
|
|
# %2$s a title="" |
|
202
|
|
|
$this->isAttribute('title', $item['caption']), |
|
203
|
|
|
|
|
204
|
|
|
# %3$s a href="" |
|
205
|
|
|
$item['slug'], |
|
206
|
|
|
|
|
207
|
|
|
# %4$s text inside item |
|
208
|
|
|
$item['title'], |
|
209
|
|
|
|
|
210
|
|
|
$item['position'] |
|
211
|
|
|
); |
|
212
|
|
|
|
|
213
|
|
|
/* |
|
214
|
|
|
* sub <ul> in <li> |
|
215
|
|
|
*/ |
|
216
|
|
|
$html[] = sprintf( |
|
217
|
|
|
'%1$s'.'<ul>', |
|
218
|
|
|
# %1$s tabulation |
|
219
|
|
|
$tab."\t" |
|
220
|
|
|
); |
|
221
|
|
|
|
|
222
|
|
|
$parent_stack[] = $item['parent_id']; |
|
223
|
|
|
$parent = $item['id']; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
// HTML for menu item with no children (aka "leaf") |
|
227
|
|
|
else { |
|
228
|
|
|
$html[] = sprintf( |
|
229
|
|
|
'%1$s'.'<li>%2$s - <a'.'%3$s'.' href="'.'%4$s'.'">%5$s</a> – pozycja: %6$s'. |
|
230
|
|
|
' <a href="/admin/appearance/menu/edit/%2$s" class="btn btn-primary btn-xs">Edytuj</a>'. |
|
231
|
|
|
' <a href="/admin/appearance/menu/del/%2$s" class="btn btn-danger btn-xs">Edytuj</a>', |
|
232
|
|
|
|
|
233
|
|
|
# %1$s tabulation |
|
234
|
|
|
str_repeat("\t", (count($parent_stack) + 1) * 2 - 1 + $nesting), |
|
235
|
|
|
|
|
236
|
|
|
$item['id'], |
|
237
|
|
|
|
|
238
|
|
|
# %2$s a title="" |
|
239
|
|
|
$this->isAttribute('title', $item['caption']), |
|
240
|
|
|
|
|
241
|
|
|
# %3$s a href="" |
|
242
|
|
|
$item['slug'], |
|
243
|
|
|
|
|
244
|
|
|
# %4$s text inside item |
|
245
|
|
|
$item['title'], |
|
246
|
|
|
|
|
247
|
|
|
$item['position'] |
|
248
|
|
|
); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
// HTML wrapper for the menu (close) |
|
253
|
|
|
$html[] = str_repeat("\t", $nesting).'</ul>'; |
|
254
|
|
|
|
|
255
|
|
|
return implode("\n", array_filter($html))."\n"; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: