1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories\Behaviors; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
trait HandleFieldsGroups |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param \A17\Twill\Models\Model $object |
12
|
|
|
* @param array|null $fields |
13
|
|
|
* @return \A17\Twill\Models\Model |
14
|
|
|
*/ |
15
|
3 |
|
public function hydrateHandleFieldsGroups($object, $fields) |
|
|
|
|
16
|
|
|
{ |
17
|
3 |
|
foreach ($this->fieldsGroups as $group => $groupFields) { |
18
|
|
|
if ($object->$group) { |
19
|
|
|
$casts = $this->getModelCasts($object); |
20
|
|
|
if (!array_key_exists($group, $casts) || (array_key_exists($group, $casts) && $casts[$group] !== 'array')) { |
21
|
|
|
$object->$group = json_encode($object->$group); |
22
|
|
|
} |
23
|
3 |
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $object; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param \A17\Twill\Models\Model|null $object |
31
|
31 |
|
* @param array $fields |
32
|
|
|
* @return array |
33
|
31 |
|
*/ |
34
|
|
|
public function prepareFieldsBeforeSaveHandleFieldsGroups($object, $fields) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
return $this->handleFieldsGroups($fields); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $fields |
41
|
26 |
|
* @return array |
42
|
|
|
*/ |
43
|
26 |
|
public function prepareFieldsBeforeCreateHandleFieldsGroups($fields) |
44
|
|
|
{ |
45
|
|
|
return $this->handleFieldsGroups($fields); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \A17\Twill\Models\Model $object |
50
|
|
|
* @param array $fields |
51
|
6 |
|
* @return array |
52
|
|
|
*/ |
53
|
6 |
|
public function getFormFieldsHandleFieldsGroups($object, $fields) |
54
|
|
|
{ |
55
|
|
|
foreach ($this->fieldsGroups as $group => $groupFields) { |
56
|
|
|
if ($object->$group) { |
57
|
|
|
$casts = $this->getModelCasts($object); |
58
|
|
|
if (array_key_exists($group, $casts) && $casts[$group] === 'array') { |
59
|
|
|
$decoded_fields = $object->$group; |
60
|
|
|
} else { |
61
|
|
|
$decoded_fields = json_decode($object->$group, true) ?? []; |
62
|
|
|
} |
63
|
|
|
|
64
|
6 |
|
// In case that some field read the value through $item->$name |
65
|
|
|
foreach ($decoded_fields as $field_name => $field_value) { |
66
|
|
|
if ($this->fieldsGroupsFormFieldNamesAutoPrefix) { |
67
|
32 |
|
$decoded_fields[$group . $this->fieldsGroupsFormFieldNameSeparator . $field_name] = $field_value; |
68
|
32 |
|
unset($decoded_fields[$field_name]); |
69
|
|
|
|
70
|
|
|
if (!is_array($field_value)) { |
71
|
|
|
$object->setAttribute($group . $this->fieldsGroupsFormFieldNameSeparator . $field_name, $field_value); |
72
|
|
|
} |
73
|
|
|
} else { |
74
|
|
|
$object->setAttribute($field_name, $field_value); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
$fields = array_merge($fields, $decoded_fields); |
78
|
|
|
} |
79
|
|
|
} |
80
|
32 |
|
|
81
|
|
|
return $fields; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function handleFieldsGroups($fields) |
85
|
|
|
{ |
86
|
|
|
foreach ($this->fieldsGroups as $group => $groupFields) { |
87
|
|
|
if ($this->fieldsGroupsFormFieldNamesAutoPrefix) { |
88
|
|
|
$groupFields = array_map(function ($field_name) use ($group) { |
89
|
|
|
return $group . $this->fieldsGroupsFormFieldNameSeparator . $field_name; |
90
|
|
|
}, $groupFields); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$fields[$group] = Arr::where(Arr::only($fields, $groupFields), function ($value, $key) { |
|
|
|
|
94
|
|
|
return !empty($value); |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
if ($this->fieldsGroupsFormFieldNamesAutoPrefix) { |
98
|
|
|
$fieldsGroupWithGroupSeparator = []; |
99
|
|
|
foreach ($fields[$group] as $key => $value) { |
100
|
|
|
$fieldsGroupWithGroupSeparator[Str::replaceFirst($group . $this->fieldsGroupsFormFieldNameSeparator, '', $key)] = $value; |
101
|
|
|
} |
102
|
|
|
$fields[$group] = $fieldsGroupWithGroupSeparator; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (in_array($group, $this->model->getTranslatedAttributes()) && is_array($fields[$group])) { |
106
|
|
|
$fieldForTranslationTrait = []; |
107
|
|
|
foreach ($fields[$group] as $field => $translatedValues) { |
108
|
|
|
foreach ($translatedValues as $locale => $value) { |
109
|
|
|
$fieldForTranslationTrait[$locale][$field] = $value; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
$fields[$group] = $fieldForTranslationTrait; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (empty($fields[$group])) { |
116
|
|
|
$fields[$group] = null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
Arr::forget($fields, $groupFields); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $fields; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param \A17\Twill\Models\Model $object |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
protected function getModelCasts($object) |
130
|
|
|
{ |
131
|
|
|
$casts = $object->getCasts(); |
132
|
|
|
if ($this->model->isTranslatable()) { |
133
|
|
|
$casts = $casts + app()->make($this->model->getTranslationModelNameDefault())->getCasts(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $casts; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.