|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
|
4
|
|
|
|
|
5
|
|
|
trait Columns |
|
6
|
|
|
{ |
|
7
|
|
|
// ------------ |
|
8
|
|
|
// COLUMNS |
|
9
|
|
|
// ------------ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Add a bunch of column names and their details to the CRUD object. |
|
13
|
|
|
* |
|
14
|
|
|
* @param [array or multi-dimensional array] |
|
15
|
|
|
*/ |
|
16
|
|
|
public function setColumns($columns) |
|
17
|
|
|
{ |
|
18
|
|
|
// clear any columns already set |
|
19
|
|
|
$this->columns = []; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
// if array, add a column for each of the items |
|
22
|
|
|
if (is_array($columns) && count($columns)) { |
|
23
|
|
|
foreach ($columns as $key => $column) { |
|
24
|
|
|
// if label and other details have been defined in the array |
|
25
|
|
|
if (is_array($column)) { |
|
26
|
|
|
$this->addColumn($column); |
|
27
|
|
|
} else { |
|
28
|
|
|
$this->addColumn([ |
|
29
|
|
|
'name' => $column, |
|
30
|
|
|
'label' => ucfirst($column), |
|
31
|
|
|
'type' => 'text', |
|
32
|
|
|
]); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (is_string($columns)) { |
|
38
|
|
|
$this->addColumn([ |
|
39
|
|
|
'name' => $columns, |
|
40
|
|
|
'label' => ucfirst($columns), |
|
41
|
|
|
'type' => 'text', |
|
42
|
|
|
]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// This was the old setColumns() function, and it did not work: |
|
46
|
|
|
// $this->columns = array_filter(array_map([$this, 'addDefaultTypeToColumn'], $columns)); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Add a column at the end of to the CRUD object's "columns" array. |
|
51
|
|
|
* |
|
52
|
|
|
* @param [string or array] |
|
53
|
|
|
*/ |
|
54
|
|
|
public function addColumn($column) |
|
55
|
|
|
{ |
|
56
|
|
|
// if a string was passed, not an array, change it to an array |
|
57
|
|
|
if (! is_array($column)) { |
|
58
|
|
|
$column = ['name' => $column]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// make sure the column has a label |
|
62
|
|
|
$column_with_details = $this->addDefaultLabel($column); |
|
63
|
|
|
|
|
64
|
|
|
array_filter($this->columns[] = $column_with_details); |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Add multiple columns at the end of the CRUD object's "columns" array. |
|
71
|
|
|
* |
|
72
|
|
|
* @param [array of columns] |
|
73
|
|
|
*/ |
|
74
|
|
|
public function addColumns($columns) |
|
75
|
|
|
{ |
|
76
|
|
|
if (count($columns)) { |
|
77
|
|
|
foreach ($columns as $key => $column) { |
|
78
|
|
|
$this->addColumn($column); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Moves the recently added column to 'before' the $target_col |
|
85
|
|
|
* |
|
86
|
|
|
* @param $target_col |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
public function beforeColumn($target_col) { |
|
|
|
|
|
|
89
|
|
|
foreach ($this->columns as $column => $value) { |
|
90
|
|
|
if ($value['name'] == $target_col) { |
|
91
|
|
|
array_splice($this->columns, $column, 0, array(array_pop($this->columns))); |
|
92
|
|
|
break; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Moves the recently added column to 'after' the $target_col |
|
99
|
|
|
* |
|
100
|
|
|
* @param $target |
|
101
|
|
|
*/ |
|
102
|
|
View Code Duplication |
public function afterColumn($target_col) { |
|
|
|
|
|
|
103
|
|
|
foreach ($this->columns as $column => $value) { |
|
104
|
|
|
if ($value['name'] == $target_col) { |
|
105
|
|
|
array_splice($this->columns, $column + 1, 0, array(array_pop($this->columns))); |
|
106
|
|
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Add the default column type to the given Column, inferring the type from the database column type. |
|
113
|
|
|
* |
|
114
|
|
|
* @param [column array] |
|
115
|
|
|
*/ |
|
116
|
|
|
public function addDefaultTypeToColumn($column) |
|
117
|
|
|
{ |
|
118
|
|
|
if (array_key_exists('name', (array) $column)) { |
|
119
|
|
|
$default_type = $this->getFieldTypeFromDbColumnType($column['name']); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
return array_merge(['type' => $default_type], $column); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* If a field or column array is missing the "label" attribute, an ugly error would be show. |
|
129
|
|
|
* So we add the field Name as a label - it's better than nothing. |
|
130
|
|
|
* |
|
131
|
|
|
* @param [field or column] |
|
132
|
|
|
*/ |
|
133
|
|
|
public function addDefaultLabel($array) |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
|
|
if (! array_key_exists('label', (array) $array) && array_key_exists('name', (array) $array)) { |
|
136
|
|
|
$array = array_merge(['label' => ucfirst($this->makeLabel($array['name']))], $array); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
return $array; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $array; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Remove multiple columns from the CRUD object using their names. |
|
146
|
|
|
* |
|
147
|
|
|
* @param [column array] |
|
148
|
|
|
*/ |
|
149
|
|
|
public function removeColumns($columns) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->columns = $this->remove('columns', $columns); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Remove a column from the CRUD object using its name. |
|
156
|
|
|
* |
|
157
|
|
|
* @param [column array] |
|
158
|
|
|
*/ |
|
159
|
|
|
public function removeColumn($column) |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->removeColumns([$column]); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $entity |
|
166
|
|
|
*/ |
|
167
|
|
|
public function remove($entity, $fields) |
|
168
|
|
|
{ |
|
169
|
|
|
return array_values(array_filter($this->{$entity}, function ($field) use ($fields) { |
|
170
|
|
|
return ! in_array($field['name'], (array) $fields); |
|
171
|
|
|
})); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Change attributes for multiple columns. |
|
176
|
|
|
* |
|
177
|
|
|
* @param [columns arrays] |
|
178
|
|
|
* @param [attributes and values array] |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setColumnsDetails($columns, $attributes) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->sync('columns', $columns, $attributes); |
|
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Change attributes for a certain column. |
|
187
|
|
|
* |
|
188
|
|
|
* @param [string] Column name. |
|
189
|
|
|
* @param [attributes and values array] |
|
190
|
|
|
*/ |
|
191
|
|
|
public function setColumnDetails($column, $attributes) |
|
192
|
|
|
{ |
|
193
|
|
|
$this->setColumnsDetails([$column], $attributes); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Order the columns in a certain way. |
|
198
|
|
|
* |
|
199
|
|
|
* @param [string] Column name. |
|
200
|
|
|
* @param [attributes and values array] |
|
201
|
|
|
*/ |
|
202
|
|
|
public function setColumnOrder($columns) |
|
|
|
|
|
|
203
|
|
|
{ |
|
204
|
|
|
// TODO |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
// ALIAS of setColumnOrder($columns) |
|
208
|
|
|
public function setColumnsOrder($columns) |
|
209
|
|
|
{ |
|
210
|
|
|
$this->setColumnOrder($columns); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Get the relationships used in the CRUD columns. |
|
215
|
|
|
* @return [array] Relationship names |
|
|
|
|
|
|
216
|
|
|
*/ |
|
217
|
|
|
public function getColumnsRelationships() |
|
218
|
|
|
{ |
|
219
|
|
|
$columns = $this->getColumns(); |
|
220
|
|
|
|
|
221
|
|
|
return collect($columns)->pluck('entity')->reject(function ($value, $key) { |
|
|
|
|
|
|
222
|
|
|
return $value == null; |
|
223
|
|
|
})->toArray(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
// ------------ |
|
227
|
|
|
// TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED |
|
228
|
|
|
// ------------ |
|
229
|
|
|
// TODO: check them |
|
230
|
|
|
|
|
231
|
|
|
public function getColumns() |
|
|
|
|
|
|
232
|
|
|
{ |
|
233
|
|
|
return $this->sort('columns'); |
|
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function orderColumns($order) |
|
237
|
|
|
{ |
|
238
|
|
|
$this->setSort('columns', (array) $order); |
|
|
|
|
|
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
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: