1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudField; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
|
8
|
|
|
trait Fields |
9
|
|
|
{ |
10
|
|
|
use FieldsProtectedMethods; |
11
|
|
|
use FieldsPrivateMethods; |
12
|
|
|
|
13
|
|
|
// ------------ |
14
|
|
|
// FIELDS |
15
|
|
|
// ------------ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get the CRUD fields for the current operation with name processed to be usable in HTML. |
19
|
|
|
* |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public function fields() |
23
|
|
|
{ |
24
|
|
|
return $this->overwriteFieldNamesFromDotNotationToArray($this->getOperationSetting('fields') ?? []); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns the fields as they are stored inside operation setting, not running the |
29
|
|
|
* presentation callbacks like converting the `dot.names` into `dot[names]` for html for example. |
30
|
|
|
*/ |
31
|
|
|
public function getCleanStateFields() |
32
|
|
|
{ |
33
|
|
|
return $this->getOperationSetting('fields') ?? []; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The only REALLY MANDATORY attribute when defining a field is the 'name'. |
38
|
|
|
* Everything else Backpack can probably guess. This method makes sure the |
39
|
|
|
* field definition array is complete, by guessing missing attributes. |
40
|
|
|
* |
41
|
|
|
* @param string|array $field The definition of a field (string or array). |
42
|
|
|
* @return array The correct definition of that field. |
43
|
|
|
*/ |
44
|
|
|
public function makeSureFieldHasNecessaryAttributes($field) |
45
|
|
|
{ |
46
|
|
|
$field = $this->makeSureFieldHasName($field); |
47
|
|
|
$field = $this->makeSureFieldHasEntity($field); |
48
|
|
|
$field = $this->makeSureFieldHasLabel($field); |
49
|
|
|
|
50
|
|
|
if (isset($field['entity']) && $field['entity'] !== false) { |
51
|
|
|
$field = $this->makeSureFieldHasRelationshipAttributes($field); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$field = $this->makeSureFieldHasType($field); |
55
|
|
|
$field = $this->makeSureSubfieldsHaveNecessaryAttributes($field); |
56
|
|
|
$field = $this->makeSureMorphSubfieldsAreDefined($field); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->setupFieldValidation($field, $field['parentFieldName'] ?? false); |
|
|
|
|
59
|
|
|
|
60
|
|
|
return $field; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* When field is a relationship, Backpack will try to guess some basic attributes from the relation. |
65
|
|
|
* |
66
|
|
|
* @param array $field |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function makeSureFieldHasRelationshipAttributes($field) |
70
|
|
|
{ |
71
|
|
|
$field = $this->makeSureFieldHasRelationType($field); |
72
|
|
|
$field = $this->makeSureFieldHasModel($field); |
73
|
|
|
$field = $this->makeSureFieldHasAttribute($field); |
74
|
|
|
$field = $this->makeSureFieldHasMultiple($field); |
75
|
|
|
$field = $this->makeSureFieldHasPivot($field); |
76
|
|
|
$field = $this->makeSureFieldHasType($field); |
77
|
|
|
|
78
|
|
|
return $field; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Register all Eloquent Model events that are defined on fields. |
83
|
|
|
* Eg. saving, saved, creating, created, updating, updated. |
84
|
|
|
* |
85
|
|
|
* @see https://laravel.com/docs/master/eloquent#events |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function registerFieldEvents() |
90
|
|
|
{ |
91
|
|
|
foreach ($this->getCleanStateFields() as $key => $field) { |
92
|
|
|
if (isset($field['events'])) { |
93
|
|
|
foreach ($field['events'] as $event => $closure) { |
94
|
|
|
$this->model->{$event}($closure); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add a field to the create/update form or both. |
102
|
|
|
* |
103
|
|
|
* @param string|array $field The new field. |
104
|
|
|
* @return self |
105
|
|
|
*/ |
106
|
|
|
public function addField($field) |
107
|
|
|
{ |
108
|
|
|
$field = $this->makeSureFieldHasNecessaryAttributes($field); |
109
|
|
|
|
110
|
|
|
$this->enableTabsIfFieldUsesThem($field); |
111
|
|
|
$this->addFieldToOperationSettings($field); |
112
|
|
|
|
113
|
|
|
if (! app('UploadStore')->isUploadHandled($field['name'])) { |
114
|
|
|
$crudFieldObject = (new CrudField($field['name'])); |
115
|
|
|
|
116
|
|
|
foreach ($field as $attribute => $value) { |
117
|
|
|
if ($crudFieldObject->hasMacro($attribute)) { |
118
|
|
|
$crudFieldObject->{$attribute}($value); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Add multiple fields to the create/update form or both. |
128
|
|
|
* |
129
|
|
|
* @param array $fields The new fields. |
130
|
|
|
*/ |
131
|
|
|
public function addFields($fields) |
132
|
|
|
{ |
133
|
|
|
if (count($fields)) { |
134
|
|
|
foreach ($fields as $field) { |
135
|
|
|
$this->addField($field); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Move the most recently added field after the given target field. |
142
|
|
|
* |
143
|
|
|
* @param string $targetFieldName The target field name. |
144
|
|
|
*/ |
145
|
|
|
public function afterField($targetFieldName) |
146
|
|
|
{ |
147
|
|
|
$this->transformFields(function ($fields) use ($targetFieldName) { |
148
|
|
|
return $this->moveField($fields, $targetFieldName, false); |
149
|
|
|
}); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Move the most recently added field before the given target field. |
154
|
|
|
* |
155
|
|
|
* @param string $targetFieldName The target field name. |
156
|
|
|
*/ |
157
|
|
|
public function beforeField($targetFieldName) |
158
|
|
|
{ |
159
|
|
|
$this->transformFields(function ($fields) use ($targetFieldName) { |
160
|
|
|
return $this->moveField($fields, $targetFieldName, true); |
161
|
|
|
}); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Move this field to be first in the fields list. |
166
|
|
|
* |
167
|
|
|
* @return bool|null |
168
|
|
|
*/ |
169
|
|
|
public function makeFirstField() |
170
|
|
|
{ |
171
|
|
|
if (! $this->fields()) { |
172
|
|
|
return false; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$firstField = array_keys(array_slice($this->getCleanStateFields(), 0, 1))[0]; |
176
|
|
|
$this->beforeField($firstField); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Remove a certain field from the create/update/both forms by its name. |
181
|
|
|
* |
182
|
|
|
* @param string $name Field name (as defined with the addField() procedure) |
183
|
|
|
*/ |
184
|
|
|
public function removeField($name) |
185
|
|
|
{ |
186
|
|
|
$this->transformFields(function ($fields) use ($name) { |
187
|
|
|
Arr::forget($fields, $name); |
188
|
|
|
|
189
|
|
|
return $fields; |
190
|
|
|
}); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Remove many fields from the create/update/both forms by their name. |
195
|
|
|
* |
196
|
|
|
* @param array $array_of_names A simple array of the names of the fields to be removed. |
197
|
|
|
*/ |
198
|
|
|
public function removeFields($array_of_names) |
199
|
|
|
{ |
200
|
|
|
if (! empty($array_of_names)) { |
201
|
|
|
foreach ($array_of_names as $name) { |
202
|
|
|
$this->removeField($name); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Remove all fields from the create/update/both forms. |
209
|
|
|
*/ |
210
|
|
|
public function removeAllFields() |
211
|
|
|
{ |
212
|
|
|
$current_fields = $this->getCleanStateFields(); |
213
|
|
|
if (! empty($current_fields)) { |
214
|
|
|
foreach ($current_fields as $field) { |
215
|
|
|
$this->removeField($field['name']); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Remove an attribute from one field's definition array. |
222
|
|
|
* |
223
|
|
|
* @param string $field The name of the field. |
224
|
|
|
* @param string $attribute The name of the attribute being removed. |
225
|
|
|
*/ |
226
|
|
|
public function removeFieldAttribute($field, $attribute) |
227
|
|
|
{ |
228
|
|
|
$fields = $this->getCleanStateFields(); |
229
|
|
|
|
230
|
|
|
unset($fields[$field][$attribute]); |
231
|
|
|
|
232
|
|
|
$this->setOperationSetting('fields', $fields); |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Update value of a given key for a current field. |
237
|
|
|
* |
238
|
|
|
* @param string $fieldName The field name |
239
|
|
|
* @param array $modifications An array of changes to be made. |
240
|
|
|
*/ |
241
|
|
|
public function modifyField($fieldName, $modifications) |
242
|
|
|
{ |
243
|
|
|
$fieldsArray = $this->getCleanStateFields(); |
244
|
|
|
$field = $this->firstFieldWhere('name', $fieldName); |
245
|
|
|
$fieldKey = $this->getFieldKey($field); |
|
|
|
|
246
|
|
|
|
247
|
|
|
foreach ($modifications as $attributeName => $attributeValue) { |
248
|
|
|
$fieldsArray[$fieldKey][$attributeName] = $attributeValue; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$this->enableTabsIfFieldUsesThem($modifications); |
252
|
|
|
|
253
|
|
|
$this->setOperationSetting('fields', $fieldsArray); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Set label for a specific field. |
258
|
|
|
* |
259
|
|
|
* @param string $field |
260
|
|
|
* @param string $label |
261
|
|
|
*/ |
262
|
|
|
public function setFieldLabel($field, $label) |
263
|
|
|
{ |
264
|
|
|
$this->modifyField($field, ['label' => $label]); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Check if field is the first of its type in the given fields array. |
269
|
|
|
* 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). |
270
|
|
|
* |
271
|
|
|
* @param array $field The current field being tested if it's the first of its type. |
272
|
|
|
* @return bool true/false |
273
|
|
|
*/ |
274
|
|
|
public function checkIfFieldIsFirstOfItsType($field) |
275
|
|
|
{ |
276
|
|
|
$fields_array = $this->getCleanStateFields(); |
277
|
|
|
$first_field = $this->getFirstOfItsTypeInArray($field['type'], $fields_array); |
|
|
|
|
278
|
|
|
|
279
|
|
|
if ($first_field && $field['name'] == $first_field['name']) { |
280
|
|
|
return true; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return false; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Decode attributes that are casted as array/object/json in the model. |
288
|
|
|
* So that they are not json_encoded twice before they are stored in the db |
289
|
|
|
* (once by Backpack in front-end, once by Laravel Attribute Casting). |
290
|
|
|
* |
291
|
|
|
* @param array $input |
292
|
|
|
* @param mixed $model |
293
|
|
|
* @return array |
294
|
|
|
*/ |
295
|
|
|
public function decodeJsonCastedAttributes($input, $model = false) |
296
|
|
|
{ |
297
|
|
|
$model = $model ? $model : $this->model; |
298
|
|
|
$fields = $this->getCleanStateFields(); |
299
|
|
|
$casted_attributes = $model->getCastedAttributes(); |
300
|
|
|
|
301
|
|
|
foreach ($fields as $field) { |
302
|
|
|
// Test the field is castable |
303
|
|
|
if (isset($field['name']) && is_string($field['name']) && array_key_exists($field['name'], $casted_attributes)) { |
304
|
|
|
// Handle JSON field types |
305
|
|
|
$jsonCastables = ['array', 'object', 'json']; |
306
|
|
|
$fieldCasting = $casted_attributes[$field['name']]; |
307
|
|
|
|
308
|
|
|
if (in_array($fieldCasting, $jsonCastables) && isset($input[$field['name']]) && ! empty($input[$field['name']]) && ! is_array($input[$field['name']])) { |
309
|
|
|
try { |
310
|
|
|
$input[$field['name']] = json_decode($input[$field['name']]); |
311
|
|
|
} catch (\Exception $e) { |
312
|
|
|
$input[$field['name']] = []; |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return $input; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @return array |
323
|
|
|
*/ |
324
|
|
|
public function getCurrentFields() |
325
|
|
|
{ |
326
|
|
|
return $this->fields(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Order the CRUD fields. If certain fields are missing from the given order array, they will be |
331
|
|
|
* pushed to the new fields array in the original order. |
332
|
|
|
* |
333
|
|
|
* @param array $order An array of field names in the desired order. |
334
|
|
|
*/ |
335
|
|
|
public function orderFields($order) |
336
|
|
|
{ |
337
|
|
|
$this->transformFields(function ($fields) use ($order) { |
338
|
|
|
return $this->applyOrderToFields($fields, $order); |
339
|
|
|
}); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Get the fields for the create or update forms. |
344
|
|
|
* |
345
|
|
|
* @return array all the fields that need to be shown and their information |
346
|
|
|
*/ |
347
|
|
|
public function getFields() |
348
|
|
|
{ |
349
|
|
|
return $this->fields(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Check if the create/update form has upload fields. |
354
|
|
|
* Upload fields are the ones that have "upload" => true defined on them. |
355
|
|
|
* |
356
|
|
|
* @param string $form create/update/both - defaults to 'both' |
357
|
|
|
* @param bool|int $id id of the entity - defaults to false |
358
|
|
|
* @return bool |
359
|
|
|
*/ |
360
|
|
|
public function hasUploadFields() |
361
|
|
|
{ |
362
|
|
|
$fields = $this->getCleanStateFields(); |
363
|
|
|
$upload_fields = Arr::where($fields, function ($value, $key) { |
364
|
|
|
if (isset($value['subfields'])) { |
365
|
|
|
foreach ($value['subfields'] as $subfield) { |
366
|
|
|
if (isset($subfield['upload']) && $subfield['upload'] === true) { |
367
|
|
|
return true; |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
return isset($value['upload']) && $value['upload'] == true; |
373
|
|
|
}); |
374
|
|
|
|
375
|
|
|
return count($upload_fields) ? true : false; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
// ---------------------- |
379
|
|
|
// FIELD ASSET MANAGEMENT |
380
|
|
|
// ---------------------- |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Get all the field types whose resources (JS and CSS) have already been loaded on page. |
384
|
|
|
* |
385
|
|
|
* @return array Array with the names of the field types. |
386
|
|
|
*/ |
387
|
|
|
public function getLoadedFieldTypes() |
388
|
|
|
{ |
389
|
|
|
return $this->getOperationSetting('loadedFieldTypes') ?? []; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Set an array of field type names as already loaded for the current operation. |
394
|
|
|
* |
395
|
|
|
* @param array $fieldTypes |
396
|
|
|
*/ |
397
|
|
|
public function setLoadedFieldTypes($fieldTypes) |
398
|
|
|
{ |
399
|
|
|
$this->setOperationSetting('loadedFieldTypes', $fieldTypes); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Get a namespaced version of the field type name. |
404
|
|
|
* Appends the 'view_namespace' attribute of the field to the `type', using dot notation. |
405
|
|
|
* |
406
|
|
|
* @param mixed $field |
407
|
|
|
* @return string Namespaced version of the field type name. Ex: 'text', 'custom.view.path.text' |
408
|
|
|
*/ |
409
|
|
|
public function getFieldTypeWithNamespace($field) |
410
|
|
|
{ |
411
|
|
|
if (is_array($field)) { |
412
|
|
|
$fieldType = $field['type']; |
413
|
|
|
if (isset($field['view_namespace'])) { |
414
|
|
|
$fieldType = implode('.', [$field['view_namespace'], $field['type']]); |
415
|
|
|
} |
416
|
|
|
} else { |
417
|
|
|
$fieldType = $field; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
return $fieldType; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Add a new field type to the loadedFieldTypes array. |
425
|
|
|
* |
426
|
|
|
* @param string $field Field array |
427
|
|
|
* @return bool Successful operation true/false. |
428
|
|
|
*/ |
429
|
|
|
public function addLoadedFieldType($field) |
430
|
|
|
{ |
431
|
|
|
$alreadyLoaded = $this->getLoadedFieldTypes(); |
432
|
|
|
$type = $this->getFieldTypeWithNamespace($field); |
433
|
|
|
|
434
|
|
|
if (! in_array($type, $this->getLoadedFieldTypes(), true)) { |
435
|
|
|
$alreadyLoaded[] = $type; |
436
|
|
|
$this->setLoadedFieldTypes($alreadyLoaded); |
437
|
|
|
|
438
|
|
|
return true; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
return false; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Alias of the addLoadedFieldType() method. |
446
|
|
|
* Adds a new field type to the loadedFieldTypes array. |
447
|
|
|
* |
448
|
|
|
* @param string $field Field array |
449
|
|
|
* @return bool Successful operation true/false. |
450
|
|
|
*/ |
451
|
|
|
public function markFieldTypeAsLoaded($field) |
452
|
|
|
{ |
453
|
|
|
return $this->addLoadedFieldType($field); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Check if a field type's reasources (CSS and JS) have already been loaded. |
458
|
|
|
* |
459
|
|
|
* @param string $field Field array |
460
|
|
|
* @return bool Whether the field type has been marked as loaded. |
461
|
|
|
*/ |
462
|
|
|
public function fieldTypeLoaded($field) |
463
|
|
|
{ |
464
|
|
|
return in_array($this->getFieldTypeWithNamespace($field), $this->getLoadedFieldTypes()); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Check if a field type's reasources (CSS and JS) have NOT been loaded. |
469
|
|
|
* |
470
|
|
|
* @param string $field Field array |
471
|
|
|
* @return bool Whether the field type has NOT been marked as loaded. |
472
|
|
|
*/ |
473
|
|
|
public function fieldTypeNotLoaded($field) |
474
|
|
|
{ |
475
|
|
|
return ! in_array($this->getFieldTypeWithNamespace($field), $this->getLoadedFieldTypes()); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Get a list of all field names for the current operation. |
480
|
|
|
* |
481
|
|
|
* @return array |
482
|
|
|
*/ |
483
|
|
|
public function getAllFieldNames() |
484
|
|
|
{ |
485
|
|
|
return Arr::flatten(Arr::pluck($this->getCleanStateFields(), 'name')); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Returns the request without anything that might have been maliciously inserted. |
490
|
|
|
* Only specific field names that have been introduced with addField() are kept in the request. |
491
|
|
|
* |
492
|
|
|
* @param \Illuminate\Http\Request $request |
493
|
|
|
* @return array |
494
|
|
|
*/ |
495
|
|
|
public function getStrippedSaveRequest($request) |
496
|
|
|
{ |
497
|
|
|
$setting = $this->getOperationSetting('strippedRequest'); |
498
|
|
|
|
499
|
|
|
// if a closure was passed |
500
|
|
|
if (is_callable($setting)) { |
501
|
|
|
return $setting($request); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
// if an invokable class was passed |
505
|
|
|
// eg. \App\Http\Requests\BackpackStrippedRequest |
506
|
|
|
if (is_string($setting) && class_exists($setting)) { |
507
|
|
|
$setting = new $setting(); |
508
|
|
|
|
509
|
|
|
return is_callable($setting) ? $setting($request) : abort(500, get_class($setting).' is not invokable.'); |
|
|
|
|
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
return $request->only($this->getAllFieldNames()); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Check if a field exists, by any given attribute. |
517
|
|
|
* |
518
|
|
|
* @param string $attribute Attribute name on that field definition array. |
519
|
|
|
* @param string $value Value of that attribute on that field definition array. |
520
|
|
|
* @return bool |
521
|
|
|
*/ |
522
|
|
|
public function hasFieldWhere($attribute, $value) |
523
|
|
|
{ |
524
|
|
|
$match = Arr::first($this->getCleanStateFields(), function ($field, $fieldKey) use ($attribute, $value) { |
525
|
|
|
return isset($field[$attribute]) && $field[$attribute] == $value; |
526
|
|
|
}); |
527
|
|
|
|
528
|
|
|
return (bool) $match; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Get the first field where a given attribute has the given value. |
533
|
|
|
* |
534
|
|
|
* @param string $attribute Attribute name on that field definition array. |
535
|
|
|
* @param string $value Value of that attribute on that field definition array. |
536
|
|
|
* @return bool |
537
|
|
|
*/ |
538
|
|
|
public function firstFieldWhere($attribute, $value) |
539
|
|
|
{ |
540
|
|
|
return Arr::first($this->getCleanStateFields(), function ($field, $fieldKey) use ($attribute, $value) { |
541
|
|
|
return isset($field[$attribute]) && $field[$attribute] == $value; |
542
|
|
|
}); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* Create and return a CrudField object for that field name. |
547
|
|
|
* |
548
|
|
|
* Enables developers to use a fluent syntax to declare their fields, |
549
|
|
|
* in addition to the existing options: |
550
|
|
|
* - CRUD::addField(['name' => 'price', 'type' => 'number']); |
551
|
|
|
* - CRUD::field('price')->type('number'); |
552
|
|
|
* |
553
|
|
|
* And if the developer uses the CrudField object as Field in their CrudController: |
554
|
|
|
* - Field::name('price')->type('number'); |
555
|
|
|
* |
556
|
|
|
* @param string $name The name of the column in the db, or model attribute. |
557
|
|
|
* @return CrudField |
558
|
|
|
*/ |
559
|
|
|
public function field($name) |
560
|
|
|
{ |
561
|
|
|
return new CrudField($name); |
562
|
|
|
} |
563
|
|
|
} |
564
|
|
|
|