1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\ember\objects; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\models\FieldLayout; |
13
|
|
|
use flipbox\craft\ember\helpers\ObjectHelper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @property int|null $fieldLayoutId |
17
|
|
|
* |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 2.0.0 |
20
|
|
|
*/ |
21
|
|
|
trait FieldLayoutMutatorTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var FieldLayout|null |
25
|
|
|
*/ |
26
|
|
|
private $fieldLayout; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
abstract static protected function fieldLayoutType(): string; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $id |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function setFieldLayoutId(int $id) |
38
|
|
|
{ |
39
|
|
|
$this->fieldLayoutId = $id; |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get associated fieldLayoutId |
45
|
|
|
* |
46
|
|
|
* @return int|null |
47
|
|
|
*/ |
48
|
|
|
public function getFieldLayoutId() |
49
|
|
|
{ |
50
|
|
|
if (null === $this->fieldLayoutId && null !== $this->fieldLayout) { |
51
|
|
|
$this->fieldLayoutId = $this->fieldLayout->id; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->fieldLayoutId; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param mixed $fieldLayout |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function setFieldLayout($fieldLayout = null) |
62
|
|
|
{ |
63
|
|
|
$this->fieldLayout = null; |
64
|
|
|
|
65
|
|
|
if (!$fieldLayout = $this->internalResolveFieldLayout($fieldLayout)) { |
66
|
|
|
$this->fieldLayout = $this->fieldLayoutId = null; |
67
|
|
|
} else { |
68
|
|
|
$this->fieldLayoutId = $fieldLayout->id; |
69
|
|
|
$this->fieldLayout = $fieldLayout; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return FieldLayout |
77
|
|
|
*/ |
78
|
|
|
public function getFieldLayout(): FieldLayout |
79
|
|
|
{ |
80
|
|
|
if ($this->fieldLayout === null) { |
81
|
|
|
if (!$fieldLayout = $this->resolveFieldLayout()) { |
82
|
|
|
$fieldLayout = $this->createFieldLayout(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->setFieldLayout($fieldLayout); |
86
|
|
|
return $this->setTypeOnFieldLayout($fieldLayout); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$fieldLayoutId = $this->fieldLayoutId; |
90
|
|
|
if ($fieldLayoutId !== null && |
91
|
|
|
$fieldLayoutId !== $this->fieldLayout->id |
92
|
|
|
) { |
93
|
|
|
$this->fieldLayout = null; |
94
|
|
|
return $this->getFieldLayout(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->setTypeOnFieldLayout($this->fieldLayout); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param FieldLayout $fieldLayout |
102
|
|
|
* @return FieldLayout |
103
|
|
|
*/ |
104
|
|
|
protected function setTypeOnFieldLayout(FieldLayout $fieldLayout): FieldLayout |
105
|
|
|
{ |
106
|
|
|
if ($fieldLayout->type === null) { |
107
|
|
|
$fieldLayout->type = static::fieldLayoutType(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $fieldLayout; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return FieldLayout|null |
115
|
|
|
*/ |
116
|
|
|
protected function resolveFieldLayout() |
117
|
|
|
{ |
118
|
|
|
if ($fieldLayoutModel = $this->resolveFieldLayoutFromId()) { |
119
|
|
|
return $fieldLayoutModel; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->resolveFieldLayoutFromType(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return FieldLayout|null |
127
|
|
|
*/ |
128
|
|
|
private function resolveFieldLayoutFromId() |
129
|
|
|
{ |
130
|
|
|
if (null === $this->fieldLayoutId) { |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return Craft::$app->getFields()->getLayoutById($this->fieldLayoutId); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return FieldLayout|null |
139
|
|
|
*/ |
140
|
|
|
private function resolveFieldLayoutFromType() |
141
|
|
|
{ |
142
|
|
|
return Craft::$app->getFields()->getLayoutByType( |
143
|
|
|
static::fieldLayoutType() |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return FieldLayout |
149
|
|
|
*/ |
150
|
|
|
private function createFieldLayout(): FieldLayout |
151
|
|
|
{ |
152
|
|
|
$fieldLayoutModel = new FieldLayout([ |
153
|
|
|
'type' => $this->fieldLayoutType() |
154
|
|
|
]); |
155
|
|
|
|
156
|
|
|
return $fieldLayoutModel; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $fieldLayout |
161
|
|
|
* @return FieldLayout|null |
162
|
|
|
*/ |
163
|
|
|
protected function internalResolveFieldLayout($fieldLayout = null): FieldLayout |
164
|
|
|
{ |
165
|
|
|
if ($fieldLayout instanceof FieldLayout) { |
166
|
|
|
return $fieldLayout; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (is_numeric($fieldLayout)) { |
170
|
|
|
return Craft::$app->getFields()->getLayoutById($fieldLayout); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (is_string($fieldLayout)) { |
174
|
|
|
return Craft::$app->getFields()->getLayoutByType($fieldLayout); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
try { |
178
|
|
|
$object = Craft::createObject(FieldLayout::class, [$fieldLayout]); |
179
|
|
|
} catch (\Exception $e) { |
180
|
|
|
$object = new FieldLayout(); |
181
|
|
|
ObjectHelper::populate( |
182
|
|
|
$object, |
183
|
|
|
$fieldLayout |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** @var FieldLayout $object */ |
188
|
|
|
return $object; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|