1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-integration/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\integration\fields; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\Element; |
13
|
|
|
use craft\base\ElementInterface; |
14
|
|
|
use flipbox\craft\integration\db\IntegrationAssociationQuery; |
15
|
|
|
use yii\db\ActiveRecord; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 2.0.0 |
20
|
|
|
* |
21
|
|
|
* @property int $id |
22
|
|
|
* @property int $elementId |
23
|
|
|
* @property int $objectId |
24
|
|
|
*/ |
25
|
|
|
trait NormalizeValueTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
abstract public static function recordClass(): string; |
31
|
|
|
|
32
|
|
|
/******************************************* |
33
|
|
|
* NORMALIZE VALUE |
34
|
|
|
*******************************************/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $value |
38
|
|
|
* @param ElementInterface|null $element |
39
|
|
|
* @return IntegrationAssociationQuery |
40
|
|
|
*/ |
41
|
|
|
public function normalizeValue( |
42
|
|
|
$value, |
43
|
|
|
ElementInterface $element = null |
44
|
|
|
) { |
45
|
|
|
if ($value instanceof IntegrationAssociationQuery) { |
46
|
|
|
return $value; |
47
|
|
|
} |
48
|
|
|
$query = $this->getQuery($element); |
49
|
|
|
$this->normalizeQueryValue($query, $value, $element); |
50
|
|
|
return $query; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param ElementInterface|null $element |
55
|
|
|
* @return IntegrationAssociationQuery |
56
|
|
|
*/ |
57
|
|
|
protected function getQuery( |
58
|
|
|
ElementInterface $element = null |
59
|
|
|
): IntegrationAssociationQuery { |
60
|
|
|
|
61
|
|
|
/** @var ActiveRecord $recordClass */ |
62
|
|
|
$recordClass = static::recordClass(); |
63
|
|
|
|
64
|
|
|
/** @var IntegrationAssociationQuery $query */ |
65
|
|
|
$query = $recordClass::find(); |
66
|
|
|
|
67
|
|
|
$query |
68
|
|
|
->field($this->id) |
69
|
|
|
->element($element) |
70
|
|
|
->site($this->targetSiteId($element)); |
71
|
|
|
|
72
|
|
|
if ($this->max !== null) { |
73
|
|
|
$query->limit($this->max); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $query; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param IntegrationAssociationQuery $query |
81
|
|
|
* @param ElementInterface|null $element |
82
|
|
|
*/ |
83
|
|
|
protected function normalizeQuery( |
84
|
|
|
IntegrationAssociationQuery $query, |
85
|
|
|
ElementInterface $element = null |
86
|
|
|
) { |
87
|
|
|
$query->element = ( |
88
|
|
|
$element === null || $element->getId() === null |
89
|
|
|
) ? false : $element->getId(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param IntegrationAssociationQuery $query |
94
|
|
|
* @param $value |
95
|
|
|
* @param ElementInterface|null $element |
96
|
|
|
*/ |
97
|
|
|
protected function normalizeQueryValue( |
98
|
|
|
IntegrationAssociationQuery $query, |
99
|
|
|
$value, |
100
|
|
|
ElementInterface $element = null |
101
|
|
|
) { |
102
|
|
|
$this->normalizeQuery($query, $element); |
103
|
|
|
|
104
|
|
|
if (is_array($value)) { |
105
|
|
|
$this->normalizeQueryInputValues($query, $value, $element); |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($value === '') { |
110
|
|
|
$this->normalizeQueryEmptyValue($query); |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param IntegrationAssociationQuery $query |
117
|
|
|
* @param array $value |
118
|
|
|
* @param ElementInterface|null $element |
119
|
|
|
*/ |
120
|
|
|
protected function normalizeQueryInputValues( |
121
|
|
|
IntegrationAssociationQuery $query, |
122
|
|
|
array $value, |
123
|
|
|
ElementInterface $element = null |
124
|
|
|
) { |
125
|
|
|
$models = []; |
126
|
|
|
$sortOrder = 1; |
127
|
|
|
foreach ($value as $val) { |
128
|
|
|
$models[] = $this->normalizeQueryInputValue($val, $sortOrder, $element); |
129
|
|
|
} |
130
|
|
|
$query->setCachedResult($models); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $value |
135
|
|
|
* @param int $sortOrder |
136
|
|
|
* @param ElementInterface|null $element |
137
|
|
|
* @return IntegrationAssociationQuery |
138
|
|
|
*/ |
139
|
|
|
protected function normalizeQueryInputValue( |
140
|
|
|
$value, |
141
|
|
|
int &$sortOrder, |
142
|
|
|
ElementInterface $element = null |
143
|
|
|
): IntegrationAssociationQuery { |
144
|
|
|
|
145
|
|
|
if (is_string($value)) { |
146
|
|
|
$value = [ |
147
|
|
|
'objectId' => $value |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$recordClass = static::recordClass(); |
152
|
|
|
|
153
|
|
|
return new $recordClass(array_merge( |
154
|
|
|
(array)$value, |
155
|
|
|
[ |
156
|
|
|
'field' => $this, |
157
|
|
|
'element' => $element, |
158
|
|
|
'siteId' => $this->targetSiteId($element), |
159
|
|
|
'sortOrder' => $sortOrder++ |
160
|
|
|
] |
161
|
|
|
)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param IntegrationAssociationQuery $query |
166
|
|
|
*/ |
167
|
|
|
protected function normalizeQueryEmptyValue( |
168
|
|
|
IntegrationAssociationQuery $query |
169
|
|
|
) { |
170
|
|
|
$query->setCachedResult([]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns the site ID that target elements should have. |
176
|
|
|
* |
177
|
|
|
* @param ElementInterface|Element|null $element |
178
|
|
|
* |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
protected function targetSiteId(ElementInterface $element = null): int |
182
|
|
|
{ |
183
|
|
|
/** @var Element $element */ |
184
|
|
|
if (Craft::$app->getIsMultiSite() === true && $element !== null) { |
185
|
|
|
return $element->siteId; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return Craft::$app->getSites()->currentSite->id; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: