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