1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
trait FieldsProtectedMethods |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* If field has entity we want to get the relation type from it. |
11
|
|
|
* |
12
|
|
|
* @param array $field |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
|
|
public function makeSureFieldHasRelationType($field) |
16
|
|
|
{ |
17
|
|
|
$field['relation_type'] = $field['relation_type'] ?? $this->inferRelationTypeFromRelationship($field); |
|
|
|
|
18
|
|
|
|
19
|
|
|
return $field; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* If field has entity we want to make sure it also has a model for that relation. |
24
|
|
|
* |
25
|
|
|
* @param array $field |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function makeSureFieldHasModel($field) |
29
|
|
|
{ |
30
|
|
|
$field['model'] = $field['model'] ?? $this->inferFieldModelFromRelationship($field); |
|
|
|
|
31
|
|
|
|
32
|
|
|
return $field; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Based on relation type we can guess if pivot is set. |
37
|
|
|
* |
38
|
|
|
* @param array $field |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function makeSureFieldHasPivot($field) |
42
|
|
|
{ |
43
|
|
|
$field['pivot'] = $field['pivot'] ?? $this->guessIfFieldHasPivotFromRelationType($field['relation_type']); |
|
|
|
|
44
|
|
|
|
45
|
|
|
return $field; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Based on relation type we can try to guess if it is a multiple field. |
50
|
|
|
* |
51
|
|
|
* @param array $field |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function makeSureFieldHasMultiple($field) |
55
|
|
|
{ |
56
|
|
|
if (isset($field['relation_type'])) { |
57
|
|
|
$field['multiple'] = $field['multiple'] ?? $this->guessIfFieldHasMultipleFromRelationType($field['relation_type']); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $field; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* In case field name is dot notation we want to convert it to a valid HTML array field name for validation purposes. |
65
|
|
|
* |
66
|
|
|
* @param array $field |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function overwriteFieldNameFromDotNotationToArray($field) |
70
|
|
|
{ |
71
|
|
|
if (is_array($field['name'])) { |
72
|
|
|
return $field; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (strpos($field['name'], '.') === false) { |
76
|
|
|
return $field; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$entity_array = explode('.', $field['name']); |
80
|
|
|
$name_string = ''; |
81
|
|
|
|
82
|
|
|
foreach ($entity_array as $key => $array_entity) { |
83
|
|
|
$name_string .= ($key == 0) ? $array_entity : '['.$array_entity.']'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$field['name'] = $name_string; |
87
|
|
|
|
88
|
|
|
return $field; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* If the field_definition_array array is a string, it means the programmer was lazy |
93
|
|
|
* and has only passed the name of the field. Turn that into a proper array. |
94
|
|
|
* |
95
|
|
|
* @param string|array $field The field definition array (or string). |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function makeSureFieldHasName($field) |
99
|
|
|
{ |
100
|
|
|
if (is_string($field)) { |
101
|
|
|
return ['name' => $field]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (is_array($field) && ! isset($field['name'])) { |
105
|
|
|
abort(500, 'All fields must have their name defined'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $field; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* If entity is not present, but it looks like the field SHOULD be a relationship field, |
113
|
|
|
* try to determine the method on the model that defines the relationship, and pass it to |
114
|
|
|
* the field as 'entity'. |
115
|
|
|
* |
116
|
|
|
* @param [type] $field [description] |
|
|
|
|
117
|
|
|
* @return [type] [description] |
|
|
|
|
118
|
|
|
*/ |
119
|
|
|
protected function makeSureFieldHasEntity($field) |
120
|
|
|
{ |
121
|
|
|
if (isset($field['entity'])) { |
122
|
|
|
return $field; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// if the name is an array it's definitely not a relationship |
126
|
|
|
if (is_array($field['name'])) { |
127
|
|
|
return $field; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
//if the name is dot notation we are sure it's a relationship |
131
|
|
|
if (strpos($field['name'], '.') !== false) { |
132
|
|
|
$field['entity'] = $field['name']; |
133
|
|
|
|
134
|
|
|
return $field; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// if there's a method on the model with this name |
138
|
|
|
if (method_exists($this->model, $field['name'])) { |
139
|
|
|
$field['entity'] = $field['name']; |
140
|
|
|
|
141
|
|
|
return $field; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// if the name ends with _id and that method exists, |
145
|
|
|
// we can probably use it as an entity |
146
|
|
|
if (Str::endsWith($field['name'], '_id')) { |
147
|
|
|
$possibleMethodName = Str::replaceLast('_id', '', $field['name']); |
148
|
|
|
|
149
|
|
|
if (method_exists($this->model, $possibleMethodName)) { |
150
|
|
|
$field['entity'] = $possibleMethodName; |
151
|
|
|
|
152
|
|
|
return $field; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $field; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function makeSureFieldHasAttribute($field) |
160
|
|
|
{ |
161
|
|
|
// if there's a model defined, but no attribute |
162
|
|
|
// guess an attribute using the identifiableAttribute functionality in CrudTrait |
163
|
|
|
if (isset($field['model']) && ! isset($field['attribute']) && method_exists($field['model'], 'identifiableAttribute')) { |
164
|
|
|
$field['attribute'] = call_user_func([(new $field['model']), 'identifiableAttribute']); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $field; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Set the label of a field, if it's missing, by capitalizing the name and replacing |
172
|
|
|
* underscores with spaces. |
173
|
|
|
* |
174
|
|
|
* @param array $field Field definition array. |
175
|
|
|
* @return array Field definition array that contains label too. |
176
|
|
|
*/ |
177
|
|
|
protected function makeSureFieldHasLabel($field) |
178
|
|
|
{ |
179
|
|
|
if (! isset($field['label'])) { |
180
|
|
|
$name = is_array($field['name']) ? $field['name'][0] : $field['name']; |
181
|
|
|
$name = str_replace('_id', '', $name); |
182
|
|
|
$field['label'] = mb_ucfirst(str_replace('_', ' ', $name)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $field; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Set the type of a field, if it's missing, by inferring it from the |
190
|
|
|
* db column type. |
191
|
|
|
* |
192
|
|
|
* @param array $field Field definition array. |
193
|
|
|
* @return array Field definition array that contains type too. |
194
|
|
|
*/ |
195
|
|
|
protected function makeSureFieldHasType($field) |
196
|
|
|
{ |
197
|
|
|
if (! isset($field['type'])) { |
198
|
|
|
$field['type'] = isset($field['relation_type']) ? $this->inferFieldTypeFromFieldRelation($field) : $this->inferFieldTypeFromDbColumnType($field['name']); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $field; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Enable the tabs functionality, if a field has a tab defined. |
206
|
|
|
* |
207
|
|
|
* @param array $field Field definition array. |
208
|
|
|
* @return void |
209
|
|
|
*/ |
210
|
|
|
protected function enableTabsIfFieldUsesThem($field) |
211
|
|
|
{ |
212
|
|
|
// if a tab was mentioned, we should enable it |
213
|
|
|
if (isset($field['tab'])) { |
214
|
|
|
if (! $this->tabsEnabled()) { |
|
|
|
|
215
|
|
|
$this->enableTabs(); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Add a field to the current operation, using the Settings API. |
222
|
|
|
* |
223
|
|
|
* @param array $field Field definition array. |
224
|
|
|
*/ |
225
|
|
|
protected function addFieldToOperationSettings($field) |
226
|
|
|
{ |
227
|
|
|
$fieldKey = $this->getFieldKey($field); |
228
|
|
|
|
229
|
|
|
$allFields = $this->getOperationSetting('fields'); |
|
|
|
|
230
|
|
|
$allFields = array_merge($this->fields(), [$fieldKey => $field]); |
|
|
|
|
231
|
|
|
|
232
|
|
|
$this->setOperationSetting('fields', $allFields); |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get the string that should be used as an array key, for the attributive array |
237
|
|
|
* where the fields are stored for the current operation. |
238
|
|
|
* |
239
|
|
|
* The array key for the field should be: |
240
|
|
|
* - name (if the name is a string) |
241
|
|
|
* - name1_name2_name3 (if the name is an array) |
242
|
|
|
* |
243
|
|
|
* @param array $field Field definition array. |
244
|
|
|
* @return string The string that should be used as array key. |
245
|
|
|
*/ |
246
|
|
|
protected function getFieldKey($field) |
247
|
|
|
{ |
248
|
|
|
if (is_array($field['name'])) { |
249
|
|
|
return implode('_', $field['name']); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $field['name']; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|