|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories\Behaviors; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
|
|
7
|
|
|
trait HandleFieldsGroups |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @param \A17\Twill\Models\Model $object |
|
12
|
|
|
* @param array $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
|
|
|
$object->$group = json_encode($object->$group); |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
3 |
|
return $object; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param \A17\Twill\Models\Model|null $object |
|
28
|
|
|
* @param array $fields |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
27 |
|
public function prepareFieldsBeforeSaveHandleFieldsGroups($object, $fields) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
27 |
|
return $this->handleFieldsGroups($fields); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param \A17\Twill\Models\Model|null $object |
|
38
|
|
|
* @param array $fields |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
22 |
|
public function prepareFieldsBeforeCreateHandleFieldsGroups($fields) |
|
42
|
|
|
{ |
|
43
|
22 |
|
return $this->handleFieldsGroups($fields); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param \A17\Twill\Models\Model $object |
|
48
|
|
|
* @param array $fields |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
5 |
|
public function getFormFieldsHandleFieldsGroups($object, $fields) |
|
52
|
|
|
{ |
|
53
|
5 |
|
foreach ($this->fieldsGroups as $group => $groupFields) { |
|
54
|
|
|
if ($object->$group) { |
|
55
|
|
|
$decoded_fields = json_decode($object->$group, true) ?? []; |
|
56
|
|
|
// In case that some field read the value through $item->$name |
|
57
|
|
|
foreach($decoded_fields as $field_name => $field_value) { |
|
58
|
|
|
$object->setAttribute($field_name, $field_value); |
|
59
|
|
|
} |
|
60
|
|
|
$fields = array_merge($fields, $decoded_fields); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
return $fields; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
28 |
|
protected function handleFieldsGroups($fields) { |
|
68
|
28 |
|
foreach ($this->fieldsGroups as $group => $groupFields) { |
|
69
|
|
|
$fields[$group] = Arr::where(Arr::only($fields, $groupFields), function ($value, $key) { |
|
|
|
|
|
|
70
|
|
|
return !empty($value); |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
if (empty($fields[$group])) { |
|
74
|
|
|
$fields[$group] = null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
Arr::forget($fields, $groupFields); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
28 |
|
return $fields; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.