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
|
<?php
declare(strict_types=1);
class Dashboard
{
private static $iconCache = array();
private static $subMenu = array();
private static $disabled = false;
public static function disable(): void
{
self::$disabled = true;
}
public static function createMenu(): void
{
if (self::$disabled)
return;
global $MENU_CAT_OVERRIDE;
$modByCategory = array();
$modById = array();
if (isset($MENU_CAT_OVERRIDE)) {
foreach ($MENU_CAT_OVERRIDE as $cat => $list) {
foreach ($list as $mod) {
$modByCategory[$cat][$mod] = null;
$modById[$mod] =& $modByCategory[$cat][$mod];
}
}
}
$all = Module::getEnabled(true);
foreach ($all as $module) {
$cat = $module->getCategory();
if (empty($cat))
continue;
$modId = $module->getIdentifier();
if (array_key_exists($modId, $modById)) {
$modById[$modId] = $module;
} else {
$modByCategory[$cat][$modId] = $module;
}
}
$currentPage = Page::getModule()->getIdentifier();
$categories = array();
foreach ($modByCategory as $catId => $modList) {
$collapse = true;
/* @var (?Module)[] $modList */
$modules = array();
foreach ($modList as $module) {
if ($module === null)
continue; // Was set in $MENU_CAT_OVERRIDE, but is not enabled
$newEntry = array(
'displayName' => $module->getDisplayName(),
'identifier' => $module->getIdentifier()
);
if ($module->getIdentifier() === $currentPage) {
$newEntry['className'] = 'active';
$newEntry['subMenu'] = self::$subMenu;
$collapse = false; // Don't collapse category if it contains the active module
}
if (!$module->doCollapse()) {
$collapse = false;
}
$modules[] = $newEntry;
}
$categories[] = [
'icon' => self::getCategoryIcon($catId),
'displayName' => Dictionary::getCategoryName($catId),
'modules' => $modules,
'collapse' => $collapse,
];
}
Render::setDashboard([
'categories' => $categories,
'url' => urlencode($_SERVER['REQUEST_URI']),
'langs' => Dictionary::getLanguages(true),
'user' => User::getName(),
'warning' => User::getName() !== false && User::hasPermission('.eventlog.*') && User::getLastSeenEvent() < Property::getLastWarningId(),
'needsSetup' => User::getName() !== false && Property::getNeedsSetup()
]);
}
public static function getCategoryIcon(string $category): string
{
if (!preg_match('/^(\w+)\.(.*)$/', $category, $out)) {
error_log('Requested category icon for invalid category "' . $category . '"');
return '';
}
$module = $out[1];
$icon = $out[2];
if (!isset(self::$iconCache[$module])) {
$path = 'modules/' . $module . '/category-icons.json';
$data = json_decode(file_get_contents($path), true);
if (!is_array($data)) {
return '';
}
self::$iconCache[$module] =& $data;
}
if (!isset(self::$iconCache[$module][$icon])) {
error_log('Icon "' . $icon . '" not found in module "' . $module . '"');
return '';
}
return 'glyphicon glyphicon-' . self::$iconCache[$module][$icon];
}
public static function addSubmenu(string $url, string $name): void
{
self::$subMenu[] = array('url' => $url, 'name' => $name);
}
public static function getSubmenus(): array
{
return self::$subMenu;
}
}
|