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 string|bool|Model $model |
|
|
|
|
17
|
|
|
* @param array $fields The fields that should be compacted. |
18
|
|
|
* @return array The updated request input. |
19
|
|
|
*/ |
20
|
|
|
public function compactFakeFields($requestInput, $model = false, $fields = []) |
21
|
|
|
{ |
22
|
|
|
// get the right fields according to the form type (create/update) |
23
|
|
|
if (empty($fields)) { |
24
|
|
|
$fields = $this->fields(); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$model = $model ? (is_string($model) ? app($model) : $model) : $this->model; |
28
|
|
|
|
29
|
|
|
$compactedFakeFields = []; |
30
|
|
|
|
31
|
|
|
foreach ($fields as $field) { |
32
|
|
|
// compact fake fields |
33
|
|
|
// explode the comma delimited names, eg: start,end in a date_range: |
34
|
|
|
$field['name'] = explode(',', $field['name']); |
35
|
|
|
foreach ($field['name'] as $fieldName) { |
36
|
|
|
$fakeFieldKey = isset($field['store_in']) ? $field['store_in'] : 'extras'; |
37
|
|
|
$isFakeField = $field['fake'] ?? false; |
38
|
|
|
|
39
|
|
|
// field is represented by the subfields |
40
|
|
|
if (isset($field['subfields']) && isset($field['model']) && $field['model'] === get_class($model)) { |
|
|
|
|
41
|
|
|
foreach ($field['subfields'] as $subfield) { |
42
|
|
|
// explode the comma delimited names, eg: start,end in a date_range: |
43
|
|
|
$subfield['name'] = explode(',', $subfield['name']); |
44
|
|
|
foreach ($subfield['name'] as $subfieldName) { |
45
|
|
|
$subfieldName = Str::afterLast($subfieldName, '.'); |
46
|
|
|
$isSubfieldFake = $subfield['fake'] ?? false; |
47
|
|
|
$subFakeFieldKey = isset($subfield['store_in']) ? $subfield['store_in'] : 'extras'; |
48
|
|
|
|
49
|
|
|
if (array_key_exists($subfieldName, $requestInput) && $isSubfieldFake) { |
50
|
|
|
$this->addCompactedField($requestInput, $subfieldName, $subFakeFieldKey); |
51
|
|
|
|
52
|
|
|
if (! in_array($subFakeFieldKey, $compactedFakeFields)) { |
53
|
|
|
$compactedFakeFields[] = $subFakeFieldKey; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (array_key_exists($fieldName, $requestInput) && $isFakeField) { |
62
|
|
|
$this->addCompactedField($requestInput, $fieldName, $fakeFieldKey); |
63
|
|
|
|
64
|
|
|
if (! in_array($fakeFieldKey, $compactedFakeFields)) { |
65
|
|
|
$compactedFakeFields[] = $fakeFieldKey; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// json_encode all fake_value columns if applicable in the database, so they can be properly stored and interpreted |
72
|
|
|
foreach ($compactedFakeFields as $value) { |
73
|
|
|
if (! (property_exists($model, 'translatable') && in_array($value, $model->getTranslatableAttributes(), true)) && $model->shouldEncodeFake($value)) { |
74
|
|
|
$requestInput[$value] = json_encode($requestInput[$value]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// if there are no fake fields defined, return the original request input |
79
|
|
|
return $requestInput; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Compact a fake field in the request input array. |
84
|
|
|
* |
85
|
|
|
* @param array $requestInput The request input. |
86
|
|
|
* @param string $fakeFieldName The fake field name. |
87
|
|
|
* @param string $fakeFieldKey The fake field key. |
88
|
|
|
*/ |
89
|
|
|
private function addCompactedField(&$requestInput, $fakeFieldName, $fakeFieldKey) |
90
|
|
|
{ |
91
|
|
|
$fakeField = $requestInput[$fakeFieldName]; |
92
|
|
|
Arr::pull($requestInput, $fakeFieldName); // remove the fake field from the request |
93
|
|
|
|
94
|
|
|
$requestInput[$fakeFieldKey][$fakeFieldName] = $fakeField; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths