We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
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 | 14 | public function addColumn($column) |
|
55 | { |
||
56 | // if a string was passed, not an array, change it to an array |
||
57 | 14 | if (! is_array($column)) { |
|
58 | 6 | $column = ['name' => $column]; |
|
59 | 6 | } |
|
60 | |||
61 | // make sure the column has a label |
||
62 | 14 | $column_with_details = $this->addDefaultLabel($column); |
|
63 | |||
64 | // make sure the column has a name |
||
65 | 14 | if (! array_key_exists('name', $column_with_details)) { |
|
66 | $column_with_details['name'] = 'anonymous_column_'.str_random(5); |
||
67 | } |
||
68 | |||
69 | 14 | array_filter($this->columns[$column_with_details['name']] = $column_with_details); |
|
70 | |||
71 | // if this is a relation type field and no corresponding model was specified, get it from the relation method |
||
72 | // defined in the main model |
||
73 | 14 | if (isset($column_with_details['entity']) && ! isset($column_with_details['model'])) { |
|
74 | 1 | $column_with_details['model'] = $this->getRelationModel($column_with_details['entity']); |
|
75 | 1 | } |
|
76 | |||
77 | 14 | return $this; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Add multiple columns at the end of the CRUD object's "columns" array. |
||
82 | * |
||
83 | * @param [array of columns] |
||
84 | */ |
||
85 | 11 | public function addColumns($columns) |
|
86 | { |
||
87 | 11 | if (count($columns)) { |
|
88 | 11 | foreach ($columns as $key => $column) { |
|
89 | 10 | $this->addColumn($column); |
|
90 | 10 | } |
|
91 | 10 | } |
|
92 | 10 | } |
|
93 | |||
94 | /** |
||
95 | * Move the most recently added column after the given target column. |
||
96 | * |
||
97 | * @param string|array $targetColumn The target column name or array. |
||
98 | */ |
||
99 | 2 | public function afterColumn($targetColumn) |
|
103 | |||
104 | /** |
||
105 | * Move the most recently added column before the given target column. |
||
106 | * |
||
107 | * @param string|array $targetColumn The target column name or array. |
||
108 | */ |
||
109 | 2 | public function beforeColumn($targetColumn) |
|
113 | |||
114 | /** |
||
115 | * Move the most recently added column before or after the given target column. Default is before. |
||
116 | * |
||
117 | * @param string|array $targetColumn The target column name or array. |
||
118 | * @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it. |
||
119 | */ |
||
120 | 4 | private function moveColumn($targetColumn, $before = true) |
|
121 | { |
||
122 | // TODO: this and the moveField method from the Fields trait should be refactored into a single method and moved |
||
123 | // into a common class |
||
124 | 4 | $targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn; |
|
125 | |||
126 | 4 | if (array_key_exists($targetColumnName, $this->columns)) { |
|
127 | 2 | $targetColumnPosition = $before ? array_search($targetColumnName, array_keys($this->columns)) : |
|
128 | 2 | array_search($targetColumnName, array_keys($this->columns)) + 1; |
|
129 | |||
130 | 2 | $element = array_pop($this->columns); |
|
131 | 2 | $beginningPart = array_slice($this->columns, 0, $targetColumnPosition, true); |
|
132 | 2 | $endingArrayPart = array_slice($this->columns, $targetColumnPosition, null, true); |
|
133 | |||
134 | 2 | $this->columns = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart); |
|
135 | 2 | } |
|
136 | 4 | } |
|
137 | |||
138 | /** |
||
139 | * Add the default column type to the given Column, inferring the type from the database column type. |
||
140 | * |
||
141 | * @param [column array] |
||
142 | */ |
||
143 | public function addDefaultTypeToColumn($column) |
||
153 | |||
154 | /** |
||
155 | * If a field or column array is missing the "label" attribute, an ugly error would be show. |
||
156 | * So we add the field Name as a label - it's better than nothing. |
||
157 | * |
||
158 | * @param [field or column] |
||
159 | */ |
||
160 | 14 | public function addDefaultLabel($array) |
|
170 | |||
171 | /** |
||
172 | * Remove a column from the CRUD panel by name. |
||
173 | * |
||
174 | * @param string $column The column name. |
||
175 | */ |
||
176 | 4 | public function removeColumn($column) |
|
180 | |||
181 | /** |
||
182 | * Remove multiple columns from the CRUD panel by name. |
||
183 | * |
||
184 | * @param array $columns Array of column names. |
||
185 | */ |
||
186 | 2 | public function removeColumns($columns) |
|
187 | { |
||
188 | 2 | if (! empty($columns)) { |
|
189 | 2 | foreach ($columns as $columnName) { |
|
190 | 2 | $this->removeColumn($columnName); |
|
191 | 2 | } |
|
192 | 2 | } |
|
193 | 2 | } |
|
194 | |||
195 | /** |
||
196 | * Remove an entry from an array. |
||
197 | * |
||
198 | * @param string $entity |
||
199 | * @param array $fields |
||
200 | * @return array values |
||
201 | * |
||
202 | * @deprecated This method is no longer used by internal code and is not recommended as it does not preserve the |
||
203 | * target array keys. |
||
204 | * @see Columns::removeColumn() to remove a column from the CRUD panel by name. |
||
205 | * @see Columns::removeColumns() to remove multiple columns from the CRUD panel by name. |
||
206 | */ |
||
207 | public function remove($entity, $fields) |
||
208 | { |
||
209 | return array_values(array_filter($this->{$entity}, function ($field) use ($fields) { |
||
210 | return ! in_array($field['name'], (array) $fields); |
||
211 | })); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Change attributes for multiple columns. |
||
216 | * |
||
217 | * @param [columns arrays] |
||
218 | * @param [attributes and values array] |
||
219 | */ |
||
220 | public function setColumnsDetails($columns, $attributes) |
||
224 | |||
225 | /** |
||
226 | * Change attributes for a certain column. |
||
227 | * |
||
228 | * @param [string] Column name. |
||
229 | * @param [attributes and values array] |
||
230 | */ |
||
231 | public function setColumnDetails($column, $attributes) |
||
235 | |||
236 | /** |
||
237 | * Set label for a specific column. |
||
238 | * |
||
239 | * @param string $column |
||
240 | * @param string $label |
||
241 | */ |
||
242 | public function setColumnLabel($column, $label) |
||
246 | |||
247 | /** |
||
248 | * Order the columns in a certain way. |
||
249 | * |
||
250 | * @param [string] Column name. |
||
251 | * @param [attributes and values array] |
||
252 | */ |
||
253 | public function setColumnOrder($columns) |
||
257 | |||
258 | // ALIAS of setColumnOrder($columns) |
||
259 | public function setColumnsOrder($columns) |
||
263 | |||
264 | /** |
||
265 | * Get the relationships used in the CRUD columns. |
||
266 | * @return [array] Relationship names |
||
267 | */ |
||
268 | 3 | public function getColumnsRelationships() |
|
276 | |||
277 | // ------------ |
||
278 | // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED |
||
279 | // ------------ |
||
280 | // TODO: check them |
||
281 | |||
282 | 3 | public function getColumns() |
|
286 | |||
287 | public function orderColumns($order) |
||
291 | } |
||
292 |
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: