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