|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Exceptions\BackpackProRequiredException; |
|
6
|
|
|
use Backpack\CRUD\ViewNamespaces; |
|
7
|
|
|
use Illuminate\Support\Fluent; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Adds fluent syntax to Backpack Widgets. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Widget extends Fluent |
|
13
|
|
|
{ |
|
14
|
|
|
protected $attributes = []; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct($attributes) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->attributes = $attributes; |
|
19
|
|
|
|
|
20
|
|
|
$this->save(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Add a new widget to the widgets collection in the Laravel Service Container. |
|
25
|
|
|
* If a widget with the same name exists, it will update the attributes of that one |
|
26
|
|
|
* instead of creating a new one. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string|array $attributes Either the name of the widget, or an array with the attributes the new widget should hold, including the name attribute. |
|
29
|
|
|
* @return Widget |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function add($attributes = null) |
|
32
|
|
|
{ |
|
33
|
|
|
// make sure the widget has a name |
|
34
|
|
|
$attributes = is_string($attributes) ? ['name' => $attributes] : $attributes; |
|
35
|
|
|
$attributes['name'] = $attributes['name'] ?? 'widget_'.rand(1, 999999999); |
|
36
|
|
|
|
|
37
|
|
|
// if that widget name already exists in the widgets collection |
|
38
|
|
|
// then pick up all widget attributes from that entry |
|
39
|
|
|
// and overwrite them with the ones passed in $attributes |
|
40
|
|
|
if ($existingItem = self::collection()->filter(function ($item) use ($attributes) { |
|
41
|
|
|
return $item->attributes['name'] === $attributes['name']; |
|
42
|
|
|
})->first()) { |
|
43
|
|
|
$attributes = array_merge($existingItem->attributes, $attributes); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// set defaults for other mandatory attributes |
|
47
|
|
|
$attributes['section'] = $attributes['section'] ?? 'before_content'; |
|
48
|
|
|
$attributes['type'] = $attributes['type'] ?? 'card'; |
|
49
|
|
|
|
|
50
|
|
|
return new static($attributes); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Return the widget attribute value or null when it doesn't exist. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $attribute |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getAttribute(string $attribute) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->attributes[$attribute] ?? null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Check if widget has the attribute. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $attribute |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function hasAttribute(string $attribute) |
|
71
|
|
|
{ |
|
72
|
|
|
return array_key_exists($attribute, $this->attributes); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* This method allows one to creat a widget without attaching it to any 'real' |
|
77
|
|
|
* widget section, by moving it to a 'hidden' section. |
|
78
|
|
|
* |
|
79
|
|
|
* It exists for one reason: so that developers can add widgets to a custom array, without |
|
80
|
|
|
* adding them to one of the widget sections. |
|
81
|
|
|
* |
|
82
|
|
|
* Ex: when developers need to pass multiple widgets as contents of the |
|
83
|
|
|
* div widget. But they don't want them added to the before_content of after_content |
|
84
|
|
|
* sections. So what they do is basically add them to a 'hidden' section, that nobody will ever see. |
|
85
|
|
|
* |
|
86
|
|
|
* @return Widget |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function make($attributes = null) |
|
89
|
|
|
{ |
|
90
|
|
|
$widget = static::add($attributes); |
|
91
|
|
|
$widget->section('hidden'); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
return $widget; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Remove an attribute from the current definition array. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $attribute Name of the attribute to forget (ex: class) |
|
100
|
|
|
* @return Widget |
|
101
|
|
|
*/ |
|
102
|
|
|
public function forget($attribute) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->offsetUnset($attribute); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// TODO: add ability to push a widget right after another widget |
|
110
|
|
|
public function after($destination) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// TODO: add ability to push a widget right before another widget |
|
115
|
|
|
public function before($destionation) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Make this widget the first one in its section. |
|
121
|
|
|
* |
|
122
|
|
|
* @return Widget |
|
123
|
|
|
*/ |
|
124
|
|
|
public function makeFirst() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->collection()->pull($this->attributes['name']); |
|
127
|
|
|
$this->collection()->prepend($this); |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Make this widget the last one in its section. |
|
134
|
|
|
* |
|
135
|
|
|
* @return Widget |
|
136
|
|
|
*/ |
|
137
|
|
|
public function makeLast() |
|
138
|
|
|
{ |
|
139
|
|
|
$this->collection()->pull($this->attributes['name']); |
|
140
|
|
|
$this->collection()->push($this); |
|
141
|
|
|
|
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get an array of full paths to the widget view, consisting of: |
|
147
|
|
|
* - the path given in the widget definition |
|
148
|
|
|
* - fallback view paths as configured in backpack/config/base.php. |
|
149
|
|
|
* |
|
150
|
|
|
* @return array |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getFinalViewPath() |
|
153
|
|
|
{ |
|
154
|
|
|
if (isset($this->attributes['viewNamespace'])) { |
|
155
|
|
|
$path = $this->attributes['viewNamespace'].'.'.$this->attributes['type']; |
|
156
|
|
|
|
|
157
|
|
|
if (view()->exists($path)) { |
|
158
|
|
|
return $path; |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
$type = $this->attributes['type']; |
|
162
|
|
|
$paths = array_map(function ($item) use ($type) { |
|
163
|
|
|
return $item.'.'.$type; |
|
164
|
|
|
}, ViewNamespaces::getWithFallbackFor('widgets', 'backpack.ui.component_view_namespaces.widgets')); |
|
165
|
|
|
|
|
166
|
|
|
foreach ($paths as $path) { |
|
167
|
|
|
if (view()->exists($path)) { |
|
168
|
|
|
return $path; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
// if no view exists, in any of the directories above... no bueno |
|
172
|
|
|
if (! backpack_pro()) { |
|
173
|
|
|
throw new BackpackProRequiredException('Cannot find the widget view: '.$this->attributes['type'].'. Please check for typos.'.(backpack_pro() ? '' : ' If you are trying to use a PRO widget, please first purchase and install the backpack/pro addon from backpackforlaravel.com'), 1); |
|
174
|
|
|
} |
|
175
|
|
|
abort(500, 'Cannot find the view for «'.$this->attributes['type'].'» widget type. Please check for typos.'); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
// ------- |
|
179
|
|
|
// ALIASES |
|
180
|
|
|
// ------- |
|
181
|
|
|
// Aka convenience methods. |
|
182
|
|
|
// These method just call other methods. |
|
183
|
|
|
|
|
184
|
|
|
// Alias of add() |
|
185
|
|
|
public static function name(...$args) |
|
186
|
|
|
{ |
|
187
|
|
|
return static::add(...$args); |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
// Alias of section() |
|
191
|
|
|
public function to(...$args) |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->section(...$args); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
// Alias of section() |
|
197
|
|
|
public function group(...$args) |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->section(...$args); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
// Alias of viewNamespace() |
|
203
|
|
|
public function from(...$args) |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->viewNamespace(...$args); |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
// ------------------ |
|
209
|
|
|
// COLLECTION METHODS |
|
210
|
|
|
// ------------------ |
|
211
|
|
|
// Manipulate the global widget collection. |
|
212
|
|
|
|
|
213
|
|
|
public static function collection() |
|
214
|
|
|
{ |
|
215
|
|
|
return app('widgets'); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Remove the widget from its section. |
|
220
|
|
|
* |
|
221
|
|
|
* @return Widget |
|
222
|
|
|
*/ |
|
223
|
|
|
public function remove() |
|
224
|
|
|
{ |
|
225
|
|
|
$this->collection()->pull($this->attributes['name']); |
|
226
|
|
|
|
|
227
|
|
|
return $this; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* This alias of remove() exists for one reason: so that developers can add |
|
232
|
|
|
* widgets to a custom array, instead of adding them to one of the widget |
|
233
|
|
|
* sections. Ex: when developers need to pass multiple widgets as contents of the |
|
234
|
|
|
* div widget. But they don't want them added to the before_content of after_content |
|
235
|
|
|
* sections. So what they do is basically add them to a section, then remove them. |
|
236
|
|
|
* What's left is the widget itself, but without being attached to any section. |
|
237
|
|
|
* |
|
238
|
|
|
* @return Widget |
|
239
|
|
|
*/ |
|
240
|
|
|
public function onlyHere(...$args) |
|
241
|
|
|
{ |
|
242
|
|
|
return $this->remove(...$args); |
|
|
|
|
|
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
// --------------- |
|
246
|
|
|
// PRIVATE METHODS |
|
247
|
|
|
// --------------- |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Update the global CrudPanel object with the current widget attributes. |
|
251
|
|
|
* |
|
252
|
|
|
* @return Widget |
|
253
|
|
|
*/ |
|
254
|
|
|
private function save() |
|
255
|
|
|
{ |
|
256
|
|
|
$itemExists = $this->collection()->filter(function ($item) { |
|
257
|
|
|
return $item->attributes['name'] === $this->attributes['name']; |
|
258
|
|
|
})->isNotEmpty(); |
|
259
|
|
|
if (! $itemExists) { |
|
260
|
|
|
$this->collection()->put($this->attributes['name'], $this); |
|
261
|
|
|
} else { |
|
262
|
|
|
$this->collection()[$this->attributes['name']] = $this; |
|
263
|
|
|
} |
|
264
|
|
|
return $this; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
// ----------------- |
|
268
|
|
|
// DEBUGGING METHODS |
|
269
|
|
|
// ----------------- |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Dump the current object to the screen, |
|
273
|
|
|
* so that the developer can see its contents. |
|
274
|
|
|
* |
|
275
|
|
|
* @return Widget |
|
276
|
|
|
*/ |
|
277
|
|
|
public function dump() |
|
278
|
|
|
{ |
|
279
|
|
|
dump($this); |
|
280
|
|
|
|
|
281
|
|
|
return $this; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Dump and die. Duumps the current object to the screen, |
|
286
|
|
|
* so that the developer can see its contents, then stops |
|
287
|
|
|
* the execution. |
|
288
|
|
|
* |
|
289
|
|
|
* @return Widget |
|
290
|
|
|
*/ |
|
291
|
|
|
public function dd() |
|
292
|
|
|
{ |
|
293
|
|
|
dd($this); |
|
294
|
|
|
|
|
295
|
|
|
return $this; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Overwritten methods to prevent BC in Laravel 11, since they introduced the `value()` method |
|
300
|
|
|
* in their Fluent class. Altough the Widget class is Fluent, it does not behave the same |
|
301
|
|
|
* in regards to `value()`, since we use it as a key in widget definition. |
|
302
|
|
|
*/ |
|
303
|
|
|
public function value($value, $default = null) |
|
|
|
|
|
|
304
|
|
|
{ |
|
305
|
|
|
$this->attributes['value'] = $value; |
|
306
|
|
|
return $this->save(); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
#[\ReturnTypeWillChange] |
|
310
|
|
|
public function offsetGet($offset): mixed |
|
311
|
|
|
{ |
|
312
|
|
|
return $this->get($offset); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
public function __get($key) |
|
316
|
|
|
{ |
|
317
|
|
|
return $this->get($key); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
// ------------- |
|
321
|
|
|
// MAGIC METHODS |
|
322
|
|
|
// ------------- |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Any call to a non-existing method on this class will be assumed to be |
|
326
|
|
|
* an attribute that the developer wants to add to that particular widget. |
|
327
|
|
|
* |
|
328
|
|
|
* Eg: class('something') will set the "class" attribute to "something" |
|
329
|
|
|
* |
|
330
|
|
|
* @param string $method The method being called that doesn't exist. |
|
331
|
|
|
* @param array $parameters The arguments when that method was called. |
|
332
|
|
|
* @return Widget |
|
333
|
|
|
*/ |
|
334
|
|
|
public function __call($method, $parameters) |
|
335
|
|
|
{ |
|
336
|
|
|
$this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true; |
|
337
|
|
|
return $this->save(); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
|
|
341
|
|
|
} |
|
342
|
|
|
|