|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Traits\Support\MacroableWithAttributes; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Adds fluent syntax to Backpack CRUD Columns. |
|
9
|
|
|
* |
|
10
|
|
|
* In addition to the existing: |
|
11
|
|
|
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']); |
|
12
|
|
|
* |
|
13
|
|
|
* Developers can also do: |
|
14
|
|
|
* - CRUD::column('price')->type('number'); |
|
15
|
|
|
* |
|
16
|
|
|
* And if the developer uses CrudColumn as Column in their CrudController: |
|
17
|
|
|
* - Column::name('price')->type('number'); |
|
18
|
|
|
* |
|
19
|
|
|
* @method self type(string $value) |
|
20
|
|
|
* @method self label(string $value) |
|
21
|
|
|
* @method self searchLogic(mixed $value) |
|
22
|
|
|
* @method self orderLogic(callable $value) |
|
23
|
|
|
* @method self orderable(bool $value) |
|
24
|
|
|
* @method self wrapper(array $value) |
|
25
|
|
|
* @method self visibleInTable(bool $value) |
|
26
|
|
|
* @method self visibleInModal(bool $value) |
|
27
|
|
|
* @method self visibleInExport(bool $value) |
|
28
|
|
|
* @method self visibleInShow(bool $value) |
|
29
|
|
|
* @method self priority(int $value) |
|
30
|
|
|
* @method self key(string $value) |
|
31
|
|
|
* @method self upload(bool $value) |
|
32
|
|
|
*/ |
|
33
|
|
|
class CrudColumn |
|
34
|
|
|
{ |
|
35
|
|
|
use MacroableWithAttributes; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
protected $attributes; |
|
38
|
|
|
|
|
39
|
|
|
public function upload($upload = true) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->attributes['upload'] = $upload; |
|
42
|
|
|
|
|
43
|
|
|
return $this->save(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function __construct($name) |
|
47
|
|
|
{ |
|
48
|
|
|
$column = $this->crud()->firstColumnWhere('name', $name); |
|
49
|
|
|
|
|
50
|
|
|
// if column exists |
|
51
|
|
|
if ((bool) $column) { |
|
52
|
|
|
// use all existing attributes |
|
53
|
|
|
$this->setAllAttributeValues($column); |
|
|
|
|
|
|
54
|
|
|
} else { |
|
55
|
|
|
// it means we're creating the column now, |
|
56
|
|
|
// so at the very least set the name attribute |
|
57
|
|
|
$this->setAttributeValue('name', $name); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// guess all attributes that weren't explicitly defined |
|
61
|
|
|
$this->attributes = $this->crud()->makeSureColumnHasNeededAttributes($this->attributes); |
|
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
|
|
|
/** |
|
161
|
|
|
* Make the current column the first one in the columns list. |
|
162
|
|
|
* |
|
163
|
|
|
* @return CrudColumn |
|
164
|
|
|
*/ |
|
165
|
|
|
public function makeFirst() |
|
166
|
|
|
{ |
|
167
|
|
|
$this->crud()->removeColumn($this->attributes['name']); |
|
168
|
|
|
$this->crud()->addColumn($this->attributes)->makeFirstColumn(); |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Make the current column the last one in the columns list. |
|
175
|
|
|
* |
|
176
|
|
|
* @return CrudColumn |
|
177
|
|
|
*/ |
|
178
|
|
|
public function makeLast() |
|
179
|
|
|
{ |
|
180
|
|
|
$this->crud()->removeColumn($this->attributes['name']); |
|
181
|
|
|
$this->crud()->addColumn($this->attributes); |
|
182
|
|
|
|
|
183
|
|
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
// ----------------- |
|
187
|
|
|
// DEBUGGING METHODS |
|
188
|
|
|
// ----------------- |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Dump the current object to the screen, |
|
192
|
|
|
* so that the developer can see its contents. |
|
193
|
|
|
* |
|
194
|
|
|
* @codeCoverageIgnore |
|
195
|
|
|
* |
|
196
|
|
|
* @return CrudColumn |
|
197
|
|
|
*/ |
|
198
|
|
|
public function dump() |
|
199
|
|
|
{ |
|
200
|
|
|
dump($this); |
|
201
|
|
|
|
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Dump and die. Duumps the current object to the screen, |
|
207
|
|
|
* so that the developer can see its contents, then stops |
|
208
|
|
|
* the execution. |
|
209
|
|
|
* |
|
210
|
|
|
* @codeCoverageIgnore |
|
211
|
|
|
* |
|
212
|
|
|
* @return CrudColumn |
|
213
|
|
|
*/ |
|
214
|
|
|
public function dd() |
|
215
|
|
|
{ |
|
216
|
|
|
dd($this); |
|
217
|
|
|
|
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function getAttributes() |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->attributes; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
// --------------- |
|
227
|
|
|
// PRIVATE METHODS |
|
228
|
|
|
// --------------- |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Set the value for a certain attribute on the CrudColumn object. |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $attribute Name of the attribute. |
|
234
|
|
|
* @param mixed $value Value of that attribute. |
|
235
|
|
|
*/ |
|
236
|
|
|
private function setAttributeValue($attribute, $value) |
|
237
|
|
|
{ |
|
238
|
|
|
$this->attributes[$attribute] = $value; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Replace all column attributes on the CrudColumn object |
|
243
|
|
|
* with the given array of attribute-value pairs. |
|
244
|
|
|
* |
|
245
|
|
|
* @param array $array Array of attributes and their values. |
|
246
|
|
|
*/ |
|
247
|
|
|
private function setAllAttributeValues($array) |
|
248
|
|
|
{ |
|
249
|
|
|
$this->attributes = $array; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Update the global CrudPanel object with the current column attributes. |
|
254
|
|
|
* |
|
255
|
|
|
* @return CrudColumn |
|
256
|
|
|
*/ |
|
257
|
|
|
private function save() |
|
258
|
|
|
{ |
|
259
|
|
|
$key = $this->attributes['key'] ?? $this->attributes['name']; |
|
260
|
|
|
|
|
261
|
|
|
if ($this->crud()->hasColumnWhere('key', $key)) { |
|
262
|
|
|
$this->crud()->setColumnDetails($key, $this->attributes); |
|
263
|
|
|
} else { |
|
264
|
|
|
$this->crud()->addColumn($this->attributes); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
return $this; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
// ------------- |
|
271
|
|
|
// MAGIC METHODS |
|
272
|
|
|
// ------------- |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* If a developer calls a method that doesn't exist, assume they want: |
|
276
|
|
|
* - the CrudColumn object to have an attribute with that value; |
|
277
|
|
|
* - that column be updated inside the global CrudPanel object;. |
|
278
|
|
|
* |
|
279
|
|
|
* Eg: type('number') will set the "type" attribute to "number" |
|
280
|
|
|
* |
|
281
|
|
|
* @param string $method The method being called that doesn't exist. |
|
282
|
|
|
* @param array $parameters The arguments when that method was called. |
|
283
|
|
|
* @return CrudColumn |
|
284
|
|
|
*/ |
|
285
|
|
|
public function __call($method, $parameters) |
|
286
|
|
|
{ |
|
287
|
|
|
if (static::hasMacro($method)) { |
|
288
|
|
|
return $this->macroCall($method, $parameters); |
|
|
|
|
|
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
$this->setAttributeValue($method, $parameters[0]); |
|
292
|
|
|
|
|
293
|
|
|
return $this->save(); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|