-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDashboardPanelShortcuts.module
executable file
·249 lines (217 loc) · 7.49 KB
/
DashboardPanelShortcuts.module
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
<?php namespace Daun\Dashboard;
use ProcessWire\AdminThemeFramework;
use ProcessWire\Page;
use ProcessWire\Selectors;
use function ProcessWire\__;
use function ProcessWire\wireIconMarkup;
// Include abstract panel base class
require_once dirname(__DIR__).'/Dashboard/DashboardPanel.class.php';
class DashboardPanelShortcuts extends DashboardPanel
{
public static function getModuleInfo()
{
return array_merge(
parent::getModuleInfo(),
[
'title' => __('Dashboard Panel: Shortcuts', __FILE__),
'summary' => __('Display a list of shortcuts to admin pages', __FILE__),
'author' => 'Philipp Daun',
'version' => '1.5.8',
]
);
}
const displayOptions = [
'grid',
'list',
];
const defaultDisplayOption = 'grid';
public function getIcon()
{
return 'flash';
}
public function getTitle()
{
return $this->_('Shortcuts');
}
public function getClassNames()
{
return ["{$this}--{$this->display}"];
}
public function getContent()
{
if (!($this->shortcuts && count($this->shortcuts))) {
return;
}
$links = array_map(function ($shortcut, $key) {
$info = $this->getShortcutInfo($shortcut, $key);
$link = $this->renderShortcutLink($info);
return $link ? "<li>{$link}</li>" : "";
}, $this->shortcuts, array_keys($this->shortcuts));
$output = implode('', $links);
return "<ul data-display='{$this->display}'>{$output}</ul>";
}
protected function getShortcutInfo($shortcut, $key = null)
{
$page = null;
$title = null;
$summary = null;
$url = null;
$anchor = null;
$icon = null;
$mode = null;
if (is_array($shortcut) && is_string(array_key_first($shortcut))) {
// Verbose array with keys passed
// ['page' => 1020, 'icon' => 'user', 'summary' => 'Edit your profile']
$page = $shortcut['page'] ?? null;
$title = $shortcut['title'] ?? null;
$summary = $shortcut['summary'] ?? null;
$url = $shortcut['url'] ?? null;
$anchor = $shortcut['anchor'] ?? null;
$mode = $shortcut['mode'] ?? null;
$icon = $shortcut['icon'] ?? null;
} elseif (is_array($shortcut)) {
// Simple numerical array passed: Extract shortcut, icon and summary
// [1020, 'user', 'Edit your profile']
$summary = $shortcut[2] ?? null;
$icon = $shortcut[1] ?? null;
$page = $shortcut[0] ?? null;
} else {
// Single page passed
// 1020
$page = $shortcut;
}
// Get URL if page/id/selector given,
// otherwise treat as URL string
if (is_object($page) && $page instanceof Page) {
$page = $page;
} elseif (is_int($page)) {
$page = $this->pages->get($page);
} elseif (is_string($page)) {
if (Selectors::stringHasSelector($page)) {
$page = $this->pages->get($page);
} else {
$url = $url ?? $page;
}
}
if (is_object($page) && $this->isPageViewable($page)) {
$info = $this->getPageInfo($page);
$title = $title ?? (is_string($key) ? $key : $info->title);
$summary = $summary ?? $info->summary;
$icon = $icon ?? $info->icon;
$url = $page->url;
} elseif ($url) {
$title = $title ?? (is_string($key) ? $key : $url);
} else {
return;
}
return [
'url' => $url,
'anchor' => $anchor,
'mode' => $mode,
'title' => $title,
'summary' => $summary,
'icon' => $icon,
];
}
protected function renderShortcutLink($link)
{
if (!$link || !($link['url'] ?? null)) {
return '';
}
$url = $link['url'] ?? null;
$isAdminLink = strpos($url ?: '', $this->config->urls->admin) === 0;
$title = $this->sanitizer->entities1($link['title'] ?? $link['url'] ?? '');
$summary = $this->summaries ? $this->sanitizer->entities1($link['summary'] ?? '') : '';
$icon = $this->renderPageIcon($link['icon'] ?? null);
$mode = $link['mode'] ?? (!$isAdminLink ? self::windowModeBlank : '');
$anchor = $link['anchor'] ?? '';
$classes = [];
if ($summary && $this->display === 'grid') {
$classes[] = 'tooltip';
}
$target = '';
if ($mode === self::windowModeBlank) {
$target = '_blank';
} elseif ($mode === self::windowModeModal) {
$classes[] = 'pw-modal';
$classes[] = 'pw-modal-large';
}
$class = implode(' ', $classes);
return
"<a href='{$url}{$anchor}' class='{$class}' title='{$summary}' target='{$target}'>
<span>
{$icon}
<span class='title'>{$title}</span>
</span>
<span>
<span class='summary' title='{$summary}'>{$summary}</span>
</span>
</a>";
}
protected function renderPageIcon($icon)
{
if ($this->icon !== null) {
$icon = $this->icon;
}
$icon = $icon ?: $this->fallbackIcon;
return $icon ? wireIconMarkup($icon, 'fw') : '';
}
protected function getPageInfo($page)
{
$adminTheme = $this->wire('adminTheme');
$hasInfoMethods = ($adminTheme && $adminTheme instanceof AdminThemeFramework);
$title = '';
$icon = '';
$summary = '';
if ($page->template == 'admin') {
if ($page->process) {
$info = $this->modules->getModuleInfoVerbose($page->process);
$summary = $page->summary ?: ($info['summary'] ?? '');
$icon = $info['icon'] ?? '';
}
$icon = $page->getIcon() ?: $icon;
$title = $hasInfoMethods ? $adminTheme->getPageTitle($page) : '';
if (!$icon) {
switch ($page->id) {
case 22: $icon = 'gears'; break; // Setup
case 21: $icon = 'plug'; break; // Modules
case 28: $icon = 'key'; break; // Access
}
}
}
if (!$title) {
$title = $page->get('title|name');
}
if (!$icon) {
$icon = $page->getIcon();
}
return (object) compact(
'title',
'summary',
'icon'
);
}
protected function isPageViewable($page)
{
if (!($page->url ?? null)) {
return false;
}
if (!$this->checkAccess) {
return true;
}
if ($page->template == 'admin' && !$page->viewable()) {
return false;
}
return true;
}
public function setup()
{
parent::setup();
$this->shortcuts = $this->data['shortcuts'] ?? [];
$this->display = $this->sanitizer->option($this->data['display'] ?? '', self::displayOptions) ?: self::defaultDisplayOption;
$this->fallbackIcon = $this->data['fallbackIcon'] ?? 'bookmark-o';
$this->icon = $this->data['icon'] ?? null;
$this->summaries = $this->data['summaries'] ?? true;
$this->checkAccess = $this->data['checkAccess'] ?? true;
}
}