1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
trait Columns |
6
|
|
|
{ |
7
|
|
|
use ColumnsProtectedMethods; |
8
|
|
|
|
9
|
|
|
// ------------ |
10
|
|
|
// COLUMNS |
11
|
|
|
// ------------ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Get the CRUD columns for the current operation. |
15
|
|
|
* |
16
|
|
|
* @return array CRUD columns. |
17
|
|
|
*/ |
18
|
|
|
public function columns() |
19
|
|
|
{ |
20
|
|
|
return $this->getOperationSetting('columns') ?? []; |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Add a bunch of column names and their details to the CRUD object. |
25
|
|
|
* |
26
|
|
|
* @param array|string $columns |
27
|
|
|
*/ |
28
|
|
|
public function setColumns($columns) |
29
|
|
|
{ |
30
|
|
|
// clear any columns already set |
31
|
|
|
$this->removeAllColumns(); |
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' => mb_ucfirst($column), |
43
|
|
|
'type' => 'text', |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (is_string($columns)) { |
50
|
|
|
$this->addColumn([ |
51
|
|
|
'name' => $columns, |
52
|
|
|
'label' => mb_ucfirst($columns), |
53
|
|
|
'type' => 'text', |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add a column at the end of to the CRUD object's "columns" array. |
60
|
|
|
* |
61
|
|
|
* @param array|string $column |
62
|
|
|
* |
63
|
|
|
* @return self |
64
|
|
|
*/ |
65
|
|
|
public function addColumn($column) |
66
|
|
|
{ |
67
|
|
|
$column = $this->makeSureColumnHasNeededAttributes($column); |
68
|
|
|
|
69
|
|
|
$this->addColumnToOperationSettings($column); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add multiple columns at the end of the CRUD object's "columns" array. |
76
|
|
|
* |
77
|
|
|
* @param array $columns |
78
|
|
|
*/ |
79
|
|
|
public function addColumns($columns) |
80
|
|
|
{ |
81
|
|
|
if (count($columns)) { |
82
|
|
|
foreach ($columns as $key => $column) { |
83
|
|
|
$this->addColumn($column); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Move the most recently added column after the given target column. |
90
|
|
|
* |
91
|
|
|
* @param string|array $targetColumn The target column name or array. |
92
|
|
|
*/ |
93
|
|
|
public function afterColumn($targetColumn) |
94
|
|
|
{ |
95
|
|
|
$this->moveColumn($targetColumn, false); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Move the most recently added column before the given target column. |
100
|
|
|
* |
101
|
|
|
* @param string|array $targetColumn The target column name or array. |
102
|
|
|
*/ |
103
|
|
|
public function beforeColumn($targetColumn) |
104
|
|
|
{ |
105
|
|
|
$this->moveColumn($targetColumn); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Move this column to be first in the columns list. |
110
|
|
|
* |
111
|
|
|
* @return bool|null |
|
|
|
|
112
|
|
|
*/ |
113
|
|
|
public function makeFirstColumn() |
114
|
|
|
{ |
115
|
|
|
if (! $this->columns()) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$firstColumn = array_keys(array_slice($this->columns(), 0, 1))[0]; |
120
|
|
|
$this->beforeColumn($firstColumn); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Add the default column type to the given Column, inferring the type from the database column type. |
125
|
|
|
* |
126
|
|
|
* @param array $column |
127
|
|
|
* |
128
|
|
|
* @return array|bool |
|
|
|
|
129
|
|
|
*/ |
130
|
|
|
public function addDefaultTypeToColumn($column) |
131
|
|
|
{ |
132
|
|
|
if (array_key_exists('name', (array) $column)) { |
133
|
|
|
$default_type = $this->getFieldTypeFromDbColumnType($column['name']); |
|
|
|
|
134
|
|
|
|
135
|
|
|
return array_merge(['type' => $default_type], $column); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Remove a column from the CRUD panel by name. |
143
|
|
|
* |
144
|
|
|
* @param string $columnKey The column key. |
145
|
|
|
*/ |
146
|
|
|
public function removeColumn($columnKey) |
147
|
|
|
{ |
148
|
|
|
$columnsArray = $this->columns(); |
149
|
|
|
array_forget($columnsArray, $columnKey); |
150
|
|
|
$this->setOperationSetting('columns', $columnsArray); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Remove multiple columns from the CRUD panel by name. |
155
|
|
|
* |
156
|
|
|
* @param array $columns Array of column names. |
157
|
|
|
*/ |
158
|
|
|
public function removeColumns($columns) |
159
|
|
|
{ |
160
|
|
|
if (! empty($columns)) { |
161
|
|
|
foreach ($columns as $columnKey) { |
162
|
|
|
$this->removeColumn($columnKey); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Remove all columns from the CRUD panel. |
169
|
|
|
*/ |
170
|
|
|
public function removeAllColumns() |
171
|
|
|
{ |
172
|
|
|
$this->setOperationSetting('columns', []); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Change attributes for multiple columns. |
177
|
|
|
* |
178
|
|
|
* @param array $columns |
179
|
|
|
* @param array $attributes |
180
|
|
|
*/ |
181
|
|
|
public function setColumnsDetails($columns, $attributes) |
182
|
|
|
{ |
183
|
|
|
foreach ($columns as $columnKey) { |
184
|
|
|
$this->setColumnDetails($columnKey, $attributes); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Change attributes for a certain column. |
190
|
|
|
* |
191
|
|
|
* @param string $columnKey Column key. |
192
|
|
|
* @param array $attributesAndValues |
193
|
|
|
*/ |
194
|
|
|
public function setColumnDetails($columnKey, $attributesAndValues) |
195
|
|
|
{ |
196
|
|
|
$columnsArray = $this->columns(); |
197
|
|
|
|
198
|
|
|
if (isset($columnsArray[$columnKey])) { |
199
|
|
|
foreach ($attributesAndValues as $attributeName => $attributeValue) { |
200
|
|
|
$columnsArray[$columnKey][$attributeName] = $attributeValue; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$this->setOperationSetting('columns', $columnsArray); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Alias for setColumnDetails(). |
209
|
|
|
* Provides a consistent syntax with Fields, Buttons, Filters modify functionality. |
210
|
|
|
* |
211
|
|
|
* @param string $column Column name. |
212
|
|
|
* @param array $attributes |
213
|
|
|
*/ |
214
|
|
|
public function modifyColumn($column, $attributes) |
215
|
|
|
{ |
216
|
|
|
$this->setColumnDetails($column, $attributes); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Set label for a specific column. |
221
|
|
|
* |
222
|
|
|
* @param string $column |
223
|
|
|
* @param string $label |
224
|
|
|
*/ |
225
|
|
|
public function setColumnLabel($column, $label) |
226
|
|
|
{ |
227
|
|
|
$this->setColumnDetails($column, ['label' => $label]); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get the relationships used in the CRUD columns. |
232
|
|
|
* |
233
|
|
|
* @return array Relationship names |
234
|
|
|
*/ |
235
|
|
|
public function getColumnsRelationships() |
236
|
|
|
{ |
237
|
|
|
$columns = $this->columns(); |
238
|
|
|
|
239
|
|
|
return collect($columns)->pluck('entity')->reject(function ($value, $key) { |
|
|
|
|
240
|
|
|
return $value == null; |
241
|
|
|
})->toArray(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Order the CRUD columns. If certain columns are missing from the given order array, they will be pushed to the |
246
|
|
|
* new columns array in the original order. |
247
|
|
|
* |
248
|
|
|
* @param array $order An array of column names in the desired order. |
249
|
|
|
*/ |
250
|
|
|
public function orderColumns($order) |
251
|
|
|
{ |
252
|
|
|
$orderedColumns = []; |
253
|
|
|
foreach ($order as $columnName) { |
254
|
|
|
if (array_key_exists($columnName, $this->columns())) { |
255
|
|
|
$orderedColumns[$columnName] = $this->columns()[$columnName]; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if (empty($orderedColumns)) { |
260
|
|
|
return; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
$remaining = array_diff_key($this->columns(), $orderedColumns); |
264
|
|
|
$this->setOperationSetting('columns', array_merge($orderedColumns, $remaining)); |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get a column by the id, from the associative array. |
269
|
|
|
* |
270
|
|
|
* @param int $column_number Placement inside the columns array. |
271
|
|
|
* |
272
|
|
|
* @return array Column details. |
273
|
|
|
*/ |
274
|
|
|
public function findColumnById($column_number) |
275
|
|
|
{ |
276
|
|
|
$result = array_slice($this->columns(), $column_number, 1); |
277
|
|
|
|
278
|
|
|
return reset($result); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Get the visibility priority for the actions column |
283
|
|
|
* in the CRUD table view. |
284
|
|
|
* |
285
|
|
|
* @return int The priority, from 1 to infinity. Lower is better. |
286
|
|
|
*/ |
287
|
|
|
public function getActionsColumnPriority() |
288
|
|
|
{ |
289
|
|
|
return (int) $this->getOperationSetting('actionsColumnPriority') ?? 1; |
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Set a certain priority for the actions column |
294
|
|
|
* in the CRUD table view. Usually set to 10000 in order to hide it. |
295
|
|
|
* |
296
|
|
|
* @param int $number The priority, from 1 to infinity. Lower is better. |
297
|
|
|
* |
298
|
|
|
* @return self |
299
|
|
|
*/ |
300
|
|
|
public function setActionsColumnPriority($number) |
301
|
|
|
{ |
302
|
|
|
$this->setOperationSetting('actionsColumnPriority', (int) $number); |
|
|
|
|
303
|
|
|
|
304
|
|
|
return $this; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.