-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDashboardPanelAddNew.module
executable file
·166 lines (141 loc) · 4.33 KB
/
DashboardPanelAddNew.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
<?php namespace Daun\Dashboard;
use ProcessWire\AdminThemeFramework;
use ProcessWire\InputfieldSubmit;
use ProcessWire\ProcessPageAdd;
use function ProcessWire\__;
use function ProcessWire\wireIconMarkup;
// Include abstract panel base class
require_once dirname(__DIR__).'/Dashboard/DashboardPanel.class.php';
class DashboardPanelAddNew extends DashboardPanel
{
public static function getModuleInfo()
{
return array_merge(
parent::getModuleInfo(),
[
'title' => __('Dashboard Panel: Add New Page', __FILE__),
'summary' => __('Allow adding new pages from the dashboard', __FILE__),
'author' => 'Philipp Daun',
'version' => '1.5.8',
]
);
}
const displayOptions = [
'list',
'dropdown',
];
const defaultDisplayOption = 'list';
public function getIcon()
{
return 'plus';
}
public function getTitle()
{
if ($this->doesRenderTitle()) {
return $this->getTitleString();
} else {
return '';
}
}
protected function doesRenderTitle()
{
if ($this->display === 'dropdown') {
return false;
} else {
return true;
}
}
protected function getTitleString()
{
/** @var AdminThemeFramework $theme */
$theme = $this->wire('adminTheme');
$label = $theme->getAddNewLabel();
return $label ?? $this->_('Add New');
}
public function getClassNames()
{
return ["{$this}--{$this->display}"];
}
public function getStyleOptions()
{
if (!$this->doesRenderTitle()) {
return [
'minimal' => true,
'padding' => false,
];
} else {
return [];
}
}
public function getContent()
{
$actions = $this->getAvailablePageActions();
if (!count($actions)) {
return '';
}
switch ($this->display) {
case 'dropdown':
return $this->renderActionDropdown($actions);
break;
case 'list':
default:
return $this->renderActionList($actions);
break;
}
}
protected function renderActionList($actions)
{
$links = array_map(function ($item) {
$icon = wireIconMarkup($item['icon'], 'fw');
$url = $item['url'];
$label = $item['label'];
return
"<li>
<a href='{$url}'>
{$icon}
<span class='title'>{$label}</span>
</a>
</li>"
;
}, $actions);
$output = implode('', $links);
$output = "<ul>{$output}</ul>";
return $output;
}
protected function renderActionDropdown($actions)
{
/** @var InputfieldSubmit $button */
$button = $this->modules->get('InputfieldSubmit');
$button->html = $this->getTitleString();
$button->setSecondary();
foreach ($actions as $item) {
$button->addActionLink($item['url'], $item['label'], $item['icon']);
}
return "<div class='DashboardPanelAddNewDropdown'>{$button->render()}</div>";
}
protected function getAvailablePageActions()
{
try {
/** @var ProcessPageAdd $module */
$module = $this->modules->getModule('ProcessPageAdd', ['noInit' => true]);
$data = $module->executeNavJSON(['getArray' => true]);
$actions = [];
foreach($data['list'] as $item) {
if (strpos($item['url'], 'bookmarks/') === 0) continue;
$item['url'] = $data['url'] . $item['url'];
$item['icon'] = $item['icon'] ?? $data['icon'];
$actions[] = $item;
}
} catch (\Throwable $th) {
$actions = [];
}
return $actions;
}
public function setup()
{
parent::setup();
$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;
}
}