1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
trait Columns |
6
|
|
|
{ |
7
|
|
|
// ------------ |
8
|
|
|
// COLUMNS |
9
|
|
|
// ------------ |
10
|
|
|
|
11
|
|
|
public $actions_column_priority = 1; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Get the CRUD columns. |
15
|
|
|
* |
16
|
3 |
|
* @return array CRUD columns. |
17
|
|
|
*/ |
18
|
3 |
|
public function getColumns() |
19
|
|
|
{ |
20
|
|
|
return $this->columns; |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Add a bunch of column names and their details to the CRUD object. |
25
|
|
|
* |
26
|
|
|
* @param [array or multi-dimensional array] |
27
|
|
|
*/ |
28
|
|
|
public function setColumns($columns) |
29
|
|
|
{ |
30
|
|
|
// clear any columns already set |
31
|
|
|
$this->columns = []; |
32
|
|
|
|
33
|
|
|
// if array, add a column for each of the items |
34
|
|
|
if (is_array($columns) && count($columns)) { |
35
|
|
|
foreach ($columns as $key => $column) { |
36
|
|
|
// if label and other details have been defined in the array |
37
|
|
|
if (is_array($column)) { |
38
|
|
|
$this->addColumn($column); |
39
|
|
|
} else { |
40
|
|
|
$this->addColumn([ |
41
|
|
|
'name' => $column, |
42
|
|
|
'label' => ucfirst($column), |
43
|
|
|
'type' => 'text', |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (is_string($columns)) { |
50
|
|
|
$this->addColumn([ |
51
|
|
|
'name' => $columns, |
52
|
|
|
'label' => ucfirst($columns), |
53
|
|
|
'type' => 'text', |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// This was the old setColumns() function, and it did not work: |
58
|
|
|
// $this->columns = array_filter(array_map([$this, 'addDefaultTypeToColumn'], $columns)); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Add a column at the end of to the CRUD object's "columns" array. |
63
|
|
|
* |
64
|
21 |
|
* @param [string or array] |
65
|
|
|
*/ |
66
|
|
|
public function addColumn($column) |
67
|
21 |
|
{ |
68
|
6 |
|
// if a string was passed, not an array, change it to an array |
69
|
|
|
if (! is_array($column)) { |
70
|
|
|
$column = ['name' => $column]; |
71
|
|
|
} |
72
|
21 |
|
|
73
|
|
|
// make sure the column has a label |
74
|
|
|
$column_with_details = $this->addDefaultLabel($column); |
75
|
21 |
|
|
76
|
|
|
// make sure the column has a name |
77
|
|
|
if (! array_key_exists('name', $column_with_details)) { |
78
|
|
|
$column_with_details['name'] = 'anonymous_column_'.str_random(5); |
79
|
|
|
} |
80
|
21 |
|
|
81
|
|
|
// check if the column exists in the database table |
82
|
|
|
$columnExistsInDb = $this->hasColumn($this->model->getTable(), $column_with_details['name']); |
|
|
|
|
83
|
21 |
|
|
84
|
18 |
|
// make sure the column has a type |
85
|
|
|
if (! array_key_exists('type', $column_with_details)) { |
86
|
|
|
$column_with_details['type'] = 'text'; |
87
|
|
|
} |
88
|
21 |
|
|
89
|
21 |
|
// make sure the column has a key |
90
|
|
|
if (! array_key_exists('key', $column_with_details)) { |
91
|
|
|
$column_with_details['key'] = $column_with_details['name']; |
92
|
|
|
} |
93
|
21 |
|
|
94
|
21 |
|
// make sure the column has a tableColumn boolean |
95
|
|
|
if (! array_key_exists('tableColumn', $column_with_details)) { |
96
|
|
|
$column_with_details['tableColumn'] = $columnExistsInDb ? true : false; |
97
|
|
|
} |
98
|
21 |
|
|
99
|
21 |
|
// make sure the column has a orderable boolean |
100
|
|
|
if (! array_key_exists('orderable', $column_with_details)) { |
101
|
|
|
$column_with_details['orderable'] = $columnExistsInDb ? true : false; |
102
|
|
|
} |
103
|
21 |
|
|
104
|
21 |
|
// make sure the column has a searchLogic |
105
|
|
|
if (! array_key_exists('searchLogic', $column_with_details)) { |
106
|
|
|
$column_with_details['searchLogic'] = $columnExistsInDb ? true : false; |
107
|
21 |
|
} |
108
|
|
|
|
109
|
|
|
array_filter($this->columns[$column_with_details['key']] = $column_with_details); |
110
|
|
|
|
111
|
21 |
|
// make sure the column has a priority in terms of visibility |
112
|
1 |
|
// if no priority has been defined, use the order in the array plus one |
113
|
|
|
if (! array_key_exists('priority', $column_with_details)) { |
114
|
|
|
$position_in_columns_array = (int) array_search($column_with_details['key'], array_keys($this->columns)); |
115
|
21 |
|
$this->columns[$column_with_details['key']]['priority'] = $position_in_columns_array + 1; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// if this is a relation type field and no corresponding model was specified, get it from the relation method |
119
|
|
|
// defined in the main model |
120
|
|
|
if (isset($column_with_details['entity']) && ! isset($column_with_details['model'])) { |
121
|
|
|
$column_with_details['model'] = $this->getRelationModel($column_with_details['entity']); |
|
|
|
|
122
|
|
|
} |
123
|
16 |
|
|
124
|
|
|
return $this; |
125
|
16 |
|
} |
126
|
16 |
|
|
127
|
15 |
|
/** |
128
|
|
|
* Add multiple columns at the end of the CRUD object's "columns" array. |
129
|
|
|
* |
130
|
15 |
|
* @param [array of columns] |
131
|
|
|
*/ |
132
|
|
|
public function addColumns($columns) |
133
|
|
|
{ |
134
|
|
|
if (count($columns)) { |
135
|
|
|
foreach ($columns as $key => $column) { |
136
|
|
|
$this->addColumn($column); |
137
|
2 |
|
} |
138
|
|
|
} |
139
|
2 |
|
} |
140
|
2 |
|
|
141
|
|
|
/** |
142
|
|
|
* Move the most recently added column after the given target column. |
143
|
|
|
* |
144
|
|
|
* @param string|array $targetColumn The target column name or array. |
145
|
|
|
*/ |
146
|
|
|
public function afterColumn($targetColumn) |
147
|
2 |
|
{ |
148
|
|
|
$this->moveColumn($targetColumn, false); |
149
|
2 |
|
} |
150
|
2 |
|
|
151
|
|
|
/** |
152
|
|
|
* Move the most recently added column before the given target column. |
153
|
|
|
* |
154
|
|
|
* @param string|array $targetColumn The target column name or array. |
155
|
|
|
*/ |
156
|
|
|
public function beforeColumn($targetColumn) |
157
|
|
|
{ |
158
|
|
|
$this->moveColumn($targetColumn); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Move this column to be first in the columns list. |
163
|
|
|
*/ |
164
|
|
|
public function makeFirstColumn() |
165
|
|
|
{ |
166
|
|
|
if (! $this->columns) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$firstColumn = array_keys(array_slice($this->columns, 0, 1))[0]; |
171
|
4 |
|
$this->beforeColumn($firstColumn); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
4 |
|
* Move the most recently added column before or after the given target column. Default is before. |
176
|
|
|
* |
177
|
4 |
|
* @param string|array $targetColumn The target column name or array. |
178
|
2 |
|
* @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it. |
|
|
|
|
179
|
2 |
|
*/ |
180
|
|
|
private function moveColumn($targetColumn, $before = true) |
181
|
2 |
|
{ |
182
|
2 |
|
// TODO: this and the moveField method from the Fields trait should be refactored into a single method and moved |
183
|
2 |
|
// into a common class |
184
|
|
|
$targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn; |
185
|
2 |
|
|
186
|
|
|
if (array_key_exists($targetColumnName, $this->columns)) { |
187
|
4 |
|
$targetColumnPosition = $before ? array_search($targetColumnName, array_keys($this->columns)) : |
188
|
|
|
array_search($targetColumnName, array_keys($this->columns)) + 1; |
189
|
|
|
|
190
|
|
|
$element = array_pop($this->columns); |
191
|
|
|
$beginningPart = array_slice($this->columns, 0, $targetColumnPosition, true); |
192
|
|
|
$endingArrayPart = array_slice($this->columns, $targetColumnPosition, null, true); |
193
|
|
|
|
194
|
|
|
$this->columns = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Add the default column type to the given Column, inferring the type from the database column type. |
200
|
|
|
* |
201
|
|
|
* @param [column array] |
202
|
|
|
*/ |
203
|
|
|
public function addDefaultTypeToColumn($column) |
204
|
|
|
{ |
205
|
|
|
if (array_key_exists('name', (array) $column)) { |
206
|
|
|
$default_type = $this->getFieldTypeFromDbColumnType($column['name']); |
|
|
|
|
207
|
|
|
|
208
|
|
|
return array_merge(['type' => $default_type], $column); |
209
|
|
|
} |
210
|
|
|
|
211
|
21 |
|
return false; |
212
|
|
|
} |
213
|
21 |
|
|
214
|
7 |
|
/** |
215
|
|
|
* If a field or column array is missing the "label" attribute, an ugly error would be show. |
216
|
7 |
|
* So we add the field Name as a label - it's better than nothing. |
217
|
|
|
* |
218
|
|
|
* @param [field or column] |
219
|
14 |
|
*/ |
220
|
|
|
public function addDefaultLabel($array) |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
if (! array_key_exists('label', (array) $array) && array_key_exists('name', (array) $array)) { |
223
|
|
|
$array = array_merge(['label' => ucfirst($this->makeLabel($array['name']))], $array); |
|
|
|
|
224
|
|
|
|
225
|
|
|
return $array; |
226
|
|
|
} |
227
|
4 |
|
|
228
|
|
|
return $array; |
229
|
4 |
|
} |
230
|
4 |
|
|
231
|
|
|
/** |
232
|
|
|
* Remove a column from the CRUD panel by name. |
233
|
|
|
* |
234
|
|
|
* @param string $column The column name. |
235
|
|
|
*/ |
236
|
|
|
public function removeColumn($column) |
237
|
2 |
|
{ |
238
|
|
|
array_forget($this->columns, $column); |
239
|
2 |
|
} |
240
|
2 |
|
|
241
|
2 |
|
/** |
242
|
|
|
* Remove multiple columns from the CRUD panel by name. |
243
|
|
|
* |
244
|
2 |
|
* @param array $columns Array of column names. |
245
|
|
|
*/ |
246
|
|
|
public function removeColumns($columns) |
247
|
|
|
{ |
248
|
|
|
if (! empty($columns)) { |
249
|
|
|
foreach ($columns as $columnName) { |
250
|
|
|
$this->removeColumn($columnName); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Remove an entry from an array. |
257
|
|
|
* |
258
|
|
|
* @param string $entity |
259
|
|
|
* @param array $fields |
260
|
|
|
* @return array values |
261
|
|
|
* |
262
|
|
|
* @deprecated This method is no longer used by internal code and is not recommended as it does not preserve the |
263
|
|
|
* target array keys. |
264
|
|
|
* @see Columns::removeColumn() to remove a column from the CRUD panel by name. |
265
|
|
|
* @see Columns::removeColumns() to remove multiple columns from the CRUD panel by name. |
266
|
|
|
*/ |
267
|
|
|
public function remove($entity, $fields) |
268
|
|
|
{ |
269
|
|
|
return array_values(array_filter($this->{$entity}, function ($field) use ($fields) { |
270
|
|
|
return ! in_array($field['name'], (array) $fields); |
271
|
|
|
})); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Change attributes for multiple columns. |
276
|
|
|
* |
277
|
|
|
* @param [columns arrays] |
278
|
|
|
* @param [attributes and values array] |
279
|
|
|
*/ |
280
|
|
|
public function setColumnsDetails($columns, $attributes) |
281
|
|
|
{ |
282
|
|
|
$this->sync('columns', $columns, $attributes); |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Change attributes for a certain column. |
287
|
|
|
* |
288
|
|
|
* @param [string] Column name. |
289
|
|
|
* @param [attributes and values array] |
290
|
|
|
*/ |
291
|
|
|
public function setColumnDetails($column, $attributes) |
292
|
|
|
{ |
293
|
|
|
$this->setColumnsDetails([$column], $attributes); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Alias for setColumnDetails(). |
298
|
|
|
* Provides a consistent syntax with Fields, Buttons, Filters modify functionality. |
299
|
|
|
* |
300
|
|
|
* @param [string] Column name. |
301
|
|
|
* @param [attributes and values array] |
302
|
3 |
|
*/ |
303
|
|
|
public function modifyColumn($column, $attributes) |
304
|
3 |
|
{ |
305
|
|
|
$this->setColumnDetails($column, $attributes); |
306
|
3 |
|
} |
307
|
2 |
|
|
308
|
3 |
|
/** |
309
|
|
|
* Set label for a specific column. |
310
|
|
|
* |
311
|
|
|
* @param string $column |
312
|
|
|
* @param string $label |
313
|
|
|
*/ |
314
|
|
|
public function setColumnLabel($column, $label) |
315
|
|
|
{ |
316
|
|
|
$this->setColumnDetails($column, ['label' => $label]); |
317
|
5 |
|
} |
318
|
|
|
|
319
|
5 |
|
/** |
320
|
5 |
|
* Get the relationships used in the CRUD columns. |
321
|
4 |
|
* @return [array] Relationship names |
|
|
|
|
322
|
4 |
|
*/ |
323
|
|
|
public function getColumnsRelationships() |
324
|
|
|
{ |
325
|
|
|
$columns = $this->getColumns(); |
326
|
5 |
|
|
327
|
2 |
|
return collect($columns)->pluck('entity')->reject(function ($value, $key) { |
|
|
|
|
328
|
|
|
return $value == null; |
329
|
|
|
})->toArray(); |
330
|
3 |
|
} |
331
|
3 |
|
|
332
|
3 |
|
/** |
333
|
|
|
* Order the CRUD columns. If certain columns are missing from the given order array, they will be pushed to the |
334
|
|
|
* new columns array in the original order. |
335
|
|
|
* |
336
|
|
|
* @param array $order An array of column names in the desired order. |
337
|
|
|
*/ |
338
|
|
|
public function orderColumns($order) |
339
|
|
|
{ |
340
|
|
|
$orderedColumns = []; |
341
|
|
|
foreach ($order as $columnName) { |
342
|
|
|
if (array_key_exists($columnName, $this->columns)) { |
343
|
|
|
$orderedColumns[$columnName] = $this->columns[$columnName]; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
if (empty($orderedColumns)) { |
348
|
|
|
return; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
$remaining = array_diff_key($this->columns, $orderedColumns); |
352
|
|
|
$this->columns = array_merge($orderedColumns, $remaining); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Set the order of the CRUD columns. |
357
|
|
|
* |
358
|
|
|
* @param array $columns Column order. |
359
|
|
|
* |
360
|
|
|
* @deprecated This method was not and will not be implemented since it's a duplicate of the orderColumns method. |
361
|
|
|
* @see Columns::orderColumns() to order the CRUD columns. |
362
|
|
|
*/ |
363
|
|
|
public function setColumnOrder($columns) |
|
|
|
|
364
|
|
|
{ |
365
|
|
|
// not implemented |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Set the order of the CRUD columns. |
370
|
|
|
* |
371
|
|
|
* @param array $columns Column order. |
372
|
21 |
|
* |
373
|
|
|
* @deprecated This method was not and will not be implemented since it's a duplicate of the orderColumns method. |
374
|
21 |
|
* @see Columns::orderColumns() to order the CRUD columns. |
375
|
|
|
*/ |
376
|
21 |
|
public function setColumnsOrder($columns) |
377
|
19 |
|
{ |
378
|
|
|
$this->setColumnOrder($columns); |
|
|
|
|
379
|
3 |
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
21 |
|
* Get a column by the id, from the associative array. |
383
|
|
|
* @param [integer] $column_number Placement inside the columns array. |
|
|
|
|
384
|
|
|
* @return [array] Column details. |
|
|
|
|
385
|
|
|
*/ |
386
|
|
|
public function findColumnById($column_number) |
|
|
|
|
387
|
|
|
{ |
388
|
|
|
$result = array_slice($this->getColumns(), $column_number, 1); |
389
|
|
|
|
390
|
|
|
return reset($result); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
protected function hasColumn($table, $name) |
394
|
|
|
{ |
395
|
|
|
static $cache = []; |
396
|
|
|
|
397
|
|
|
if (isset($cache[$table])) { |
398
|
|
|
$columns = $cache[$table]; |
399
|
|
|
} else { |
400
|
|
|
$columns = $cache[$table] = $this->getSchema()->getColumnListing($table); |
|
|
|
|
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
return in_array($name, $columns); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Get the visibility priority for the actions column |
408
|
|
|
* in the CRUD table view. |
409
|
|
|
* |
410
|
|
|
* @return int The priority, from 1 to infinity. Lower is better. |
411
|
|
|
*/ |
412
|
|
|
public function getActionsColumnPriority() |
413
|
|
|
{ |
414
|
|
|
return (int) $this->actions_column_priority; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Set a certain priority for the actions column |
419
|
|
|
* in the CRUD table view. Usually set to 10000 in order to hide it. |
420
|
|
|
* |
421
|
|
|
* @param int $number The priority, from 1 to infinity. Lower is better. |
422
|
|
|
*/ |
423
|
|
|
public function setActionsColumnPriority($number) |
424
|
|
|
{ |
425
|
|
|
$this->actions_column_priority = (int) $number; |
426
|
|
|
|
427
|
|
|
return $this; |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: