|
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_definition_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 this is a relation type field and no corresponding model was specified, get it from the relation method |
|
27
|
|
|
// defined in the main model |
|
28
|
|
|
if (isset($complete_field_array['entity']) && ! isset($complete_field_array['model'])) { |
|
29
|
|
|
$complete_field_array['model'] = $this->getRelationModel($complete_field_array['entity']); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// if the label is missing, we should set it |
|
33
|
|
|
if (! isset($complete_field_array['label'])) { |
|
34
|
|
|
$complete_field_array['label'] = ucfirst($complete_field_array['name']); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// if the field type is missing, we should set it |
|
38
|
|
|
if (! isset($complete_field_array['type'])) { |
|
39
|
|
|
$complete_field_array['type'] = $this->getFieldTypeFromDbColumnType($complete_field_array['name']); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// if a tab was mentioned, we should enable it |
|
43
|
|
|
if (isset($complete_field_array['tab'])) { |
|
44
|
|
|
if (! $this->tabsEnabled()) { |
|
|
|
|
|
|
45
|
|
|
$this->enableTabs(); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// store the field information into the correct variable on the CRUD object |
|
50
|
|
|
switch (strtolower($form)) { |
|
51
|
|
|
case 'create': |
|
52
|
|
|
$this->create_fields[$complete_field_array['name']] = $complete_field_array; |
|
|
|
|
|
|
53
|
|
|
break; |
|
54
|
|
|
|
|
55
|
|
|
case 'update': |
|
56
|
|
|
$this->update_fields[$complete_field_array['name']] = $complete_field_array; |
|
|
|
|
|
|
57
|
|
|
break; |
|
58
|
|
|
|
|
59
|
|
|
default: |
|
60
|
|
|
$this->create_fields[$complete_field_array['name']] = $complete_field_array; |
|
61
|
|
|
$this->update_fields[$complete_field_array['name']] = $complete_field_array; |
|
62
|
|
|
break; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function addFields($fields, $form = 'both') |
|
69
|
|
|
{ |
|
70
|
|
|
if (count($fields)) { |
|
71
|
|
|
foreach ($fields as $field) { |
|
72
|
|
|
$this->addField($field, $form); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Move the most recently added field after the given target field. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $targetFieldName The target field name. |
|
81
|
|
|
* @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function afterField($targetFieldName, $form = 'both') |
|
84
|
|
|
{ |
|
85
|
|
|
$this->moveFieldInForm($targetFieldName, $form, false); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Move the most recently added field before the given target field. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $targetFieldName The target field name. |
|
92
|
|
|
* @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function beforeField($targetFieldName, $form = 'both') |
|
95
|
|
|
{ |
|
96
|
|
|
$this->moveFieldInForm($targetFieldName, $form); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Move the most recently added field from a given form before or after the given target field. Default is before. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $targetFieldName The target field name. |
|
103
|
|
|
* @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
|
104
|
|
|
* @param bool $before If true, the field will be moved before the target field, otherwise it will be moved after it. |
|
|
|
|
|
|
105
|
|
|
*/ |
|
106
|
|
|
protected function moveFieldInForm($targetFieldName, $form = 'both', $before = true) |
|
107
|
|
|
{ |
|
108
|
|
|
switch ($form) { |
|
109
|
|
|
case 'create': |
|
110
|
|
|
$this->moveField($this->create_fields, $targetFieldName, $before); |
|
111
|
|
|
break; |
|
112
|
|
|
case 'update': |
|
113
|
|
|
$this->moveField($this->update_fields, $targetFieldName, $before); |
|
114
|
|
|
break; |
|
115
|
|
|
default: |
|
116
|
|
|
$this->moveField($this->create_fields, $targetFieldName, $before); |
|
117
|
|
|
$this->moveField($this->update_fields, $targetFieldName, $before); |
|
118
|
|
|
break; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Move the most recently added field before or after the given target field. Default is before. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $fields The form fields. |
|
126
|
|
|
* @param string $targetFieldName The target field name. |
|
127
|
|
|
* @param bool $before If true, the field will be moved before the target field, otherwise it will be moved after it. |
|
|
|
|
|
|
128
|
|
|
*/ |
|
129
|
|
|
private function moveField(&$fields, $targetFieldName, $before = true) |
|
130
|
|
|
{ |
|
131
|
|
|
if (array_key_exists($targetFieldName, $fields)) { |
|
132
|
|
|
$targetFieldPosition = $before ? array_search($targetFieldName, array_keys($fields)) |
|
133
|
|
|
: array_search($targetFieldName, array_keys($fields)) + 1; |
|
134
|
|
|
|
|
135
|
|
|
if ($targetFieldPosition >= (count($fields) - 1)) { |
|
136
|
|
|
// target field name is same as element |
|
137
|
|
|
return; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$element = array_pop($fields); |
|
141
|
|
|
$beginningArrayPart = array_slice($fields, 0, $targetFieldPosition, true); |
|
142
|
|
|
$endingArrayPart = array_slice($fields, $targetFieldPosition, null, true); |
|
143
|
|
|
|
|
144
|
|
|
$fields = array_merge($beginningArrayPart, [$element['name'] => $element], $endingArrayPart); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Remove a certain field from the create/update/both forms by its name. |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $name Field name (as defined with the addField() procedure) |
|
152
|
|
|
* @param string $form update/create/both |
|
153
|
|
|
*/ |
|
154
|
|
|
public function removeField($name, $form = 'both') |
|
155
|
|
|
{ |
|
156
|
|
|
switch (strtolower($form)) { |
|
157
|
|
|
case 'create': |
|
158
|
|
|
array_forget($this->create_fields, $name); |
|
159
|
|
|
break; |
|
160
|
|
|
|
|
161
|
|
|
case 'update': |
|
162
|
|
|
array_forget($this->update_fields, $name); |
|
163
|
|
|
break; |
|
164
|
|
|
|
|
165
|
|
|
default: |
|
166
|
|
|
array_forget($this->create_fields, $name); |
|
167
|
|
|
array_forget($this->update_fields, $name); |
|
168
|
|
|
break; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Remove many fields from the create/update/both forms by their name. |
|
174
|
|
|
* |
|
175
|
|
|
* @param array $array_of_names A simple array of the names of the fields to be removed. |
|
176
|
|
|
* @param string $form update/create/both |
|
177
|
|
|
*/ |
|
178
|
|
|
public function removeFields($array_of_names, $form = 'both') |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
if (! empty($array_of_names)) { |
|
181
|
|
|
foreach ($array_of_names as $name) { |
|
182
|
|
|
$this->removeField($name, $form); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Check if field is the first of its type in the given fields array. |
|
189
|
|
|
* 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). |
|
|
|
|
|
|
190
|
|
|
* |
|
191
|
|
|
* @param array $field The current field being tested if it's the first of its type. |
|
192
|
|
|
* @param array $fields_array All the fields in that particular form. |
|
193
|
|
|
* |
|
194
|
|
|
* @return bool true/false |
|
195
|
|
|
*/ |
|
196
|
|
|
public function checkIfFieldIsFirstOfItsType($field, $fields_array) |
|
|
|
|
|
|
197
|
|
|
{ |
|
198
|
|
|
$first_field = $this->getFirstOfItsTypeInArray($field['type'], $fields_array); |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
if ($field['name'] == $first_field['name']) { |
|
|
|
|
|
|
201
|
|
|
return true; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return false; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Order the fields in a certain way. |
|
209
|
|
|
* |
|
210
|
|
|
* @param [string] Column name. |
|
211
|
|
|
* @param [attributes and values array] |
|
212
|
|
|
*/ |
|
213
|
|
|
public function setFieldOrder($fields) |
|
|
|
|
|
|
214
|
|
|
{ |
|
215
|
|
|
// TODO |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
// ALIAS of setFieldOrder($fields) |
|
219
|
|
|
public function setFieldsOrder($fields) |
|
220
|
|
|
{ |
|
221
|
|
|
$this->setFieldOrder($fields); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Decode attributes that are casted as array/object/json in the model. |
|
226
|
|
|
* So that they are not json_encoded twice before they are stored in the db |
|
227
|
|
|
* (once by Backpack in front-end, once by Laravel Attribute Casting). |
|
228
|
|
|
*/ |
|
229
|
|
|
public function decodeJsonCastedAttributes($data, $form, $id = false) |
|
|
|
|
|
|
230
|
|
|
{ |
|
231
|
|
|
// get the right fields according to the form type (create/update) |
|
232
|
|
|
$fields = $this->getFields($form, $id); |
|
|
|
|
|
|
233
|
|
|
$casted_attributes = $this->model->getCastedAttributes(); |
|
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
foreach ($fields as $field) { |
|
236
|
|
|
|
|
237
|
|
|
// Test the field is castable |
|
238
|
|
|
if (isset($field['name']) && array_key_exists($field['name'], $casted_attributes)) { |
|
239
|
|
|
|
|
240
|
|
|
// Handle JSON field types |
|
241
|
|
|
$jsonCastables = ['array', 'object', 'json']; |
|
242
|
|
|
$fieldCasting = $casted_attributes[$field['name']]; |
|
243
|
|
|
|
|
244
|
|
|
if (in_array($fieldCasting, $jsonCastables) && isset($data[$field['name']]) && ! empty($data[$field['name']]) && ! is_array($data[$field['name']])) { |
|
|
|
|
|
|
245
|
|
|
try { |
|
246
|
|
|
$data[$field['name']] = json_decode($data[$field['name']]); |
|
247
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
248
|
|
|
$data[$field['name']] = []; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return $data; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function getCurrentFields() |
|
|
|
|
|
|
258
|
|
|
{ |
|
259
|
|
|
if ($this->entry) { |
|
260
|
|
|
return $this->getUpdateFields($this->entry->getKey()); |
|
|
|
|
|
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
return $this->getCreateFields(); |
|
|
|
|
|
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
// ------------ |
|
267
|
|
|
// TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED |
|
268
|
|
|
// ------------ |
|
269
|
|
|
// TODO: check them |
|
270
|
|
|
|
|
271
|
|
|
public function orderFields($order) |
|
272
|
|
|
{ |
|
273
|
|
|
$this->setSort('fields', (array) $order); |
|
|
|
|
|
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
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.