1
|
|
|
<?php namespace EvolutionCMS\Support; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* menu->Build('id','parent','name','link','alt','onclick','permission','target','divider 1/0','menuindex') |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
use EvolutionCMS\Interfaces\MenuInterface; |
8
|
|
|
|
9
|
|
|
class Menu implements MenuInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
public $defaults = array(); |
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
public $menu; |
19
|
|
|
/** |
20
|
|
|
* @var |
21
|
|
|
*/ |
22
|
|
|
public $output; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $menu |
26
|
|
|
* @param array $setting |
27
|
|
|
*/ |
28
|
|
|
public function build($menu, $setting = array()) |
29
|
|
|
{ |
30
|
|
|
$this->defaults['outerClass'] = 'nav'; |
31
|
|
|
$this->defaults['parentClass'] = 'dropdown'; |
32
|
|
|
$this->defaults['parentLinkClass'] = 'dropdown-toggle'; |
33
|
|
|
$this->defaults['parentLinkAttr'] = 'data-toggle="dropdown"'; |
34
|
|
|
$this->defaults['parentLinkIn'] = '<b class="caret"></b>'; |
35
|
|
|
$this->defaults['innerClass'] = 'subnav'; |
36
|
|
|
|
37
|
|
|
$this->defaults = $setting + $this->defaults; |
38
|
|
|
$this->structurise($menu); |
39
|
|
|
$this->output = $this->drawSub('main', 0); |
40
|
|
|
echo $this->output; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $menu |
45
|
|
|
*/ |
46
|
|
|
public function structurise($menu) |
47
|
|
|
{ |
48
|
|
|
$new = array(); |
49
|
|
|
foreach ($menu as $key => $row) { |
50
|
|
|
$data[$key] = $row[9]; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
array_multisort($data, SORT_ASC, $menu); |
54
|
|
|
|
55
|
|
|
foreach ($menu as $key => $value) { |
56
|
|
|
$new[$value[1]][] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->menu = $new; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param int $parentid |
64
|
|
|
* @param int $level |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function drawSub($parentid, $level) |
68
|
|
|
{ |
69
|
|
|
$modx = evolutionCMS(); |
70
|
|
|
|
71
|
|
|
$output = ''; |
72
|
|
|
|
73
|
|
|
if (isset($this->menu[$parentid])) { |
74
|
|
|
|
75
|
|
|
$ph = array(); |
|
|
|
|
76
|
|
|
$countChild = 0; |
77
|
|
|
$itemTpl = ' |
78
|
|
|
<li id="[+id+]" class="[+li_class+]"><a href="[+href+]" alt="[+alt+]" target="[+target+]" onclick="[+onclick+]"[+a_class+] [+LinkAttr+]>[+itemName+]</a>[+DrawSub+]</li>'; |
79
|
|
|
$outerTpl = '<ul id="[+id+]" class="[+class+]">[+output+]</ul>'; |
80
|
|
|
foreach ($this->menu[$parentid] as $key => $value) { |
81
|
|
|
if ($value[6] !== '') { |
82
|
|
|
$permissions = explode(',', $value[6]); |
83
|
|
|
foreach ($permissions as $val) { |
84
|
|
|
if (!$modx->hasPermission($val)) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$countChild++; |
91
|
|
|
$id = $value[0]; |
92
|
|
|
$ph['id'] = $id; |
93
|
|
|
$ph['li_class'] = $this->getItemClass($id) . $value[10]; |
94
|
|
|
$ph['href'] = $value[3]; |
95
|
|
|
$ph['alt'] = $value[4]; |
96
|
|
|
$ph['target'] = $value[7]; |
97
|
|
|
$ph['onclick'] = $value[5]; |
98
|
|
|
$ph['a_class'] = $this->getLinkClass($id); |
99
|
|
|
$ph['LinkAttr'] = $this->getLinkAttr($id); |
100
|
|
|
$ph['itemName'] = $value[2] . $this->getItemName($id); |
101
|
|
|
|
102
|
|
|
$ph['DrawSub'] = ''; |
103
|
|
|
|
104
|
|
|
if (isset($this->menu[$id])) { |
105
|
|
|
$level++; |
106
|
|
|
$ph['DrawSub'] = $this->drawSub($id, $level); |
107
|
|
|
$level--; |
108
|
|
|
// Optional buttons |
109
|
|
|
} else { |
110
|
|
|
if (isset($value[11]) && !empty($value[11])) { |
111
|
|
|
$optionalButton = ''; |
112
|
|
|
if (is_array($value[11])) { |
113
|
|
|
foreach ($value[11] as $opt) { |
114
|
|
|
$optionalButton .= sprintf('<%s href="%s" class="%s" onclick="%s" title="%s">%s</%s>', |
115
|
|
|
$opt[0], $opt[1], $opt[2], $opt[3], $opt[4], $opt[5], $opt[0]); |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
$opt = $value[11]; |
119
|
|
|
$optionalButton = sprintf('<%s href="%s" class="%s" onclick="%s" title="%s">%s</%s>', |
120
|
|
|
$opt[0], $opt[1], $opt[2], $opt[3], $opt[4], $opt[5], $opt[0]); |
121
|
|
|
} |
122
|
|
|
$ph['DrawSub'] = $optionalButton; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$output .= $modx->parseText($itemTpl, $ph); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$ph = array(); |
130
|
|
|
if ($countChild > 0) { |
131
|
|
|
$ph['id'] = $level == 0 ? $this->defaults['outerClass'] : ''; |
132
|
|
|
$ph['class'] = $level == 0 ? $this->defaults['outerClass'] : $this->defaults['innerClass']; |
133
|
|
|
$ph['output'] = $output; |
134
|
|
|
$output = $modx->parseText($outerTpl, $ph); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $output; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param int $id |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getLinkClass($id) |
146
|
|
|
{ |
147
|
|
|
if (isset($this->menu[$id])) { |
148
|
|
|
return ' class="' . $this->defaults['parentLinkClass'] . '"'; |
149
|
|
|
} else { |
150
|
|
|
return ''; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param int $id |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getLinkAttr($id) |
159
|
|
|
{ |
160
|
|
|
if (isset($this->menu[$id])) { |
161
|
|
|
return $this->defaults['parentLinkAttr']; |
162
|
|
|
} else { |
163
|
|
|
return ''; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param int $id |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getItemClass($id) |
172
|
|
|
{ |
173
|
|
|
if (isset($this->menu[$id])) { |
174
|
|
|
return $this->defaults['parentClass'] . ' '; |
175
|
|
|
} else { |
176
|
|
|
return ''; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param int $id |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getItemName($id) |
185
|
|
|
{ |
186
|
|
|
if (isset($this->menu[$id])) { |
187
|
|
|
return $this->defaults['parentLinkIn']; |
188
|
|
|
} else { |
189
|
|
|
return ''; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.