1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
trait Fields |
6
|
|
|
{ |
7
|
|
|
// ------------ |
8
|
|
|
// FIELDS |
9
|
|
|
// ------------ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Add a field to the create/update form or both. |
13
|
|
|
* |
14
|
|
|
* @param string $form The form to add the field to (create/update/both) |
15
|
|
|
*/ |
16
|
|
|
public function addField($field, $form = 'both') |
17
|
|
|
{ |
18
|
|
|
// if the field_defition_array array is a string, it means the programmer was lazy and has only passed the name |
19
|
|
|
// set some default values, so the field will still work |
20
|
|
|
if (is_string($field)) { |
21
|
|
|
$complete_field_array['name'] = $field; |
|
|
|
|
22
|
|
|
} else { |
23
|
|
|
$complete_field_array = $field; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// if the label is missing, we should set it |
27
|
|
|
if (! isset($complete_field_array['label'])) { |
28
|
|
|
$complete_field_array['label'] = ucfirst($complete_field_array['name']); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// if the field type is missing, we should set it |
32
|
|
|
if (! isset($complete_field_array['type'])) { |
33
|
|
|
$complete_field_array['type'] = $this->getFieldTypeFromDbColumnType($complete_field_array['name']); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// if a tab was mentioned, we should enable it |
37
|
|
|
if (isset($complete_field_array['tab'])) { |
38
|
|
|
if (! $this->tabsEnabled()) { |
|
|
|
|
39
|
|
|
$this->enableTabs(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// store the field information into the correct variable on the CRUD object |
44
|
|
|
switch (strtolower($form)) { |
45
|
|
|
case 'create': |
46
|
|
|
$this->create_fields[$complete_field_array['name']] = $complete_field_array; |
|
|
|
|
47
|
|
|
break; |
48
|
|
|
|
49
|
|
|
case 'update': |
50
|
|
|
$this->update_fields[$complete_field_array['name']] = $complete_field_array; |
|
|
|
|
51
|
|
|
break; |
52
|
|
|
|
53
|
|
|
default: |
54
|
|
|
$this->create_fields[$complete_field_array['name']] = $complete_field_array; |
55
|
|
|
$this->update_fields[$complete_field_array['name']] = $complete_field_array; |
56
|
|
|
break; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function addFields($fields, $form = 'both') |
63
|
|
|
{ |
64
|
|
|
if (count($fields)) { |
65
|
|
|
foreach ($fields as $field) { |
66
|
|
|
$this->addField($field, $form); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Moves the recently added field to 'after' the $target_field. |
73
|
|
|
* |
74
|
|
|
* @param $target_field |
75
|
|
|
*/ |
76
|
|
|
public function afterField($target_field) |
|
|
|
|
77
|
|
|
{ |
78
|
|
View Code Duplication |
foreach ($this->create_fields as $field => $value) { |
|
|
|
|
79
|
|
|
if ($value['name'] == $target_field) { |
80
|
|
|
array_splice($this->create_fields, $field + 1, 0, [$field => array_pop($this->create_fields)]); |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
View Code Duplication |
foreach ($this->update_fields as $field => $value) { |
|
|
|
|
85
|
|
|
if ($value['name'] == $target_field) { |
86
|
|
|
array_splice($this->update_fields, $field + 1, 0, [$field => array_pop($this->update_fields)]); |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Moves the recently added field to 'before' the $target_field. |
94
|
|
|
* |
95
|
|
|
* @param $target_field |
96
|
|
|
*/ |
97
|
|
|
public function beforeField($target_field) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$key = 0; |
100
|
|
View Code Duplication |
foreach ($this->create_fields as $field => $value) { |
|
|
|
|
101
|
|
|
if ($value['name'] == $target_field) { |
102
|
|
|
array_splice($this->create_fields, $key, 0, [$field => array_pop($this->create_fields)]); |
103
|
|
|
break; |
104
|
|
|
} |
105
|
|
|
$key++; |
106
|
|
|
} |
107
|
|
|
$key = 0; |
108
|
|
View Code Duplication |
foreach ($this->update_fields as $field => $value) { |
|
|
|
|
109
|
|
|
if ($value['name'] == $target_field) { |
110
|
|
|
array_splice($this->update_fields, $key, 0, [$field => array_pop($this->update_fields)]); |
111
|
|
|
break; |
112
|
|
|
} |
113
|
|
|
$key++; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Remove a certain field from the create/update/both forms by its name. |
119
|
|
|
* |
120
|
|
|
* @param string $name Field name (as defined with the addField() procedure) |
121
|
|
|
* @param string $form update/create/both |
122
|
|
|
*/ |
123
|
|
|
public function removeField($name, $form = 'both') |
124
|
|
|
{ |
125
|
|
|
switch (strtolower($form)) { |
126
|
|
|
case 'create': |
127
|
|
|
array_forget($this->create_fields, $name); |
128
|
|
|
break; |
129
|
|
|
|
130
|
|
|
case 'update': |
131
|
|
|
array_forget($this->update_fields, $name); |
132
|
|
|
break; |
133
|
|
|
|
134
|
|
|
default: |
135
|
|
|
array_forget($this->create_fields, $name); |
136
|
|
|
array_forget($this->update_fields, $name); |
137
|
|
|
break; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Remove many fields from the create/update/both forms by their name. |
143
|
|
|
* |
144
|
|
|
* @param array $array_of_names A simple array of the names of the fields to be removed. |
145
|
|
|
* @param string $form update/create/both |
146
|
|
|
*/ |
147
|
|
|
public function removeFields($array_of_names, $form = 'both') |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
if (! empty($array_of_names)) { |
150
|
|
|
foreach ($array_of_names as $name) { |
151
|
|
|
$this->removeField($name, $form); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Check if field is the first of its type in the given fields array. |
158
|
|
|
* It's used in each field_type.blade.php to determine wether to push the css and js content or not (we only need to push the js and css for a field the first time it's loaded in the form, not any subsequent times). |
|
|
|
|
159
|
|
|
* |
160
|
|
|
* @param array $field The current field being tested if it's the first of its type. |
161
|
|
|
* @param array $fields_array All the fields in that particular form. |
162
|
|
|
* |
163
|
|
|
* @return bool true/false |
164
|
|
|
*/ |
165
|
|
|
public function checkIfFieldIsFirstOfItsType($field, $fields_array) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
if ($field['name'] == $this->getFirstOfItsTypeInArray($field['type'], $fields_array)['name']) { |
|
|
|
|
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Order the fields in a certain way. |
176
|
|
|
* |
177
|
|
|
* @param [string] Column name. |
178
|
|
|
* @param [attributes and values array] |
179
|
|
|
*/ |
180
|
|
|
public function setFieldOrder($fields) |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
// TODO |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// ALIAS of setFieldOrder($fields) |
186
|
|
|
public function setFieldsOrder($fields) |
187
|
|
|
{ |
188
|
|
|
$this->setFieldOrder($fields); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Decode attributes that are casted as array/object/json in the model. |
193
|
|
|
* So that they are not json_encoded twice before they are stored in the db |
194
|
|
|
* (once by Backpack in front-end, once by Laravel Attribute Casting). |
195
|
|
|
*/ |
196
|
|
|
public function decodeJsonCastedAttributes($data, $form, $id = false) |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
// get the right fields according to the form type (create/update) |
199
|
|
|
$fields = $this->getFields($form, $id); |
|
|
|
|
200
|
|
|
$casted_attributes = $this->model->getCastedAttributes(); |
|
|
|
|
201
|
|
|
|
202
|
|
|
foreach ($fields as $field) { |
203
|
|
|
|
204
|
|
|
// Test the field is castable |
205
|
|
|
if (isset($field['name']) && array_key_exists($field['name'], $casted_attributes)) { |
206
|
|
|
|
207
|
|
|
// Handle JSON field types |
208
|
|
|
$jsonCastables = ['array', 'object', 'json']; |
209
|
|
|
$fieldCasting = $casted_attributes[$field['name']]; |
210
|
|
|
|
211
|
|
|
if (in_array($fieldCasting, $jsonCastables) && isset($data[$field['name']]) && ! empty($data[$field['name']]) && ! is_array($data[$field['name']])) { |
|
|
|
|
212
|
|
|
try { |
213
|
|
|
$data[$field['name']] = json_decode($data[$field['name']]); |
214
|
|
|
} catch (Exception $e) { |
|
|
|
|
215
|
|
|
$data[$field['name']] = []; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $data; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function getCurrentFields() |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
if ($this->entry) { |
227
|
|
|
return $this->getUpdateFields($this->entry->getKey()); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $this->getCreateFields(); |
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// ------------ |
234
|
|
|
// TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED |
235
|
|
|
// ------------ |
236
|
|
|
// TODO: check them |
237
|
|
|
|
238
|
|
|
public function orderFields($order) |
239
|
|
|
{ |
240
|
|
|
$this->setSort('fields', (array) $order); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.