-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathQuickMenu.php
309 lines (291 loc) · 9.52 KB
/
QuickMenu.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
<?php
/**
* Class Quick Menu
* @author David Ticona Saravia <[email protected]>
* @version 1.0.1 (04/2020)
*/
class QuickMenu
{
private $dropdownIcon = '';
private $activeClass = 'active';
protected $activeItem = '';
private $arrAttr = array();
private $arrData = array();
private $result = array();
public function __construct($options = array())
{
if (isset($options['data']))
{
$this->setData($options['data']);
}
$this->dropdownIcon = isset($options['dropdownIcon']) ? $options['dropdownIcon'] : $this->dropdownIcon;
$this->activeClass = isset($options['active-class']) ? $options['active-class'] : $this->activeClass;
}
/**
* Set the attributes for the tag vars
* @param string $name Var name
* @param mixed $value Var value
* Var names: 'ul', 'ul-root', 'li', 'li-parent', 'a', 'a-parent', 'active-class'
*/
public function set($name, $value)
{
$tags = array('ul', 'ul-root', 'li', 'li-parent', 'a', 'a-parent');
if (in_array($name, $tags))
{
$this->arrAttr[$name] = $value;
}
/* legacy */
if ($name=='active-class')
{
$this->activeClass = $value;
}
}
/**
* Set dropdown icon for display with submenus
* @param string $content Content for the dropdown icon (Html code)
*/
public function setDropdownIcon($content)
{
$this->dropdownIcon = $content;
}
/**
* @param string $href The active href
* @param string $activeClass (Optional) The Css class for the active item
*/
public function setActiveItem($href, $activeClass = '')
{
$this->activeItem = $href;
if ($activeClass != '')
{
$this->activeClass = $activeClass;
$this->set('active-class', $this->activeClass); /* legacy */
}
}
/**
* @param mixed $data Data (Json string or associative array)
*/
public function setData($data)
{
if (is_string($data))
{
$this->arrData = json_decode($data, TRUE);
} elseif (is_array($data))
{
$this->arrData = $data;
}
}
/**
* Insert an item
* @param array $item
* @param string $before_at (Optional) The reference position for insert. The 'text' attribute
* @param string $parent (Optional) The node parent if the insert is in a submenu. The 'text' attribute
*/
public function insert($item, $before_at = '', $parent = '')
{
if ($before_at === '' && $parent === '')
{
$this->arrData[] = $item;
return;
}
if ($parent === '')
{
$pos = array_search($before_at, array_column($this->arrData, 'text'));
if ($pos !== FALSE)
{
array_splice($this->arrData, $pos, 0, array($item));
return;
}
$this->arrData[] = $item;
} else
{
$pos_parent = array_search($parent, array_column($this->arrData, 'text'));
if ($pos_parent === FALSE)
{
$this->a8rrData[] = $item;
return;
}
$pos = array_search($before_at, array_column($this->arrData[$pos_parent]['children'], 'text'));
if ($pos !== FALSE)
{
array_splice($this->arrData[$pos_parent]['children'], $pos, 0, array($item));
return;
}
$this->arrData[$pos_parent]['children'][] = $item;
}
}
/**
* Replace an item (find by text attribute)
* @param array $newItem The new item
* @param string $text The text item for search
*/
public function replace(array $newItem, $text)
{
$pos = array_search($text, array_column($this->arrData, 'text'));
if ($pos===FALSE)
{
return FALSE;
}
$this->arrData[$pos] = $newItem;
return TRUE;
}
/**
* Remove an item (from top level) by text attribute
* @param string $text Text item
*/
public function remove($text)
{
$pos = array_search($text, array_column($this->arrData, 'text'));
if ($pos===FALSE)
{
return FALSE;
}
array_splice($this->arrData, $pos, 1);
return TRUE;
}
/**
* Get an menu item (from top level)
* @param string $text Text menu to find
* @return mixed Array with the item. Else not found, return NULL
*/
public function getItem($text)
{
$pos = array_search($text, array_column($this->arrData, 'text'));
return ($pos!==FALSE) ? $this->arrData[$pos] : NULL;
}
/**
* The Html menu
* @return string Html menu
*/
public function html()
{
if (!empty($this->result))
{
return $this->buildFromResult($this->result);
}
return $this->build($this->arrData);
}
/**
* Build the menu from query database.
* This is a alias (shorthand) for setResult() and html()
* @param array $result The resultset
* @param string $columnID The ID column name (Primary key)
* @param string $columnParent The column name for identify the parent item
*/
public function fromResult($result, $columnID, $columnParent)
{
$this->setResult($result, $columnID, $columnParent);
return $this->buildFromResult($this->result);
}
/**
* Set result from query database
* @param array $result The resultset
* @param string $columnID The ID column name (Primary key)
* @param string $columnParent The column name for identify the parent item
*/
public function setResult($result, $columnID, $columnParent)
{
$items = array();
foreach ($result as $row)
{
$target = (isset($row->target)) ? $row->target : '_self';
$icon = (isset($row->icon)) ? $row->icon : '';
$items[$row->$columnParent][$row->$columnID] = array('id' => $row->id, 'href' => $row->href, 'text' => $row->text, 'icon' => $icon, 'target' => $target);
}
$this->result = $items;
}
/**
* @param string $tag
* @param string $extra Add css class
* @return string Tag Attributes stored
*/
protected function getAttr($tag, $extra = '')
{
return $this->buildAttributes($tag, $extra);
}
/**
* @param array $item The Item menu
* @param boolean $isParent This item is parent?
* @return string The Html code
*/
protected function getTextItem($item, $isParent)
{
$str = (isset($item['icon'])) ? "<i class=\"{$item['icon']}\"></i> " : '';
$str .= ($isParent) ? "{$item['text']} {$this->dropdownIcon}" : $item['text'];
return $str;
}
/**
* Renderize the tag attributes from array
* @param string $tag The tag
* @param string $extra append a css class
* @return string The string atributes
*/
private function buildAttributes($tag, $extra = '')
{
$str = '';
if (isset($this->arrAttr[$tag]))
{
foreach ($this->arrAttr[$tag] as $name => $value)
{
if (($extra!='')&&($name=='class'))
{
$value = "{$value} {$extra}";
}
$str .= " {$name}=\"{$value}\"";
}
}
return $str;
}
/**
* Build the menu
* @param array $array
* @param int $depth (Optional)
* @return string The Html code
*/
protected function build($array, $depth = 0)
{
$str = ($depth === 0) ? '<ul' . $this->getAttr('ul-root') . '>' : '<ul' . $this->getAttr('ul') . '>';
foreach ($array as $item)
{
$isParent = isset($item['children']);
$li = ($isParent) ? 'li-parent' : 'li';
$a = ($isParent) ? 'a-parent' : 'a';
$activeClass = ($this->activeItem == $item['href']) ? $this->activeClass : '';
$str .= '<li' . $this->getAttr($li, $activeClass) . ">";
$str .= '<a href="' . $item['href'] . '" title="' . $item['title'] . '"' . $this->getAttr($a) . '>' . $this->getTextItem($item, $isParent) . '</a>';
if ($isParent)
{
$str .= $this->build($item['children'], 1);
}
$str .= '</li>';
}
$str .= '</ul>';
return $str;
}
/**
* Build the menu from a prepared array of Query Result
* @param array $array Array de items
* @param int $parent Parent ID
* @param int $level Nivel del item
*/
protected function buildFromResult(array $array, $parent = 0, $level = 0)
{
$ul = ($parent === 0) ? 'ul-root' : 'ul';
$str = '<ul' . $this->getAttr($ul) . '>';
foreach ($array[$parent] as $item_id => $item)
{
$isParent = isset($array[$item_id]);
$li = ($isParent) ? 'li-parent' : 'li';
$a = ($isParent) ? 'a-parent' : 'a';
$activeClass = ($this->activeItem == $item['href']) ? $this->activeClass : '';
$str .= '<li' . $this->getAttr($li, $activeClass) . '>';
$str .= "<a href=\"{$item['href']}\" target=\"{$item['target']}\"" . $this->getAttr($a) . '>' . $this->getTextItem($item, $isParent) . '</a>';
if ($isParent)
{
$str .= $this->buildFromResult($array, $item_id, $level + 2);
}
$str .= '</li>';
}
$str .= '</ul>';
return $str;
}
}