1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
trait FakeFields |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Update the request input array to something that can be passed to the model's create or update function. |
12
|
|
|
* The resulting array will only include the fields that are stored in the database and their values, |
13
|
|
|
* plus the '_token' and 'redirect_after_save' variables. |
14
|
|
|
* |
15
|
|
|
* @param array $requestInput The request input. |
16
|
|
|
* @param array $fields |
17
|
|
|
* |
18
|
|
|
* @see \Illuminate\Http\Request::all() For an example on how to get the request input. |
19
|
|
|
* |
20
|
|
|
* @return array The updated request input. |
21
|
|
|
*/ |
22
|
|
|
public function compactFakeFields($requestInput, $model = false, $fields = []) |
23
|
|
|
{ |
24
|
|
|
// get the right fields according to the form type (create/update) |
25
|
|
|
if (empty($fields)) { |
26
|
|
|
$fields = $this->fields(); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$model = $model ? (is_string($model) ? app($model) : $model) : $this->model; |
30
|
|
|
|
31
|
|
|
$compactedFakeFields = []; |
32
|
|
|
|
33
|
|
|
foreach ($fields as $field) { |
34
|
|
|
// compact fake fields |
35
|
|
|
// cast the field name to array first, to account for array field names |
36
|
|
|
// in fields that send multiple inputs and want them all saved to the database |
37
|
|
|
foreach ((array) $field['name'] as $fieldName) { |
38
|
|
|
$fakeFieldKey = isset($field['store_in']) ? $field['store_in'] : 'extras'; |
39
|
|
|
$isFakeField = $field['fake'] ?? false; |
40
|
|
|
|
41
|
|
|
// field is represented by the subfields |
42
|
|
|
if (isset($field['subfields']) && $field['model'] === get_class($model)) { |
43
|
|
|
foreach ($field['subfields'] as $subfield) { |
44
|
|
|
$subfieldName = Str::afterLast($subfield['name'], '.'); |
45
|
|
|
$isSubfieldFake = $subfield['fake'] ?? false; |
46
|
|
|
$subFakeFieldKey = isset($subfield['store_in']) ? $subfield['store_in'] : 'extras'; |
47
|
|
|
|
48
|
|
|
if (array_key_exists($subfieldName, $requestInput) && $isSubfieldFake) { |
49
|
|
|
$this->addCompactedField($requestInput, $subfieldName, $subFakeFieldKey); |
50
|
|
|
|
51
|
|
|
if (! in_array($subFakeFieldKey, $compactedFakeFields)) { |
52
|
|
|
$compactedFakeFields[] = $subFakeFieldKey; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (array_key_exists($fieldName, $requestInput) && $isFakeField) { |
60
|
|
|
$this->addCompactedField($requestInput, $fieldName, $fakeFieldKey); |
61
|
|
|
|
62
|
|
|
if (! in_array($fakeFieldKey, $compactedFakeFields)) { |
63
|
|
|
$compactedFakeFields[] = $fakeFieldKey; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// json_encode all fake_value columns if applicable in the database, so they can be properly stored and interpreted |
70
|
|
|
foreach ($compactedFakeFields as $value) { |
71
|
|
|
if (! (property_exists($model, 'translatable') && in_array($value, $model->getTranslatableAttributes(), true)) && $model->shouldEncodeFake($value)) { |
|
|
|
|
72
|
|
|
$requestInput[$value] = json_encode($requestInput[$value]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// if there are no fake fields defined, return the original request input |
77
|
|
|
return $requestInput; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Compact a fake field in the request input array. |
82
|
|
|
* |
83
|
|
|
* @param array $requestInput The request input. |
84
|
|
|
* @param string $fakeFieldName The fake field name. |
85
|
|
|
* @param string $fakeFieldKey The fake field key. |
86
|
|
|
*/ |
87
|
|
|
private function addCompactedField(&$requestInput, $fakeFieldName, $fakeFieldKey) |
88
|
|
|
{ |
89
|
|
|
$fakeField = $requestInput[$fakeFieldName]; |
90
|
|
|
Arr::pull($requestInput, $fakeFieldName); // remove the fake field from the request |
91
|
|
|
|
92
|
|
|
$requestInput[$fakeFieldKey][$fakeFieldName] = $fakeField; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|