1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Fluent; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Adds fluent syntax to Backpack Widgets. |
9
|
|
|
*/ |
10
|
|
|
class Widget extends Fluent |
11
|
|
|
{ |
12
|
|
|
protected $attributes = []; |
13
|
|
|
|
14
|
|
|
public function __construct($attributes) |
15
|
|
|
{ |
16
|
|
|
$this->attributes = $attributes; |
17
|
|
|
|
18
|
|
|
$this->save(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Add a new widget to the widgets collection in the Laravel Service Container. |
23
|
|
|
* If a widget with the same name exists, it will update the attributes of that one |
24
|
|
|
* instead of creating a new one. |
25
|
|
|
* |
26
|
|
|
* @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. |
27
|
|
|
* |
28
|
|
|
* @return Widget |
29
|
|
|
*/ |
30
|
|
|
public static function add($attributes = null) |
31
|
|
|
{ |
32
|
|
|
// make sure the widget has a name |
33
|
|
|
$attributes = is_string($attributes) ? ['name' => $attributes] : $attributes; |
34
|
|
|
$attributes['name'] = $attributes['name'] ?? 'widget_'.rand(1, 999999999); |
35
|
|
|
|
36
|
|
|
// if that widget name already exists in the widgets collection |
37
|
|
|
// then pick up all widget attributes from that entry |
38
|
|
|
// and overwrite them with the ones passed in $attributes |
39
|
|
|
if ($existingItem = self::collection()->firstWhere('name', $attributes['name'])) { |
40
|
|
|
$attributes = array_merge($existingItem->attributes, $attributes); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// set defaults for other mandatory attributes |
44
|
|
|
$attributes['section'] = $attributes['section'] ?? 'before_content'; |
45
|
|
|
$attributes['type'] = $attributes['type'] ?? 'card'; |
46
|
|
|
|
47
|
|
|
return new static($attributes); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* This method allows one to creat a widget without attaching it to any 'real' |
52
|
|
|
* widget section, by moving it to a 'hidden' section. |
53
|
|
|
* |
54
|
|
|
* It exists for one reason: so that developers can add widgets to a custom array, without |
55
|
|
|
* adding them to one of the widget sections. |
56
|
|
|
* |
57
|
|
|
* Ex: when developers need to pass multiple widgets as contents of the |
58
|
|
|
* div widget. But they don't want them added to the before_content of after_content |
59
|
|
|
* sections. So what they do is basically add them to a 'hidden' section, that nobody will ever see. |
60
|
|
|
* |
61
|
|
|
* @return Widget |
62
|
|
|
*/ |
63
|
|
|
public static function make($attributes = null) |
64
|
|
|
{ |
65
|
|
|
$widget = static::add($attributes); |
66
|
|
|
$widget->section('hidden'); |
|
|
|
|
67
|
|
|
|
68
|
|
|
return $widget; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Remove an attribute from the current definition array. |
73
|
|
|
* |
74
|
|
|
* @param string $attribute Name of the attribute to forget (ex: class) |
75
|
|
|
* @return Widget |
76
|
|
|
*/ |
77
|
|
|
public function forget($attribute) |
78
|
|
|
{ |
79
|
|
|
$this->offsetUnset($attribute); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// TODO: add ability to push a widget right after another widget |
85
|
|
|
public function after($destination) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// TODO: add ability to push a widget right before another widget |
90
|
|
|
public function before($destionation) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Make this widget the first one in its section. |
96
|
|
|
* |
97
|
|
|
* @return Widget |
98
|
|
|
*/ |
99
|
|
|
public function makeFirst() |
100
|
|
|
{ |
101
|
|
|
$this->collection()->pull($this->name); |
|
|
|
|
102
|
|
|
$this->collection()->prepend($this); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Make this widget the last one in its section. |
109
|
|
|
* |
110
|
|
|
* @return Widget |
111
|
|
|
*/ |
112
|
|
|
public function makeLast() |
113
|
|
|
{ |
114
|
|
|
$this->collection()->pull($this->name); |
|
|
|
|
115
|
|
|
$this->collection()->push($this); |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// ------- |
121
|
|
|
// ALIASES |
122
|
|
|
// ------- |
123
|
|
|
// Aka convenience methods. |
124
|
|
|
// These method just call other methods. |
125
|
|
|
|
126
|
|
|
// Alias of add() |
127
|
|
|
public static function name(...$args) |
128
|
|
|
{ |
129
|
|
|
return static::add(...$args); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Alias of section() |
133
|
|
|
public function to(...$args) |
134
|
|
|
{ |
135
|
|
|
return $this->section(...$args); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// Alias of section() |
139
|
|
|
public function group(...$args) |
140
|
|
|
{ |
141
|
|
|
return $this->section(...$args); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Alias of viewNamespace() |
145
|
|
|
public function from(...$args) |
146
|
|
|
{ |
147
|
|
|
return $this->viewNamespace(...$args); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// ------------------ |
151
|
|
|
// COLLECTION METHODS |
152
|
|
|
// ------------------ |
153
|
|
|
// Manipulate the global widget collection. |
154
|
|
|
|
155
|
|
|
public static function collection() |
156
|
|
|
{ |
157
|
|
|
return app('widgets'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Remove the widget from its section. |
162
|
|
|
* |
163
|
|
|
* @return Widget |
164
|
|
|
*/ |
165
|
|
|
public function remove() |
166
|
|
|
{ |
167
|
|
|
$this->collection()->pull($this->name); |
|
|
|
|
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* This alias of remove() exists for one reason: so that developers can add |
174
|
|
|
* widgets to a custom array, instead of adding them to one of the widget |
175
|
|
|
* sections. Ex: when developers need to pass multiple widgets as contents of the |
176
|
|
|
* div widget. But they don't want them added to the before_content of after_content |
177
|
|
|
* sections. So what they do is basically add them to a section, then remove them. |
178
|
|
|
* What's left is the widget itself, but without being attached to any section. |
179
|
|
|
* |
180
|
|
|
* @return Widget |
181
|
|
|
*/ |
182
|
|
|
public function onlyHere(...$args) |
183
|
|
|
{ |
184
|
|
|
return $this->remove(...$args); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// --------------- |
188
|
|
|
// PRIVATE METHODS |
189
|
|
|
// --------------- |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Update the global CrudPanel object with the current widget attributes. |
193
|
|
|
* |
194
|
|
|
* @return Widget |
195
|
|
|
*/ |
196
|
|
|
private function save() |
197
|
|
|
{ |
198
|
|
|
$itemExists = $this->collection()->contains('name', $this->attributes['name']); |
199
|
|
|
|
200
|
|
|
if (! $itemExists) { |
201
|
|
|
$this->collection()->put($this->attributes['name'], $this); |
202
|
|
|
} else { |
203
|
|
|
$this->collection()[$this->name] = $this; |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// ----------------- |
210
|
|
|
// DEBUGGING METHODS |
211
|
|
|
// ----------------- |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Dump the current object to the screen, |
215
|
|
|
* so that the developer can see its contents. |
216
|
|
|
* |
217
|
|
|
* @return Widget |
218
|
|
|
*/ |
219
|
|
|
public function dump() |
220
|
|
|
{ |
221
|
|
|
dump($this); |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Dump and die. Duumps the current object to the screen, |
228
|
|
|
* so that the developer can see its contents, then stops |
229
|
|
|
* the execution. |
230
|
|
|
* |
231
|
|
|
* @return Widget |
232
|
|
|
*/ |
233
|
|
|
public function dd() |
234
|
|
|
{ |
235
|
|
|
dd($this); |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// ------------- |
241
|
|
|
// MAGIC METHODS |
242
|
|
|
// ------------- |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Any call to a non-existing method on this class will be assumed to be |
246
|
|
|
* an attribute that the developer wants to add to that particular widget. |
247
|
|
|
* |
248
|
|
|
* Eg: class('something') will set the "class" attribute to "something" |
249
|
|
|
* |
250
|
|
|
* @param string $method The method being called that doesn't exist. |
251
|
|
|
* @param array $parameters The arguments when that method was called. |
252
|
|
|
* |
253
|
|
|
* @return Widget |
254
|
|
|
*/ |
255
|
|
|
public function __call($method, $parameters) |
256
|
|
|
{ |
257
|
|
|
$this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true; |
258
|
|
|
|
259
|
|
|
return $this->save(); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|