1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
4
|
|
|
* @license https://github.com/flipboxfactory/craft-sortable-associations/blob/master/LICENSE |
5
|
|
|
* @link https://github.com/flipboxfactory/craft-sortable-associations |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace flipbox\craft\domains\fields; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use craft\base\Element; |
12
|
|
|
use craft\base\ElementInterface; |
13
|
|
|
use craft\helpers\ArrayHelper; |
14
|
|
|
use flipbox\craft\domains\queries\DomainsQuery; |
15
|
|
|
use flipbox\craft\domains\records\Domain; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
* |
21
|
|
|
* @property int $id |
22
|
|
|
* @property string $defaultStatus |
23
|
|
|
*/ |
24
|
|
|
trait NormalizeValueTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function getQuery( |
30
|
|
|
ElementInterface $element = null |
31
|
|
|
): DomainsQuery { |
32
|
|
|
|
33
|
|
|
$query = Domain::find() |
34
|
|
|
->setSiteId($this->targetSiteId($element)) |
35
|
|
|
->setFieldId($this->id); |
36
|
|
|
|
37
|
|
|
return $query; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $value |
42
|
|
|
* @param ElementInterface|null $element |
43
|
|
|
* @return DomainsQuery |
44
|
|
|
*/ |
45
|
|
|
public function normalizeValue( |
46
|
|
|
$value, |
47
|
|
|
ElementInterface $element = null |
48
|
|
|
): DomainsQuery { |
49
|
|
|
if ($value instanceof DomainsQuery) { |
50
|
|
|
return $value; |
51
|
|
|
} |
52
|
|
|
$query = $this->getQuery($element); |
53
|
|
|
|
54
|
|
|
$this->normalizeQueryValue($query, $value, $element); |
55
|
|
|
|
56
|
|
|
return $query; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param DomainsQuery $query |
61
|
|
|
* @param ElementInterface|null $element |
62
|
|
|
*/ |
63
|
|
|
protected function normalizeQuery( |
64
|
|
|
DomainsQuery $query, |
65
|
|
|
ElementInterface $element = null |
66
|
|
|
) { |
67
|
|
|
$query->element = ( |
68
|
|
|
$element === null || $element->getId() === null |
69
|
|
|
) ? false : $element->getId(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param DomainsQuery $query |
74
|
|
|
* @param $value |
75
|
|
|
* @param ElementInterface|null $element |
76
|
|
|
*/ |
77
|
|
|
protected function normalizeQueryValue( |
78
|
|
|
DomainsQuery $query, |
79
|
|
|
$value, |
80
|
|
|
ElementInterface $element = null |
81
|
|
|
) { |
82
|
|
|
$this->normalizeQuery($query, $element); |
83
|
|
|
|
84
|
|
|
if (is_array($value)) { |
85
|
|
|
$this->normalizeQueryInputValues($query, $value, $element); |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($value === '') { |
90
|
|
|
$this->normalizeQueryEmptyValue($query); |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param DomainsQuery $query |
97
|
|
|
* @param array $value |
98
|
|
|
* @param ElementInterface|null $element |
99
|
|
|
*/ |
100
|
|
|
protected function normalizeQueryInputValues( |
101
|
|
|
DomainsQuery $query, |
102
|
|
|
array $value, |
103
|
|
|
ElementInterface $element = null |
104
|
|
|
) { |
105
|
|
|
$records = []; |
106
|
|
|
$sortOrder = 1; |
107
|
|
|
|
108
|
|
|
foreach ($value as $val) { |
109
|
|
|
$records[] = $this->normalizeQueryInputValue($val, $sortOrder, $element); |
110
|
|
|
} |
111
|
|
|
$query->setCachedResult($records); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritdoc |
116
|
|
|
*/ |
117
|
|
|
protected function normalizeQueryInputValue( |
118
|
|
|
$value, |
119
|
|
|
int &$sortOrder, |
120
|
|
|
ElementInterface $element = null |
121
|
|
|
): Domain { |
122
|
|
|
if (!is_array($value)) { |
123
|
|
|
$value = [ |
124
|
|
|
'domain' => $value, |
125
|
|
|
'status' => $this->defaultStatus |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$domain = ArrayHelper::getValue($value, 'domain'); |
130
|
|
|
$status = ArrayHelper::getValue($value, 'status'); |
131
|
|
|
|
132
|
|
|
$record = Domain::find() |
133
|
|
|
->setDomain($domain) |
134
|
|
|
->setField($this) |
135
|
|
|
->setElement($element) |
136
|
|
|
->setSiteId($this->targetSiteId($element)) |
137
|
|
|
->one(); |
138
|
|
|
|
139
|
|
|
if (empty($record)) { |
140
|
|
|
$record = new Domain([ |
141
|
|
|
'field' => $this, |
142
|
|
|
'element' => $element, |
143
|
|
|
'domain' => $domain, |
144
|
|
|
'siteId' => $this->targetSiteId($element) |
145
|
|
|
]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$record->status = $status; |
149
|
|
|
$record->sortOrder = $sortOrder++; |
150
|
|
|
|
151
|
|
|
return $record; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param DomainsQuery $query |
156
|
|
|
*/ |
157
|
|
|
protected function normalizeQueryEmptyValue( |
158
|
|
|
DomainsQuery $query |
159
|
|
|
) { |
160
|
|
|
$query->setCachedResult([]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the site ID that target elements should have. |
165
|
|
|
* |
166
|
|
|
* @param ElementInterface|Element|null $element |
167
|
|
|
* |
168
|
|
|
* @return int |
169
|
|
|
*/ |
170
|
|
|
protected function targetSiteId(ElementInterface $element = null): int |
171
|
|
|
{ |
172
|
|
|
/** @var Element $element */ |
173
|
|
|
if (Craft::$app->getIsMultiSite() === true && $element !== null) { |
174
|
|
|
return $element->siteId; |
175
|
|
|
} |
176
|
|
|
return Craft::$app->getSites()->currentSite->id; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|