|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NovaFlexibleContent\Layouts\LayoutTraits; |
|
4
|
|
|
|
|
5
|
|
|
use NovaFlexibleContent\Flexible; |
|
6
|
|
|
use NovaFlexibleContent\Layouts\Layout; |
|
7
|
|
|
|
|
8
|
|
|
trait HasFlexibleFieldInLayout |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* If layout contains sub flexible groups, find group by key recursive. |
|
12
|
|
|
* |
|
13
|
|
|
* @param string $groupKey |
|
14
|
|
|
* @return Layout|null |
|
15
|
|
|
*/ |
|
16
|
4 |
|
public function findFlexibleGroupRecursive(string $groupKey): ?Layout |
|
17
|
|
|
{ |
|
18
|
4 |
|
if ($this->isUseKey($groupKey)) { |
|
|
|
|
|
|
19
|
1 |
|
return $this; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
4 |
|
foreach ($this->fieldsCollection() as $field) { |
|
|
|
|
|
|
23
|
4 |
|
if ($field instanceof Flexible) { |
|
24
|
4 |
|
if ($group = $field->findGroupRecursive($groupKey)) { |
|
25
|
3 |
|
return $group; |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
2 |
|
return null; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* TODO: rebuild |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function findGroupRecursiveAndSetAttribute($groupKey, $fieldKey, $newValue): bool |
|
37
|
|
|
{ |
|
38
|
2 |
|
$data = $this->getAttributes(); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
2 |
|
if ($this->isUseKey($groupKey)) { |
|
41
|
1 |
|
if (array_key_exists($fieldKey, $data)) { |
|
42
|
1 |
|
$this->setAttribute($fieldKey, $newValue); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
1 |
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
$result = $this->setAttributeValue($data, $groupKey, $fieldKey, $newValue); |
|
51
|
|
|
|
|
52
|
2 |
|
$this->attributes = $data; |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
2 |
|
return $result; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
public function setAttributeValue(array &$array, string $groupKey, string $fieldKey, mixed $newValue): bool |
|
58
|
|
|
{ |
|
59
|
2 |
|
foreach ($array as $key => $value) { |
|
60
|
2 |
|
if (is_object($value) |
|
61
|
2 |
|
&& property_exists($value, 'key') |
|
62
|
2 |
|
&& property_exists($value, 'attributes') |
|
63
|
2 |
|
&& $value->key === $groupKey |
|
64
|
2 |
|
&& is_object($value->attributes)) { |
|
65
|
2 |
|
foreach ($value->attributes as $attribute => $attrValue) { |
|
66
|
2 |
|
if ($attribute === $fieldKey) { |
|
67
|
2 |
|
$value->attributes->$attribute = $newValue; |
|
68
|
|
|
|
|
69
|
2 |
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
2 |
|
if (is_array($value)) { |
|
74
|
2 |
|
if ($this->setAttributeValue($array[$key], $groupKey, $fieldKey, $newValue)) { |
|
75
|
2 |
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|