1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Traits\Support\MacroableWithAttributes; |
6
|
|
|
use Illuminate\Support\Traits\Conditionable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Adds fluent syntax to Backpack CRUD Columns. |
10
|
|
|
* |
11
|
|
|
* In addition to the existing: |
12
|
|
|
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']); |
13
|
|
|
* |
14
|
|
|
* Developers can also do: |
15
|
|
|
* - CRUD::column('price')->type('number'); |
16
|
|
|
* |
17
|
|
|
* And if the developer uses CrudColumn as Column in their CrudController: |
18
|
|
|
* - Column::name('price')->type('number'); |
19
|
|
|
* |
20
|
|
|
* @method self type(string $value) |
21
|
|
|
* @method self label(string $value) |
22
|
|
|
* @method self searchLogic(mixed $value) |
23
|
|
|
* @method self orderLogic(callable $value) |
24
|
|
|
* @method self orderable(bool $value) |
25
|
|
|
* @method self wrapper(array $value) |
26
|
|
|
* @method self visibleInTable(bool $value) |
27
|
|
|
* @method self visibleInModal(bool $value) |
28
|
|
|
* @method self visibleInExport(bool $value) |
29
|
|
|
* @method self visibleInShow(bool $value) |
30
|
|
|
* @method self priority(int $value) |
31
|
|
|
* @method self key(string $value) |
32
|
|
|
* @method self upload(bool $value) |
33
|
|
|
* @method self linkTo(string $routeName, ?array $parameters = []) |
34
|
|
|
*/ |
35
|
|
|
class CrudColumn |
36
|
|
|
{ |
37
|
|
|
use Conditionable; |
38
|
|
|
use MacroableWithAttributes; |
|
|
|
|
39
|
|
|
|
40
|
|
|
protected $attributes; |
41
|
|
|
|
42
|
|
|
public function __construct($nameOrDefinitionArray) |
43
|
|
|
{ |
44
|
|
|
if (is_array($nameOrDefinitionArray)) { |
45
|
|
|
$column = $this->crud()->addAndReturnColumn($nameOrDefinitionArray); |
46
|
|
|
$name = $column->getAttributes()['name']; |
47
|
|
|
} else { |
48
|
|
|
$name = $nameOrDefinitionArray; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$column = $this->crud()->firstColumnWhere('name', $name); |
52
|
|
|
|
53
|
|
|
// if column exists |
54
|
|
|
if ((bool) $column) { |
55
|
|
|
// use all existing attributes |
56
|
|
|
$this->setAllAttributeValues($column); |
|
|
|
|
57
|
|
|
} else { |
58
|
|
|
// it means we're creating the column now, |
59
|
|
|
// so at the very least set the name attribute |
60
|
|
|
$this->setAttributeValue('name', $name); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->save(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function crud() |
67
|
|
|
{ |
68
|
|
|
return app()->make('crud'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create a CrudColumn object with the parameter as its name. |
73
|
|
|
* |
74
|
|
|
* @param string $name Name of the column in the db, or model attribute. |
75
|
|
|
* @return CrudColumn |
76
|
|
|
*/ |
77
|
|
|
public static function name($name) |
78
|
|
|
{ |
79
|
|
|
return new static($name); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Change the CrudColumn key. |
84
|
|
|
* |
85
|
|
|
* @param string $key New key for the column |
86
|
|
|
* @return CrudColumn |
87
|
|
|
*/ |
88
|
|
|
public function key(string $key) |
89
|
|
|
{ |
90
|
|
|
if (! isset($this->attributes['name'])) { |
91
|
|
|
abort(500, 'Column name must be defined before changing the key.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$columns = $this->crud()->columns(); |
95
|
|
|
|
96
|
|
|
$searchKey = $this->attributes['key']; |
97
|
|
|
$column = $this->attributes; |
98
|
|
|
|
99
|
|
|
if (isset($columns[$searchKey])) { |
100
|
|
|
unset($columns[$searchKey]); |
101
|
|
|
$column['key'] = $key; |
102
|
|
|
} |
103
|
|
|
$this->attributes = $column; |
104
|
|
|
$this->crud()->setOperationSetting('columns', array_merge($columns, [$key => $column])); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Remove the current column from the current operation. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function remove() |
115
|
|
|
{ |
116
|
|
|
$this->crud()->removeColumn($this->attributes['name']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Remove an attribute from the column definition array. |
121
|
|
|
* |
122
|
|
|
* @param string $attribute Name of the attribute being removed |
123
|
|
|
* @return CrudColumn |
124
|
|
|
*/ |
125
|
|
|
public function forget($attribute) |
126
|
|
|
{ |
127
|
|
|
$this->crud()->removeColumnAttribute($this->attributes['name'], $attribute); |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Move the current column after another column. |
134
|
|
|
* |
135
|
|
|
* @param string $destinationColumn Name of the destination column. |
136
|
|
|
* @return CrudColumn |
137
|
|
|
*/ |
138
|
|
|
public function after($destinationColumn) |
139
|
|
|
{ |
140
|
|
|
$this->crud()->removeColumn($this->attributes['name']); |
141
|
|
|
$this->crud()->addColumn($this->attributes)->afterColumn($destinationColumn); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Move the current column before another column. |
148
|
|
|
* |
149
|
|
|
* @param string $destinationColumn Name of the destination column. |
150
|
|
|
* @return CrudColumn |
151
|
|
|
*/ |
152
|
|
|
public function before($destinationColumn) |
153
|
|
|
{ |
154
|
|
|
$this->crud()->removeColumn($this->attributes['name']); |
155
|
|
|
$this->crud()->addColumn($this->attributes)->beforeColumn($destinationColumn); |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function upload($upload = true) |
161
|
|
|
{ |
162
|
|
|
$this->attributes['upload'] = $upload; |
163
|
|
|
|
164
|
|
|
return $this->save(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Make the current column the first one in the columns list. |
169
|
|
|
* |
170
|
|
|
* @return CrudColumn |
171
|
|
|
*/ |
172
|
|
|
public function makeFirst() |
173
|
|
|
{ |
174
|
|
|
$this->crud()->removeColumn($this->attributes['name']); |
175
|
|
|
$this->crud()->addColumn($this->attributes)->makeFirstColumn(); |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Make the current column the last one in the columns list. |
182
|
|
|
* |
183
|
|
|
* @return CrudColumn |
184
|
|
|
*/ |
185
|
|
|
public function makeLast() |
186
|
|
|
{ |
187
|
|
|
$this->crud()->removeColumn($this->attributes['name']); |
188
|
|
|
$this->crud()->addColumn($this->attributes); |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// ----------------- |
194
|
|
|
// DEBUGGING METHODS |
195
|
|
|
// ----------------- |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Dump the current object to the screen, |
199
|
|
|
* so that the developer can see its contents. |
200
|
|
|
* |
201
|
|
|
* @codeCoverageIgnore |
202
|
|
|
* |
203
|
|
|
* @return CrudColumn |
204
|
|
|
*/ |
205
|
|
|
public function dump() |
206
|
|
|
{ |
207
|
|
|
dump($this); |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Dump and die. Dumps the current object to the screen, |
214
|
|
|
* so that the developer can see its contents, then stops |
215
|
|
|
* the execution. |
216
|
|
|
* |
217
|
|
|
* @codeCoverageIgnore |
218
|
|
|
* |
219
|
|
|
* @return CrudColumn |
220
|
|
|
*/ |
221
|
|
|
public function dd() |
222
|
|
|
{ |
223
|
|
|
dd($this); |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function getAttributes() |
229
|
|
|
{ |
230
|
|
|
return $this->attributes; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// --------------- |
234
|
|
|
// PRIVATE METHODS |
235
|
|
|
// --------------- |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Set the value for a certain attribute on the CrudColumn object. |
239
|
|
|
* |
240
|
|
|
* @param string $attribute Name of the attribute. |
241
|
|
|
* @param mixed $value Value of that attribute. |
242
|
|
|
*/ |
243
|
|
|
private function setAttributeValue($attribute, $value) |
244
|
|
|
{ |
245
|
|
|
$this->attributes[$attribute] = $value; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Replace all column attributes on the CrudColumn object |
250
|
|
|
* with the given array of attribute-value pairs. |
251
|
|
|
* |
252
|
|
|
* @param array $array Array of attributes and their values. |
253
|
|
|
*/ |
254
|
|
|
private function setAllAttributeValues($array) |
255
|
|
|
{ |
256
|
|
|
$this->attributes = $array; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Update the global CrudPanel object with the current column attributes. |
261
|
|
|
* |
262
|
|
|
* @return CrudColumn |
263
|
|
|
*/ |
264
|
|
|
private function save() |
265
|
|
|
{ |
266
|
|
|
$key = $this->attributes['key'] ?? $this->attributes['name']; |
267
|
|
|
|
268
|
|
|
if ($this->crud()->hasColumnWhere('key', $key)) { |
269
|
|
|
$this->crud()->setColumnDetails($key, $this->attributes); |
270
|
|
|
} else { |
271
|
|
|
$this->crud()->addColumn($this->attributes); |
272
|
|
|
$this->attributes = $this->getFreshAttributes(); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get the fresh attributes for the current column. |
280
|
|
|
* |
281
|
|
|
* @return array |
282
|
|
|
*/ |
283
|
|
|
private function getFreshAttributes() |
284
|
|
|
{ |
285
|
|
|
$key = isset($this->attributes['key']) ? 'key' : 'name'; |
286
|
|
|
$search = $this->attributes['key'] ?? $this->attributes['name']; |
287
|
|
|
|
288
|
|
|
return $this->crud()->firstColumnWhere($key, $search); |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// ------------- |
292
|
|
|
// MAGIC METHODS |
293
|
|
|
// ------------- |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* If a developer calls a method that doesn't exist, assume they want: |
297
|
|
|
* - the CrudColumn object to have an attribute with that value; |
298
|
|
|
* - that column be updated inside the global CrudPanel object;. |
299
|
|
|
* |
300
|
|
|
* Eg: type('number') will set the "type" attribute to "number" |
301
|
|
|
* |
302
|
|
|
* @param string $method The method being called that doesn't exist. |
303
|
|
|
* @param array $parameters The arguments when that method was called. |
304
|
|
|
* @return CrudColumn |
305
|
|
|
*/ |
306
|
|
|
public function __call($method, $parameters) |
307
|
|
|
{ |
308
|
|
|
if (static::hasMacro($method)) { |
309
|
|
|
return $this->macroCall($method, $parameters); |
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$this->setAttributeValue($method, $parameters[0]); |
313
|
|
|
|
314
|
|
|
return $this->save(); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|