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 type |
62
|
|
|
$column_with_details = $this->addDefaultTypeToColumn($column); |
|
|
|
|
63
|
|
|
|
64
|
|
|
// make sure the column has a label |
65
|
|
|
$column_with_details = $this->addDefaultLabel($column); |
66
|
|
|
|
67
|
|
|
return array_filter($this->columns[] = $column_with_details); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add multiple columns at the end of the CRUD object's "columns" array. |
72
|
|
|
* |
73
|
|
|
* @param [array of columns] |
74
|
|
|
*/ |
75
|
|
|
public function addColumns($columns) |
76
|
|
|
{ |
77
|
|
|
if (count($columns)) { |
78
|
|
|
foreach ($columns as $key => $column) { |
79
|
|
|
$this->addColumn($column); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Add the default column type to the given Column, inferring the type from the database column type. |
86
|
|
|
* |
87
|
|
|
* @param [column array] |
88
|
|
|
*/ |
89
|
|
|
public function addDefaultTypeToColumn($column) |
90
|
|
|
{ |
91
|
|
|
if (array_key_exists('name', (array) $column)) { |
92
|
|
|
$default_type = $this->getFieldTypeFromDbColumnType($column['name']); |
|
|
|
|
93
|
|
|
|
94
|
|
|
return array_merge(['type' => $default_type], $column); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* If a field or column array is missing the "label" attribute, an ugly error would be show. |
102
|
|
|
* So we add the field Name as a label - it's better than nothing. |
103
|
|
|
* |
104
|
|
|
* @param [field or column] |
105
|
|
|
*/ |
106
|
|
|
public function addDefaultLabel($array) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
if (! array_key_exists('label', (array) $array) && array_key_exists('name', (array) $array)) { |
109
|
|
|
$array = array_merge(['label' => ucfirst($this->makeLabel($array['name']))], $array); |
|
|
|
|
110
|
|
|
|
111
|
|
|
return $array; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $array; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Remove multiple columns from the CRUD object using their names. |
119
|
|
|
* |
120
|
|
|
* @param [column array] |
121
|
|
|
*/ |
122
|
|
|
public function removeColumns($columns) |
123
|
|
|
{ |
124
|
|
|
$this->columns = $this->remove('columns', $columns); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Remove a column from the CRUD object using its name. |
129
|
|
|
* |
130
|
|
|
* @param [column array] |
131
|
|
|
*/ |
132
|
|
|
public function removeColumn($column) |
133
|
|
|
{ |
134
|
|
|
return $this->removeColumns([$column]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $entity |
139
|
|
|
*/ |
140
|
|
|
public function remove($entity, $fields) |
141
|
|
|
{ |
142
|
|
|
return array_values(array_filter($this->{$entity}, function ($field) use ($fields) { |
143
|
|
|
return ! in_array($field['name'], (array) $fields); |
144
|
|
|
})); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Change attributes for multiple columns. |
149
|
|
|
* |
150
|
|
|
* @param [columns arrays] |
151
|
|
|
* @param [attributes and values array] |
152
|
|
|
*/ |
153
|
|
|
public function setColumnsDetails($columns, $attributes) |
154
|
|
|
{ |
155
|
|
|
$this->sync('columns', $columns, $attributes); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Change attributes for a certain column. |
160
|
|
|
* |
161
|
|
|
* @param [string] Column name. |
162
|
|
|
* @param [attributes and values array] |
163
|
|
|
*/ |
164
|
|
|
public function setColumnDetails($column, $attributes) |
165
|
|
|
{ |
166
|
|
|
$this->setColumnsDetails([$column], $attributes); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Order the columns in a certain way. |
171
|
|
|
* |
172
|
|
|
* @param [string] Column name. |
173
|
|
|
* @param [attributes and values array] |
174
|
|
|
*/ |
175
|
|
|
public function setColumnOrder($columns) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
// TODO |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// ALIAS of setColumnOrder($columns) |
181
|
|
|
public function setColumnsOrder($columns) |
182
|
|
|
{ |
183
|
|
|
$this->setColumnOrder($columns); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get the relationships used in the CRUD columns. |
188
|
|
|
* @return [array] Relationship names |
|
|
|
|
189
|
|
|
*/ |
190
|
|
|
public function getColumnsRelationships() |
191
|
|
|
{ |
192
|
|
|
$columns = $this->getColumns(); |
193
|
|
|
|
194
|
|
|
return collect($columns)->pluck('entity')->reject(function($value, $key) { |
|
|
|
|
195
|
|
|
return $value == null; |
196
|
|
|
})->toArray(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// ------------ |
200
|
|
|
// TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED |
201
|
|
|
// ------------ |
202
|
|
|
// TODO: check them |
203
|
|
|
|
204
|
|
|
public function getColumns() |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
return $this->sort('columns'); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function orderColumns($order) |
210
|
|
|
{ |
211
|
|
|
$this->setSort('columns', (array) $order); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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: