|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudColumn; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
trait ColumnsProtectedMethods |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Add a column to the current operation, using the Setting API. |
|
13
|
|
|
* |
|
14
|
|
|
* @param array $column Column definition array. |
|
15
|
|
|
*/ |
|
16
|
|
|
protected function addColumnToOperationSettings($column) |
|
17
|
|
|
{ |
|
18
|
|
|
$allColumns = $this->columns(); |
|
|
|
|
|
|
19
|
|
|
$allColumns = Arr::add($allColumns, $column['key'], $column); |
|
20
|
|
|
|
|
21
|
|
|
$this->setOperationSetting('columns', $allColumns); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* If a column priority has not been defined, provide a default one. |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $column Column definition array. |
|
28
|
|
|
* @return array Proper array defining the column. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function makeSureColumnHasPriority($column) |
|
31
|
|
|
{ |
|
32
|
|
|
$columns_count = $this->countColumnsWithoutActions(); |
|
|
|
|
|
|
33
|
|
|
$assumed_priority = $columns_count ? $columns_count : 0; |
|
34
|
|
|
|
|
35
|
|
|
$column['priority'] = $column['priority'] ?? $assumed_priority; |
|
36
|
|
|
|
|
37
|
|
|
return $column; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* If the field definition array is actually a string, it means the programmer was lazy |
|
42
|
|
|
* and has only passed the name of the column. Turn that into a proper array. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $column Column definition array. |
|
45
|
|
|
* @return array Proper array defining the column. |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function makeSureColumnHasName($column) |
|
48
|
|
|
{ |
|
49
|
|
|
if (is_string($column)) { |
|
|
|
|
|
|
50
|
|
|
return ['name' => Str::replace(' ', '', $column)]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (is_array($column) && ! isset($column['name'])) { |
|
54
|
|
|
$column['name'] = 'anonymous_column_'.Str::random(5); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$column['name'] = Str::replace(' ', '', $column['name']); |
|
58
|
|
|
|
|
59
|
|
|
return $column; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* If a column array is missing the "label" attribute, an ugly error would be show. |
|
64
|
|
|
* So we add the field Name as a label - it's better than nothing. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $column Column definition array. |
|
67
|
|
|
* @return array Proper array defining the column. |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function makeSureColumnHasLabel($column) |
|
70
|
|
|
{ |
|
71
|
|
|
if (! isset($column['label'])) { |
|
72
|
|
|
$column['label'] = mb_ucfirst($this->makeLabel($column['name'])); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $column; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* If a column definition is missing the type, set a default. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $column Column definition array. |
|
82
|
|
|
* @return array Column definition array with type. |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function makeSureColumnHasType($column) |
|
85
|
|
|
{ |
|
86
|
|
|
// Do not alter type if it has been set by developer |
|
87
|
|
|
if (isset($column['type'])) { |
|
88
|
|
|
return $column; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Set text as default column type |
|
92
|
|
|
$column['type'] = 'text'; |
|
93
|
|
|
|
|
94
|
|
|
if (method_exists($this->model, 'translationEnabledForModel') && $this->model->translationEnabledForModel() && array_key_exists($column['name'], $this->model->getTranslations())) { |
|
95
|
|
|
return $column; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$could_be_relation = Arr::get($column, 'entity', false) !== false; |
|
99
|
|
|
|
|
100
|
|
|
if ($could_be_relation) { |
|
101
|
|
|
$column['type'] = $this->inferFieldTypeFromRelationType($column['relation_type']); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (in_array($column['name'], $this->model->getDates())) { |
|
105
|
|
|
$column['type'] = 'datetime'; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if ($this->model->hasCast($column['name'])) { |
|
109
|
|
|
$attributeType = $this->model->getCasts()[$column['name']]; |
|
110
|
|
|
|
|
111
|
|
|
switch ($attributeType) { |
|
112
|
|
|
case 'array': |
|
113
|
|
|
case 'encrypted:array': |
|
114
|
|
|
case 'collection': |
|
115
|
|
|
case 'encrypted:collection': |
|
116
|
|
|
$column['type'] = 'array'; |
|
117
|
|
|
break; |
|
118
|
|
|
case 'json': |
|
119
|
|
|
case 'object': |
|
120
|
|
|
$column['type'] = 'json'; |
|
121
|
|
|
break; |
|
122
|
|
|
case 'bool': |
|
123
|
|
|
case 'boolean': |
|
124
|
|
|
$column['type'] = 'check'; |
|
125
|
|
|
break; |
|
126
|
|
|
case 'date': |
|
127
|
|
|
$column['type'] = 'date'; |
|
128
|
|
|
break; |
|
129
|
|
|
case 'datetime': |
|
130
|
|
|
$column['type'] = 'datetime'; |
|
131
|
|
|
break; |
|
132
|
|
|
case 'double': |
|
133
|
|
|
case 'float': |
|
134
|
|
|
case 'int': |
|
135
|
|
|
case 'integer': |
|
136
|
|
|
case 'real': |
|
137
|
|
|
case 'timestamp': |
|
138
|
|
|
$column['type'] = 'number'; |
|
139
|
|
|
break; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $column; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* If a column definition is missing the key, set the default. |
|
148
|
|
|
* The key is used when storing all columns using the Settings API, |
|
149
|
|
|
* it is used as the "key" of the associative array that holds all columns. |
|
150
|
|
|
* |
|
151
|
|
|
* @param array $column Column definition array. |
|
152
|
|
|
* @return array Column definition array with key. |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function makeSureColumnHasKey($column) |
|
155
|
|
|
{ |
|
156
|
|
|
if (! isset($column['key'])) { |
|
157
|
|
|
$column['key'] = str_replace('.', '__', $column['name']); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $column; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* If a column definition is missing the wrapper element, set the default (empty). |
|
165
|
|
|
* The wrapper is the HTML element that wrappes around the column text. |
|
166
|
|
|
* By defining this array a developer can wrap the text into an anchor (link), |
|
167
|
|
|
* span, div or whatever they want. |
|
168
|
|
|
* |
|
169
|
|
|
* @param array $column Column definition array. |
|
170
|
|
|
* @return array Column definition array with wrapper. |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function makeSureColumnHasWrapper($column) |
|
173
|
|
|
{ |
|
174
|
|
|
if (! isset($column['wrapper'])) { |
|
175
|
|
|
$column['wrapper'] = []; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $column; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
protected function makeSureColumnHasEntity($column) |
|
182
|
|
|
{ |
|
183
|
|
|
if (isset($column['entity'])) { |
|
184
|
|
|
return $column; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
// if the name is an array it's definitely not a relationship |
|
188
|
|
|
if (is_array($column['name'])) { |
|
189
|
|
|
return $column; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// if the name is dot notation it might be a relationship |
|
193
|
|
|
if (strpos($column['name'], '.') !== false) { |
|
194
|
|
|
$possibleMethodName = Str::before($column['name'], '.'); |
|
195
|
|
|
|
|
196
|
|
|
// if the first part of the string exists as method in the model |
|
197
|
|
|
if (method_exists($this->model, $possibleMethodName)) { |
|
198
|
|
|
// check model method for possibility of being a relationship |
|
199
|
|
|
$column['entity'] = $this->modelMethodIsRelationship($this->model, $possibleMethodName) ? $column['name'] : false; |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
if ($column['entity']) { |
|
202
|
|
|
// if the user setup the attribute in relation string, we are not going to infer that attribute from model |
|
203
|
|
|
// instead we get the defined attribute by the user. |
|
204
|
|
|
if ($this->isAttributeInRelationString($column)) { |
|
|
|
|
|
|
205
|
|
|
$column['attribute'] = $column['attribute'] ?? Str::afterLast($column['entity'], '.'); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $column; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
// if there's a method on the model with this name |
|
214
|
|
|
if (method_exists($this->model, $column['name'])) { |
|
215
|
|
|
// check model method for possibility of being a relationship |
|
216
|
|
|
$column['entity'] = $this->modelMethodIsRelationship($this->model, $column['name']); |
|
217
|
|
|
|
|
218
|
|
|
return $column; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
// if the name ends with _id and that method exists, |
|
222
|
|
|
// we can probably use it as an entity |
|
223
|
|
|
if (Str::endsWith($column['name'], '_id')) { |
|
224
|
|
|
$possibleMethodName = Str::replaceLast('_id', '', $column['name']); |
|
225
|
|
|
|
|
226
|
|
|
if (method_exists($this->model, $possibleMethodName)) { |
|
227
|
|
|
// check model method for possibility of being a relationship |
|
228
|
|
|
$column['entity'] = $this->modelMethodIsRelationship($this->model, $possibleMethodName); |
|
229
|
|
|
|
|
230
|
|
|
return $column; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return $column; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Infer the attribute for the column when needed. |
|
239
|
|
|
* |
|
240
|
|
|
* @param array $column |
|
241
|
|
|
* @return void |
|
242
|
|
|
*/ |
|
243
|
|
|
protected function makeSureColumnHasAttribute(array $column) |
|
244
|
|
|
{ |
|
245
|
|
|
return $this->makeSureFieldHasAttribute($column); |
|
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* If an entity has been defined for the column, but no model, |
|
250
|
|
|
* determine the model from that relationship. |
|
251
|
|
|
* |
|
252
|
|
|
* @param array $column Column definition array. |
|
253
|
|
|
* @return array Column definition array with model. |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function makeSureColumnHasModel($column) |
|
256
|
|
|
{ |
|
257
|
|
|
// if this is a relation type field and no corresponding model was specified, |
|
258
|
|
|
// get it from the relation method defined in the main model |
|
259
|
|
|
if (isset($column['entity']) && $column['entity'] !== false && ! isset($column['model'])) { |
|
260
|
|
|
$column['model'] = $this->getRelationModel($column['entity']); |
|
|
|
|
|
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
return $column; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* If an entity has been defined for the column, but no relation type, |
|
268
|
|
|
* determine the relation type from that relationship. |
|
269
|
|
|
* |
|
270
|
|
|
* @param array $column Column definition array. |
|
271
|
|
|
* @return array Column definition array with model. |
|
272
|
|
|
*/ |
|
273
|
|
|
protected function makeSureColumnHasRelationType($column) |
|
274
|
|
|
{ |
|
275
|
|
|
if (isset($column['entity']) && $column['entity'] !== false) { |
|
276
|
|
|
$column['relation_type'] = $column['relation_type'] ?? $this->inferRelationTypeFromRelationship($column); |
|
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
return $column; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Move the most recently added column before or after the given target column. Default is before. |
|
284
|
|
|
* |
|
285
|
|
|
* @param string|array $targetColumn The target column name or array. |
|
286
|
|
|
* @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it. |
|
287
|
|
|
*/ |
|
288
|
|
|
protected function moveColumn($targetColumn, $before = true) |
|
289
|
|
|
{ |
|
290
|
|
|
// TODO: this and the moveField method from the Fields trait should be refactored into a single method and moved |
|
291
|
|
|
// into a common class |
|
292
|
|
|
$targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn; |
|
293
|
|
|
$columnsArray = $this->columns(); |
|
294
|
|
|
|
|
295
|
|
|
if (array_key_exists($targetColumnName, $columnsArray)) { |
|
296
|
|
|
$targetColumnPosition = $before ? array_search($targetColumnName, array_keys($columnsArray)) : |
|
297
|
|
|
array_search($targetColumnName, array_keys($columnsArray)) + 1; |
|
298
|
|
|
|
|
299
|
|
|
$element = array_pop($columnsArray); |
|
300
|
|
|
$beginningPart = array_slice($columnsArray, 0, $targetColumnPosition, true); |
|
|
|
|
|
|
301
|
|
|
$endingArrayPart = array_slice($columnsArray, $targetColumnPosition, null, true); |
|
|
|
|
|
|
302
|
|
|
|
|
303
|
|
|
$columnsArray = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart); |
|
304
|
|
|
$this->setOperationSetting('columns', $columnsArray); |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Check if the column exists in the database, as a DB column. |
|
310
|
|
|
* |
|
311
|
|
|
* @param string $table |
|
312
|
|
|
* @param string $name |
|
313
|
|
|
* @return bool |
|
314
|
|
|
*/ |
|
315
|
|
|
protected function hasDatabaseColumn($table, $name) |
|
316
|
|
|
{ |
|
317
|
|
|
static $cache = []; |
|
318
|
|
|
|
|
319
|
|
|
if (! $this->driverIsSql()) { |
|
|
|
|
|
|
320
|
|
|
return true; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
if (isset($cache[$table])) { |
|
324
|
|
|
$columns = $cache[$table]; |
|
325
|
|
|
} else { |
|
326
|
|
|
$columns = $cache[$table] = $this->getSchema()->getColumnListing($table); |
|
|
|
|
|
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
return in_array($name, $columns); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Prepare the column attributes and add it to operation settings. |
|
334
|
|
|
*/ |
|
335
|
|
|
private function prepareAttributesAndAddColumn(array|string $column): CrudColumn |
|
336
|
|
|
{ |
|
337
|
|
|
$column = $this->makeSureColumnHasNeededAttributes($column); |
|
|
|
|
|
|
338
|
|
|
$this->addColumnToOperationSettings($column); |
|
339
|
|
|
|
|
340
|
|
|
$column = (new CrudColumn($column['name']))->callRegisteredAttributeMacros(); |
|
341
|
|
|
|
|
342
|
|
|
return $column; |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
|