-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDashboardPanelInstance.class.php
266 lines (228 loc) · 5.8 KB
/
DashboardPanelInstance.class.php
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
<?php namespace Daun\Dashboard;
use ProcessWire\WireArray;
use ProcessWire\WireData;
if (!function_exists('array_key_first')) {
function array_key_first(array $arr)
{
foreach ($arr as $key => $unused) {
return $key;
}
return null;
}
}
/**
* Dashboard panel instance.
*
* Wire-derived object that holds
* the configuration of a single panel instance
*/
class DashboardPanelInstance extends WireData
{
}
/**
* Dashboard panel group.
*
* Wire-derived object that holds
* the configuration of a group of panel instances
*/
class DashboardPanelGroup extends DashboardPanelInstance
{
public function add($item)
{
return $this->panels->add($item);
}
public function import($items)
{
return $this->panels->import($items);
}
public function insertBefore($item, $existingItem)
{
return $this->panels->insertBefore($item, $existingItem);
}
public function insertAfter($item, $existingItem)
{
return $this->panels->insertAfter($item, $existingItem);
}
public function remove($item)
{
return $this->panels->remove($item);
}
public function flatten()
{
return $this->panels->flatten();
}
}
/**
* Dashboard panel tab.
*
* Wire-derived object that holds
* the configuration of a tab of panel instances
*/
class DashboardPanelTab extends DashboardPanelGroup
{
}
/**
* Dashboard panel array class.
*
* WireArray that holds a collection of panel instances
*/
class DashboardPanelArray extends WireArray
{
protected $duplicateChecking = false;
protected $frozen = false;
public function freeze()
{
$this->frozen = true;
}
public function frozen()
{
return $this->frozen;
}
public function makeBlankItem()
{
return null;
}
public function isValidItem($item)
{
return $item instanceof DashboardPanelInstance;
}
/**
* Flatten the panels in this array to include panels in any
* panel groups.
*
* @return DashboardPanelArray
*/
public function flatten()
{
$flattened = new self();
/* Recursively flatten nested groups */
foreach ($this->getAll() as $item) {
if ($item instanceof DashboardPanelGroup) {
$flattened->import($item->flatten());
} elseif ($item instanceof DashboardPanelInstance) {
$flattened->add($item);
}
}
return $flattened;
}
public function add($config)
{
// Account for array of configs: all panel configs have associative
// keys, so numerical keys indicate an array of configs
if (is_array($config) && is_int(array_key_first($config))) {
return $this->import($config);
} else {
return parent::add($this->createInstance($config));
}
}
public function set($key, $value)
{
return parent::set($key, $this->createInstance($value));
}
public function prepend($item)
{
return parent::prepend($this->createInstance($item));
}
public function unshift($item)
{
return parent::unshift($this->createInstance($item));
}
public function insertBefore($item, $existingItem)
{
return parent::insertBefore($this->createInstance($item), $existingItem);
}
public function insertAfter($item, $existingItem)
{
return parent::insertAfter($this->createInstance($item), $existingItem);
}
public function import($items)
{
$instances = [];
foreach ($items as $key => $item) {
$instances[$key] = $this->createInstance($item);
}
return parent::import($instances);
}
/**
* Create a panel and return it.
*
* @param array $config
*
* @return DashboardPanelGroup
*/
public function createPanel(array $config)
{
if (!is_array($config)) {
return;
}
if (!($config['panel'] ?? false)) {
throw new \Exception('Missing required `panel` parameter');
}
$config['type'] = 'panel';
if ($config['data'] ?? false) {
$config['dataArray'] = $config['data'];
unset($config['data']);
}
$instance = new DashboardPanelInstance();
$instance->setArray($config);
return $instance;
}
/**
* Create a group of panels and return it.
*
* @param array $config
*
* @return DashboardPanelGroup
*/
public function createGroup(array $config)
{
if (!is_array($config)) {
return;
}
$config['type'] = 'group';
$config['panels'] = new self();
$group = new DashboardPanelGroup();
$group->setArray($config);
return $group;
}
/**
* Create a tab and return it.
*
* @param array $config
*
* @return DashboardPanelTab
*/
public function createTab(array $config)
{
if (!is_array($config)) {
return;
}
$config['type'] = 'tab';
$config['panels'] = new self();
$tab = new DashboardPanelTab();
$tab->setArray($config);
return $tab;
}
/**
* Create an instance of the DashboardPanelInstance class.
*
* @param array $config
*
* @return DashboardPanelInstance
*/
public function createInstance($config)
{
if (!is_array($config)) {
return $config;
}
switch ($config['type'] ?? null) {
case 'tab':
return $this->createTab($config);
case 'group':
return $this->createGroup($config);
case 'panel':
default:
return $this->createPanel($config);
}
}
}