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