@@ -52,76 +52,124 @@ class DashboardPanelShortcuts extends DashboardPanel
5252 return;
5353 }
5454
55- $rows = array_map(function ($shortcut, $key) {
56- $page = null;
57- $pageIcon = '';
58- $title = '';
59- $summary = '';
60- $url = '';
61- $icon = '';
62-
63- // Passed array? Extract to shortcut, icon and summary
64- // e.g. [1020, 'user', 'Edit your profile']
65- if (is_array($shortcut)) {
66- // Avoiding list() construct since indices can be undefined
67- $summary = $shortcut[2] ?? null;
68- $icon = $shortcut[1] ?? null;
69- $shortcut = $shortcut[0] ?? null;
70- }
55+ $links = array_map(function ($shortcut, $key) {
56+ $info = $this->getShortcutInfo($shortcut, $key);
57+ $link = $this->renderShortcutLink($info);
58+ return $link ? "<li>{$link}</li>" : "";
59+ }, $this->shortcuts, array_keys($this->shortcuts));
7160
72- // Get URL from shortcut if page/id/selector given,
73- // otherwise treat shortcut as URL string
74- if (is_object($shortcut) && $shortcut instanceof Page) {
75- $page = $shortcut;
76- } elseif (is_int($shortcut)) {
77- $page = $this->pages->get($shortcut);
78- } elseif (is_string($shortcut)) {
79- if (Selectors::stringHasSelector($shortcut)) {
80- $page = $this->pages->get($shortcut);
81- } else {
82- $url = $shortcut;
83- }
84- }
61+ $output = implode('', $links);
62+
63+ return "<ul data-display='{$this->display}'>{$output}</ul>";
64+ }
8565
86- if ($page) {
87- if (!$this->isPageViewable($page)) {
88- return;
89- }
90- $info = $this->getPageInfo($page);
91- $title = is_string($key) ? $key : $info->title;
92- $summary = $summary ?: $info->summary;
93- $pageIcon = $info->icon;
94- $url = $page->url;
95- } elseif ($url) {
96- $title = $key;
66+ protected function getShortcutInfo($shortcut, $key = null)
67+ {
68+ $page = null;
69+ $title = null;
70+ $summary = null;
71+ $url = null;
72+ $anchor = null;
73+ $icon = null;
74+ $mode = null;
75+
76+ if (is_array($shortcut) && is_string(array_key_first($shortcut))) {
77+ // Verbose array with keys passed
78+ // ['page' => 1020, 'icon' => 'user', 'summary' => 'Edit your profile']
79+ $page = $shortcut['page'] ?? null;
80+ $title = $shortcut['title'] ?? null;
81+ $summary = $shortcut['summary'] ?? null;
82+ $url = $shortcut['url'] ?? null;
83+ $anchor = $shortcut['anchor'] ?? null;
84+ $mode = $shortcut['mode'] ?? null;
85+ $icon = $shortcut['icon'] ?? null;
86+ } elseif (is_array($shortcut)) {
87+ // Simple numerical array passed: Extract shortcut, icon and summary
88+ // [1020, 'user', 'Edit your profile']
89+ $summary = $shortcut[2] ?? null;
90+ $icon = $shortcut[1] ?? null;
91+ $page = $shortcut[0] ?? null;
92+ } else {
93+ // Single page passed
94+ // 1020
95+ $page = $shortcut;
96+ }
97+
98+ // Get URL if page/id/selector given,
99+ // otherwise treat as URL string
100+ if (is_object($page) && $page instanceof Page) {
101+ $page = $page;
102+ } elseif (is_int($page)) {
103+ $page = $this->pages->get($page);
104+ } elseif (is_string($page)) {
105+ if (Selectors::stringHasSelector($page)) {
106+ $page = $this->pages->get($page);
97107 } else {
98- return ;
108+ $url = $url ?? $page ;
99109 }
110+ }
100111
101- $icon = $this->renderPageIcon($icon ?: $pageIcon);
102- $title = $this->sanitizer->entities1($title);
103- $summary = $this->summaries ? $this->sanitizer->entities1($summary) : '';
104- $linkClass = $summary && $this->display === 'grid' ? 'tooltip' : '';
105- $isAdminLink = strpos($url, $this->config->urls->admin) === 0;
106- $target = !$isAdminLink ? '_blank' : '';
107-
108- return
109- "<li>
110- <a href='{$url}' class='{$linkClass}' title='{$summary}' target='{$target}'>
111- <span>
112- {$icon}
113- <span class='title'>{$title}</span>
114- </span>
115- <span>
116- <span class='summary' title='{$summary}'>{$summary}</span>
117- </span>
118- </a>
119- </li>";
120- }, $this->shortcuts, array_keys($this->shortcuts));
112+ if ($page && $this->isPageViewable($page)) {
113+ $info = $this->getPageInfo($page);
114+ $title = $title ?? (is_string($key) ? $key : $info->title);
115+ $summary = $summary ?? $info->summary;
116+ $icon = $icon ?? $info->icon;
117+ $url = $page->url;
118+ } elseif ($url) {
119+ $title = $title ?? (is_string($key) ? $key : $url);
120+ } else {
121+ return;
122+ }
121123
122- $output = implode('', $rows);
124+ return [
125+ 'url' => $url,
126+ 'anchor' => $anchor,
127+ 'mode' => $mode,
128+ 'title' => $title,
129+ 'summary' => $summary,
130+ 'icon' => $icon,
131+ ];
132+ }
123133
124- return "<ul data-display='{$this->display}'>{$output}</ul>";
134+ protected function renderShortcutLink($link)
135+ {
136+ if (!$link || !($link['url'] ?? null)) {
137+ return '';
138+ }
139+
140+ $url = $link['url'] ?? null;
141+ $isAdminLink = strpos($url, $this->config->urls->admin) === 0;
142+ $title = $this->sanitizer->entities1($link['title'] ?? $link['url'] ?? '');
143+ $summary = $this->summaries ? $this->sanitizer->entities1($link['summary'] ?? '') : '';
144+ $icon = $this->renderPageIcon($link['icon'] ?? null);
145+ $mode = $link['mode'] ?? (!$isAdminLink ? self::windowModeBlank : '');
146+ $anchor = $link['anchor'] ?? '';
147+
148+ $classes = [];
149+ if ($summary && $this->display === 'grid') {
150+ $classes[] = 'tooltip';
151+ }
152+
153+ $target = '';
154+ if ($mode === self::windowModeBlank) {
155+ $target = '_blank';
156+ } elseif ($mode === self::windowModeModal) {
157+ $classes[] = 'pw-modal';
158+ $classes[] = 'pw-modal-large';
159+ }
160+
161+ $class = implode(' ', $classes);
162+
163+ return
164+ "<a href='{$url}{$anchor}' class='{$class}' title='{$summary}' target='{$target}'>
165+ <span>
166+ {$icon}
167+ <span class='title'>{$title}</span>
168+ </span>
169+ <span>
170+ <span class='summary' title='{$summary}'>{$summary}</span>
171+ </span>
172+ </a>";
125173 }
126174
127175 protected function renderPageIcon($icon)
0 commit comments