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
|
|
|
// store the field information into the correct variable on the CRUD object |
37
|
|
|
switch (strtolower($form)) { |
38
|
|
|
case 'create': |
39
|
|
|
$this->create_fields[$complete_field_array['name']] = $complete_field_array; |
|
|
|
|
40
|
|
|
break; |
41
|
|
|
|
42
|
|
|
case 'update': |
43
|
|
|
$this->update_fields[$complete_field_array['name']] = $complete_field_array; |
|
|
|
|
44
|
|
|
break; |
45
|
|
|
|
46
|
|
|
default: |
47
|
|
|
$this->create_fields[$complete_field_array['name']] = $complete_field_array; |
48
|
|
|
$this->update_fields[$complete_field_array['name']] = $complete_field_array; |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function addFields($fields, $form = 'both') |
56
|
|
|
{ |
57
|
|
|
if (count($fields)) { |
58
|
|
|
foreach ($fields as $field) { |
59
|
|
|
$this->addField($field, $form); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Moves the recently added field to 'after' the $target_field |
66
|
|
|
* |
67
|
|
|
* @param $target_field |
68
|
|
|
*/ |
69
|
|
|
public function afterField($target_field) { |
|
|
|
|
70
|
|
View Code Duplication |
foreach ($this->create_fields as $field => $value) { |
|
|
|
|
71
|
|
|
if ($value['name'] == $target_field) { |
72
|
|
|
array_splice($this->create_fields, $field + 1, 0, [$field => array_pop($this->create_fields)]); |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
View Code Duplication |
foreach ($this->update_fields as $field => $value) { |
|
|
|
|
77
|
|
|
if ($value['name'] == $target_field) { |
78
|
|
|
array_splice($this->update_fields, $field + 1, 0, [$field => array_pop($this->update_fields)]); |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Moves the recently added field to 'before' the $target_field |
86
|
|
|
* |
87
|
|
|
* @param $target_field |
88
|
|
|
*/ |
89
|
|
|
public function beforeField($target_field) { |
|
|
|
|
90
|
|
|
$key = 0; |
91
|
|
View Code Duplication |
foreach ($this->create_fields as $field => $value) { |
|
|
|
|
92
|
|
|
if ($value['name'] == $target_field) { |
93
|
|
|
array_splice($this->create_fields, $key, 0, [$field => array_pop($this->create_fields)]); |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
$key++; |
97
|
|
|
} |
98
|
|
|
$key = 0; |
99
|
|
View Code Duplication |
foreach ($this->update_fields as $field => $value) { |
|
|
|
|
100
|
|
|
if ($value['name'] == $target_field) { |
101
|
|
|
array_splice($this->update_fields, $key, 0, [$field => array_pop($this->update_fields)]); |
102
|
|
|
break; |
103
|
|
|
} |
104
|
|
|
$key++; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Remove a certain field from the create/update/both forms by its name. |
110
|
|
|
* |
111
|
|
|
* @param string $name Field name (as defined with the addField() procedure) |
112
|
|
|
* @param string $form update/create/both |
113
|
|
|
*/ |
114
|
|
|
public function removeField($name, $form = 'both') |
115
|
|
|
{ |
116
|
|
|
switch (strtolower($form)) { |
117
|
|
|
case 'create': |
118
|
|
|
array_forget($this->create_fields, $name); |
119
|
|
|
break; |
120
|
|
|
|
121
|
|
|
case 'update': |
122
|
|
|
array_forget($this->update_fields, $name); |
123
|
|
|
break; |
124
|
|
|
|
125
|
|
|
default: |
126
|
|
|
array_forget($this->create_fields, $name); |
127
|
|
|
array_forget($this->update_fields, $name); |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Remove many fields from the create/update/both forms by their name. |
134
|
|
|
* |
135
|
|
|
* @param array $array_of_names A simple array of the names of the fields to be removed. |
136
|
|
|
* @param string $form update/create/both |
137
|
|
|
*/ |
138
|
|
|
public function removeFields($array_of_names, $form = 'both') |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
if (! empty($array_of_names)) { |
141
|
|
|
foreach ($array_of_names as $name) { |
142
|
|
|
$this->removeField($name, $form); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Check if field is the first of its type in the given fields array. |
149
|
|
|
* 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). |
|
|
|
|
150
|
|
|
* |
151
|
|
|
* @param array $field The current field being tested if it's the first of its type. |
152
|
|
|
* @param array $fields_array All the fields in that particular form. |
153
|
|
|
* |
154
|
|
|
* @return bool true/false |
155
|
|
|
*/ |
156
|
|
|
public function checkIfFieldIsFirstOfItsType($field, $fields_array) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
if ($field['name'] == $this->getFirstOfItsTypeInArray($field['type'], $fields_array)['name']) { |
|
|
|
|
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Order the fields in a certain way. |
167
|
|
|
* |
168
|
|
|
* @param [string] Column name. |
169
|
|
|
* @param [attributes and values array] |
170
|
|
|
*/ |
171
|
|
|
public function setFieldOrder($fields) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
// TODO |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// ALIAS of setFieldOrder($fields) |
177
|
|
|
public function setFieldsOrder($fields) |
178
|
|
|
{ |
179
|
|
|
$this->setFieldOrder($fields); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// ------------ |
183
|
|
|
// TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED |
184
|
|
|
// ------------ |
185
|
|
|
// TODO: check them |
186
|
|
|
|
187
|
|
|
public function orderFields($order) |
188
|
|
|
{ |
189
|
|
|
$this->setSort('fields', (array) $order); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
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.