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|array $field The new field. |
15
|
|
|
* @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
16
|
|
|
* |
17
|
|
|
* @return self |
18
|
|
|
*/ |
19
|
68 |
|
public function addField($field, $form = 'both') |
20
|
|
|
{ |
21
|
|
|
// if the field_definition_array array is a string, it means the programmer was lazy and has only passed the name |
22
|
|
|
// set some default values, so the field will still work |
23
|
68 |
|
if (is_string($field)) { |
24
|
4 |
|
$completeFieldsArray['name'] = $field; |
|
|
|
|
25
|
|
|
} else { |
26
|
64 |
|
$completeFieldsArray = $field; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// if this is a relation type field and no corresponding model was specified, get it from the relation method |
30
|
|
|
// defined in the main model |
31
|
68 |
|
if (isset($completeFieldsArray['entity']) && ! isset($completeFieldsArray['model'])) { |
32
|
8 |
|
$completeFieldsArray['model'] = $this->getRelationModel($completeFieldsArray['entity']); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// if the label is missing, we should set it |
36
|
68 |
|
if (! isset($completeFieldsArray['label'])) { |
37
|
50 |
|
$completeFieldsArray['label'] = mb_ucfirst(str_replace('_', ' ', $completeFieldsArray['name'])); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// if the field type is missing, we should set it |
41
|
67 |
|
if (! isset($completeFieldsArray['type'])) { |
42
|
63 |
|
$completeFieldsArray['type'] = $this->getFieldTypeFromDbColumnType($completeFieldsArray['name']); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// if a tab was mentioned, we should enable it |
46
|
67 |
|
if (isset($completeFieldsArray['tab'])) { |
47
|
7 |
|
if (! $this->tabsEnabled()) { |
|
|
|
|
48
|
7 |
|
$this->enableTabs(); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->transformFields($form, function ($fields) use ($completeFieldsArray) { |
53
|
67 |
|
$fields[$completeFieldsArray['name']] = $completeFieldsArray; |
54
|
|
|
|
55
|
67 |
|
return $fields; |
56
|
67 |
|
}); |
57
|
|
|
|
58
|
67 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Add multiple fields to the create/update form or both. |
63
|
|
|
* |
64
|
|
|
* @param array $fields The new fields. |
65
|
|
|
* @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
66
|
|
|
*/ |
67
|
62 |
|
public function addFields($fields, $form = 'both') |
68
|
|
|
{ |
69
|
62 |
|
if (count($fields)) { |
70
|
62 |
|
foreach ($fields as $field) { |
71
|
62 |
|
$this->addField($field, $form); |
72
|
|
|
} |
73
|
|
|
} |
74
|
61 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Move the most recently added field after the given target field. |
78
|
|
|
* |
79
|
|
|
* @param string $targetFieldName The target field name. |
80
|
|
|
* @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
81
|
|
|
*/ |
82
|
6 |
|
public function afterField($targetFieldName, $form = 'both') |
83
|
|
|
{ |
84
|
|
|
$this->transformFields($form, function ($fields) use ($targetFieldName) { |
85
|
6 |
|
return $this->moveField($fields, $targetFieldName, false); |
86
|
6 |
|
}); |
87
|
6 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Move the most recently added field before the given target field. |
91
|
|
|
* |
92
|
|
|
* @param string $targetFieldName The target field name. |
93
|
|
|
* @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
94
|
|
|
*/ |
95
|
7 |
|
public function beforeField($targetFieldName, $form = 'both') |
96
|
|
|
{ |
97
|
|
|
$this->transformFields($form, function ($fields) use ($targetFieldName) { |
98
|
7 |
|
return $this->moveField($fields, $targetFieldName, true); |
99
|
7 |
|
}); |
100
|
7 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Move the most recently added field before or after the given target field. Default is before. |
104
|
|
|
* |
105
|
|
|
* @param array $fields The form fields. |
106
|
|
|
* @param string $targetFieldName The target field name. |
107
|
|
|
* @param bool $before If true, the field will be moved before the target field, otherwise it will be moved after it. |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
13 |
|
private function moveField($fields, $targetFieldName, $before = true) |
112
|
|
|
{ |
113
|
13 |
|
if (array_key_exists($targetFieldName, $fields)) { |
114
|
11 |
|
$targetFieldPosition = $before ? array_search($targetFieldName, array_keys($fields)) |
115
|
11 |
|
: array_search($targetFieldName, array_keys($fields)) + 1; |
116
|
|
|
|
117
|
11 |
|
if ($targetFieldPosition >= (count($fields) - 1)) { |
118
|
|
|
// target field name is same as element |
119
|
3 |
|
return $fields; |
120
|
|
|
} |
121
|
|
|
|
122
|
9 |
|
$element = array_pop($fields); |
123
|
9 |
|
$beginningArrayPart = array_slice($fields, 0, $targetFieldPosition, true); |
124
|
9 |
|
$endingArrayPart = array_slice($fields, $targetFieldPosition, null, true); |
125
|
|
|
|
126
|
9 |
|
$fields = array_merge($beginningArrayPart, [$element['name'] => $element], $endingArrayPart); |
127
|
|
|
} |
128
|
|
|
|
129
|
11 |
|
return $fields; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Remove a certain field from the create/update/both forms by its name. |
134
|
|
|
* |
135
|
|
|
* @param string $name Field name (as defined with the addField() procedure) |
136
|
|
|
* @param string $form update/create/both |
137
|
|
|
*/ |
138
|
4 |
|
public function removeField($name, $form = 'both') |
139
|
|
|
{ |
140
|
|
|
$this->transformFields($form, function ($fields) use ($name) { |
141
|
4 |
|
array_forget($fields, $name); |
|
|
|
|
142
|
|
|
|
143
|
4 |
|
return $fields; |
144
|
4 |
|
}); |
145
|
4 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Remove many fields from the create/update/both forms by their name. |
149
|
|
|
* |
150
|
|
|
* @param array $array_of_names A simple array of the names of the fields to be removed. |
151
|
|
|
* @param string $form update/create/both |
152
|
|
|
*/ |
153
|
4 |
|
public function removeFields($array_of_names, $form = 'both') |
154
|
|
|
{ |
155
|
4 |
|
if (! empty($array_of_names)) { |
156
|
4 |
|
foreach ($array_of_names as $name) { |
157
|
4 |
|
$this->removeField($name, $form); |
158
|
|
|
} |
159
|
|
|
} |
160
|
4 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Remove all fields from the create/update/both forms. |
164
|
|
|
* |
165
|
|
|
* @param string $form update/create/both |
166
|
|
|
*/ |
167
|
|
|
public function removeAllFields($form = 'both') |
168
|
|
|
{ |
169
|
|
|
$current_fields = $this->getCurrentFields(); |
170
|
|
|
if (! empty($current_fields)) { |
171
|
|
|
foreach ($current_fields as $field) { |
172
|
|
|
$this->removeField($field['name'], $form); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Update value of a given key for a current field. |
179
|
|
|
* |
180
|
|
|
* @param string $field The field |
181
|
|
|
* @param array $modifications An array of changes to be made. |
182
|
|
|
* @param string $form update/create/both |
183
|
|
|
*/ |
184
|
|
|
public function modifyField($field, $modifications, $form = 'both') |
185
|
|
|
{ |
186
|
|
|
foreach ($modifications as $key => $newValue) { |
187
|
|
|
switch (strtolower($form)) { |
188
|
|
|
case 'create': |
189
|
|
|
$this->create_fields[$field][$key] = $newValue; |
|
|
|
|
190
|
|
|
break; |
191
|
|
|
|
192
|
|
|
case 'update': |
193
|
|
|
$this->update_fields[$field][$key] = $newValue; |
|
|
|
|
194
|
|
|
break; |
195
|
|
|
|
196
|
|
|
default: |
197
|
|
|
$this->create_fields[$field][$key] = $newValue; |
198
|
|
|
$this->update_fields[$field][$key] = $newValue; |
199
|
|
|
break; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set label for a specific field. |
206
|
|
|
* |
207
|
|
|
* @param string $field |
208
|
|
|
* @param string $label |
209
|
|
|
*/ |
210
|
|
|
public function setFieldLabel($field, $label) |
211
|
|
|
{ |
212
|
|
|
if (isset($this->create_fields[$field])) { |
213
|
|
|
$this->create_fields[$field]['label'] = $label; |
214
|
|
|
} |
215
|
|
|
if (isset($this->update_fields[$field])) { |
216
|
|
|
$this->update_fields[$field]['label'] = $label; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Check if field is the first of its type in the given fields array. |
222
|
|
|
* 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). |
223
|
|
|
* |
224
|
|
|
* @param array $field The current field being tested if it's the first of its type. |
225
|
|
|
* |
226
|
|
|
* @return bool true/false |
227
|
|
|
*/ |
228
|
3 |
|
public function checkIfFieldIsFirstOfItsType($field) |
229
|
|
|
{ |
230
|
3 |
|
$fields_array = $this->getCurrentFields(); |
231
|
3 |
|
$first_field = $this->getFirstOfItsTypeInArray($field['type'], $fields_array); |
|
|
|
|
232
|
|
|
|
233
|
2 |
|
if ($field['name'] == $first_field['name']) { |
|
|
|
|
234
|
1 |
|
return true; |
235
|
|
|
} |
236
|
|
|
|
237
|
2 |
|
return false; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Decode attributes that are casted as array/object/json in the model. |
242
|
|
|
* So that they are not json_encoded twice before they are stored in the db |
243
|
|
|
* (once by Backpack in front-end, once by Laravel Attribute Casting). |
244
|
|
|
*/ |
245
|
6 |
|
public function decodeJsonCastedAttributes($data, $form, $id = false) |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
// get the right fields according to the form type (create/update) |
248
|
6 |
|
$fields = $this->getFields($form, $id); |
|
|
|
|
249
|
5 |
|
$casted_attributes = $this->model->getCastedAttributes(); |
|
|
|
|
250
|
|
|
|
251
|
5 |
|
foreach ($fields as $field) { |
252
|
|
|
|
253
|
|
|
// Test the field is castable |
254
|
5 |
|
if (isset($field['name']) && array_key_exists($field['name'], $casted_attributes)) { |
255
|
|
|
|
256
|
|
|
// Handle JSON field types |
257
|
5 |
|
$jsonCastables = ['array', 'object', 'json']; |
258
|
5 |
|
$fieldCasting = $casted_attributes[$field['name']]; |
259
|
|
|
|
260
|
5 |
|
if (in_array($fieldCasting, $jsonCastables) && isset($data[$field['name']]) && ! empty($data[$field['name']]) && ! is_array($data[$field['name']])) { |
261
|
|
|
try { |
262
|
|
|
$data[$field['name']] = json_decode($data[$field['name']]); |
263
|
|
|
} catch (\Exception $e) { |
264
|
|
|
$data[$field['name']] = []; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
5 |
|
return $data; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return array |
275
|
|
|
*/ |
276
|
12 |
|
public function getCurrentFields() |
277
|
|
|
{ |
278
|
12 |
|
if ($this->entry) { |
279
|
1 |
|
return $this->getUpdateFields($this->entry->getKey()); |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
11 |
|
return $this->getCreateFields(); |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Order the CRUD fields in the given form. If certain fields are missing from the given order array, they will be |
287
|
|
|
* pushed to the new fields array in the original order. |
288
|
|
|
* |
289
|
|
|
* @param array $order An array of field names in the desired order. |
290
|
|
|
* @param string $form The CRUD form. Can be 'create', 'update' or 'both'. |
291
|
|
|
*/ |
292
|
7 |
|
public function orderFields($order, $form = 'both') |
293
|
|
|
{ |
294
|
|
|
$this->transformFields($form, function ($fields) use ($order) { |
295
|
7 |
|
return $this->applyOrderToFields($fields, $order); |
296
|
7 |
|
}); |
297
|
7 |
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Apply the given order to the fields and return the new array. |
301
|
|
|
* |
302
|
|
|
* @param array $fields The fields array. |
303
|
|
|
* @param array $order The desired field order array. |
304
|
|
|
* |
305
|
|
|
* @return array The ordered fields array. |
306
|
|
|
*/ |
307
|
7 |
|
private function applyOrderToFields($fields, $order) |
308
|
|
|
{ |
309
|
7 |
|
$orderedFields = []; |
310
|
7 |
|
foreach ($order as $fieldName) { |
311
|
6 |
|
if (array_key_exists($fieldName, $fields)) { |
312
|
5 |
|
$orderedFields[$fieldName] = $fields[$fieldName]; |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
7 |
|
if (empty($orderedFields)) { |
317
|
2 |
|
return $fields; |
318
|
|
|
} |
319
|
|
|
|
320
|
5 |
|
$remaining = array_diff_key($fields, $orderedFields); |
321
|
|
|
|
322
|
5 |
|
return array_merge($orderedFields, $remaining); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Set the order of the CRUD fields. |
327
|
|
|
* |
328
|
|
|
* @param array $fields Fields order. |
329
|
|
|
* |
330
|
|
|
* @deprecated This method was not and will not be implemented since its a duplicate of the orderFields method. |
331
|
|
|
* @see Fields::orderFields() to order the CRUD fields. |
332
|
|
|
*/ |
333
|
|
|
public function setFieldOrder($fields) |
|
|
|
|
334
|
|
|
{ |
335
|
|
|
// not implemented |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Set the order of the CRUD fields. |
340
|
|
|
* |
341
|
|
|
* @param array $fields Fields order. |
342
|
|
|
* |
343
|
|
|
* @deprecated This method was not and will not be implemented since its a duplicate of the orderFields method. |
344
|
|
|
* @see Fields::orderFields() to order the CRUD fields. |
345
|
|
|
*/ |
346
|
|
|
public function setFieldsOrder($fields) |
347
|
|
|
{ |
348
|
|
|
$this->setFieldOrder($fields); |
|
|
|
|
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Apply the given callback to the form fields. |
353
|
|
|
* |
354
|
|
|
* @param string $form The CRUD form. Can be 'create', 'update' or 'both'. |
355
|
|
|
* @param callable $callback The callback function to run for the given form fields. |
356
|
|
|
*/ |
357
|
67 |
|
private function transformFields($form, callable $callback) |
358
|
|
|
{ |
359
|
67 |
|
switch (strtolower($form)) { |
360
|
67 |
|
case 'create': |
361
|
14 |
|
$this->create_fields = $callback($this->create_fields); |
362
|
14 |
|
break; |
363
|
|
|
|
364
|
59 |
|
case 'update': |
365
|
15 |
|
$this->update_fields = $callback($this->update_fields); |
366
|
15 |
|
break; |
367
|
|
|
|
368
|
|
|
default: |
369
|
50 |
|
$this->create_fields = $callback($this->create_fields); |
370
|
50 |
|
$this->update_fields = $callback($this->update_fields); |
371
|
50 |
|
break; |
372
|
|
|
} |
373
|
67 |
|
} |
374
|
|
|
} |
375
|
|
|
|
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.