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