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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Menu;
use Piwik\Container\StaticContainer;
use Piwik\Plugins\SitesManager\API;
use Piwik\Singleton;
use Piwik\Plugin\Manager as PluginManager;
/**
* Base class for classes that manage one of Piwik's menus.
*
* There are three menus in Piwik, the main menu, the top menu and the admin menu.
* Each menu has a class that manages the menu's content. Each class invokes
* a different event to allow plugins to add new menu items.
*
* @static \Piwik\Menu\MenuAbstract getInstance()
*/
abstract class MenuAbstract extends Singleton
{
protected $menu = null;
protected $menuEntries = array();
protected $menuEntriesToRemove = array();
protected $edits = array();
protected $renames = array();
protected $orderingApplied = false;
protected $menuIcons = array();
protected static $menus = array();
/**
* Builds the menu, applies edits, renames
* and orders the entries.
*
* @return Array
*/
public function getMenu()
{
$this->buildMenu();
$this->applyEdits();
$this->applyRemoves();
$this->applyRenames();
$this->applyOrdering();
return $this->menu;
}
/**
* lets you register a menu icon for a certain menu category to replace the default arrow icon.
*
* @param string $menuName The translation key of a main menu category, eg 'Dashboard_Dashboard'
* @param string $iconCssClass The css class name of an icon, eg 'icon-user'
*/
public function registerMenuIcon($menuName, $iconCssClass)
{
$this->menuIcons[$menuName] = $iconCssClass;
}
/**
* Returns a list of available plugin menu instances.
*
* @return \Piwik\Plugin\Menu[]
*/
protected function getAllMenus()
{
if (!empty(self::$menus)) {
return self::$menus;
}
$components = PluginManager::getInstance()->findComponents('Menu', 'Piwik\\Plugin\\Menu');
self::$menus = array();
foreach ($components as $component) {
self::$menus[] = StaticContainer::get($component);
}
return self::$menus;
}
/**
* To use only for tests.
*
* @deprecated The whole $menus cache should be replaced by a real transient cache
*/
public static function clearMenus()
{
self::$menus = array();
}
/**
* Adds a new entry to the menu.
*
* @param string $menuName The menu's category name. Can be a translation token.
* @param string $subMenuName The menu item's name. Can be a translation token.
* @param string|array $url The URL the admin menu entry should link to, or an array of query parameters
* that can be used to build the URL.
* @param int $order The order hint.
* @param bool|string $tooltip An optional tooltip to display or false to display the tooltip.
* @since 2.7.0
* @api
*/
public function addItem($menuName, $subMenuName, $url, $order = 50, $tooltip = false)
{
// make sure the idSite value used is numeric (hack-y fix for #3426)
if (isset($url['idSite']) && !is_numeric($url['idSite'])) {
$idSites = API::getInstance()->getSitesIdWithAtLeastViewAccess();
$url['idSite'] = reset($idSites);
}
$this->menuEntries[] = array(
$menuName,
$subMenuName,
$url,
$order,
$tooltip
);
}
/**
* Removes an existing entry from the menu.
*
* @param string $menuName The menu's category name. Can be a translation token.
* @param bool|string $subMenuName The menu item's name. Can be a translation token.
* @api
*/
public function remove($menuName, $subMenuName = false)
{
$this->menuEntriesToRemove[] = array(
$menuName,
$subMenuName
);
}
/**
* Builds a single menu item
*
* @param string $menuName
* @param string $subMenuName
* @param string $url
* @param int $order
* @param bool|string $tooltip Tooltip to display.
*/
private function buildMenuItem($menuName, $subMenuName, $url, $order = 50, $tooltip = false)
{
if (!isset($this->menu[$menuName])) {
$this->menu[$menuName] = array(
'_hasSubmenu' => false,
'_order' => $order
);
}
if (empty($subMenuName)) {
$this->menu[$menuName]['_url'] = $url;
$this->menu[$menuName]['_order'] = $order;
$this->menu[$menuName]['_name'] = $menuName;
$this->menu[$menuName]['_tooltip'] = $tooltip;
if (!empty($this->menuIcons[$menuName])) {
$this->menu[$menuName]['_icon'] = $this->menuIcons[$menuName];
} else {
$this->menu[$menuName]['_icon'] = '';
}
}
if (!empty($subMenuName)) {
$this->menu[$menuName][$subMenuName]['_url'] = $url;
$this->menu[$menuName][$subMenuName]['_order'] = $order;
$this->menu[$menuName][$subMenuName]['_name'] = $subMenuName;
$this->menu[$menuName][$subMenuName]['_tooltip'] = $tooltip;
$this->menu[$menuName]['_hasSubmenu'] = true;
if (!array_key_exists('_tooltip', $this->menu[$menuName])) {
$this->menu[$menuName]['_tooltip'] = $tooltip;
}
}
}
/**
* Builds the menu from the $this->menuEntries variable.
*/
private function buildMenu()
{
foreach ($this->menuEntries as $menuEntry) {
$this->buildMenuItem($menuEntry[0], $menuEntry[1], $menuEntry[2], $menuEntry[3], $menuEntry[4]);
}
}
/**
* Renames a single menu entry.
*
* @param $mainMenuOriginal
* @param $subMenuOriginal
* @param $mainMenuRenamed
* @param $subMenuRenamed
* @api
*/
public function rename($mainMenuOriginal, $subMenuOriginal, $mainMenuRenamed, $subMenuRenamed)
{
$this->renames[] = array($mainMenuOriginal, $subMenuOriginal,
$mainMenuRenamed, $subMenuRenamed);
}
/**
* Edits a URL of an existing menu entry.
*
* @param $mainMenuToEdit
* @param $subMenuToEdit
* @param $newUrl
* @api
*/
public function editUrl($mainMenuToEdit, $subMenuToEdit, $newUrl)
{
$this->edits[] = array($mainMenuToEdit, $subMenuToEdit, $newUrl);
}
/**
* Applies all edits to the menu.
*/
private function applyEdits()
{
foreach ($this->edits as $edit) {
$mainMenuToEdit = $edit[0];
$subMenuToEdit = $edit[1];
$newUrl = $edit[2];
if ($subMenuToEdit === null) {
if (isset($this->menu[$mainMenuToEdit])) {
$menuDataToEdit = &$this->menu[$mainMenuToEdit];
} else {
$menuDataToEdit = null;
}
} else {
if (isset($this->menu[$mainMenuToEdit][$subMenuToEdit])) {
$menuDataToEdit = &$this->menu[$mainMenuToEdit][$subMenuToEdit];
} else {
$menuDataToEdit = null;
}
}
if (empty($menuDataToEdit)) {
$this->buildMenuItem($mainMenuToEdit, $subMenuToEdit, $newUrl);
} else {
$menuDataToEdit['_url'] = $newUrl;
}
}
}
private function applyRemoves()
{
foreach ($this->menuEntriesToRemove as $menuToDelete) {
if (empty($menuToDelete[1])) {
// Delete Main Menu
if (isset($this->menu[$menuToDelete[0]])) {
unset($this->menu[$menuToDelete[0]]);
}
} else {
// Delete Sub Menu
if (isset($this->menu[$menuToDelete[0]][$menuToDelete[1]])) {
unset($this->menu[$menuToDelete[0]][$menuToDelete[1]]);
}
}
}
}
/**
* Applies renames to the menu.
*/
private function applyRenames()
{
foreach ($this->renames as $rename) {
$mainMenuOriginal = $rename[0];
$subMenuOriginal = $rename[1];
$mainMenuRenamed = $rename[2];
$subMenuRenamed = $rename[3];
// Are we changing a submenu?
if (!empty($subMenuOriginal)) {
if (isset($this->menu[$mainMenuOriginal][$subMenuOriginal])) {
$save = $this->menu[$mainMenuOriginal][$subMenuOriginal];
$save['_name'] = $subMenuRenamed;
unset($this->menu[$mainMenuOriginal][$subMenuOriginal]);
$this->menu[$mainMenuRenamed][$subMenuRenamed] = $save;
}
} // Changing a first-level element
elseif (isset($this->menu[$mainMenuOriginal])) {
$save = $this->menu[$mainMenuOriginal];
$save['_name'] = $mainMenuRenamed;
unset($this->menu[$mainMenuOriginal]);
$this->menu[$mainMenuRenamed] = $save;
}
}
}
/**
* Orders the menu according to their order.
*/
private function applyOrdering()
{
if (empty($this->menu)
|| $this->orderingApplied
) {
return;
}
uasort($this->menu, array($this, 'menuCompare'));
foreach ($this->menu as $key => &$element) {
if (is_null($element)) {
unset($this->menu[$key]);
} elseif ($element['_hasSubmenu']) {
uasort($element, array($this, 'menuCompare'));
}
}
$this->orderingApplied = true;
}
/**
* Compares two menu entries. Used for ordering.
*
* @param array $itemOne
* @param array $itemTwo
* @return boolean
*/
protected function menuCompare($itemOne, $itemTwo)
{
if (!is_array($itemOne) && !is_array($itemTwo)) {
return 0;
}
if (!is_array($itemOne) && is_array($itemTwo)) {
return -1;
}
if (is_array($itemOne) && !is_array($itemTwo)) {
return 1;
}
if (!isset($itemOne['_order']) && !isset($itemTwo['_order'])) {
return 0;
}
if (!isset($itemOne['_order']) && isset($itemTwo['_order'])) {
return -1;
}
if (isset($itemOne['_order']) && !isset($itemTwo['_order'])) {
return 1;
}
if ($itemOne['_order'] == $itemTwo['_order']) {
return strcmp(
@$itemOne['_name'],
@$itemTwo['_name']);
}
return ($itemOne['_order'] < $itemTwo['_order']) ? -1 : 1;
}
}
|