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 craft\helpers\StringHelper; |
15
|
|
|
use flipbox\craft\ember\helpers\SiteHelper; |
16
|
|
|
use flipbox\craft\integration\queries\IntegrationAssociationQuery; |
17
|
|
|
use flipbox\craft\integration\records\IntegrationAssociation; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
* |
23
|
|
|
* @property int|null $max |
24
|
|
|
*/ |
25
|
|
|
trait NormalizeValueTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
abstract public static function recordClass(): string; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ElementInterface|Element|null $element |
34
|
|
|
* @return IntegrationAssociationQuery |
35
|
|
|
*/ |
36
|
|
|
protected function getQuery(ElementInterface $element = null): IntegrationAssociationQuery |
37
|
|
|
{ |
38
|
|
|
/** @var IntegrationAssociation $recordClass */ |
39
|
|
|
$recordClass = static::recordClass(); |
40
|
|
|
|
41
|
|
|
$query = $recordClass::find(); |
42
|
|
|
|
43
|
|
|
if ($this->max !== null) { |
44
|
|
|
$query->limit($this->max); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$query->field($this) |
48
|
|
|
->siteId(SiteHelper::ensureSiteId($element === null ? null : $element->siteId)) |
49
|
|
|
->elementId(($element === null || $element->getId() === null |
50
|
|
|
) ? false : $element->getId()); |
51
|
|
|
|
52
|
|
|
return $query; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $value |
57
|
|
|
* @param ElementInterface|null $element |
58
|
|
|
* @return IntegrationAssociationQuery |
59
|
|
|
*/ |
60
|
|
|
public function normalizeValue( |
61
|
|
|
$value, |
62
|
|
|
ElementInterface $element = null |
63
|
|
|
) |
64
|
|
|
{ |
65
|
|
|
|
66
|
|
|
if ($value instanceof IntegrationAssociationQuery) { |
67
|
|
|
return $value; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$query = $this->getQuery($element); |
71
|
|
|
$this->normalizeQueryValue($query, $value, $element); |
72
|
|
|
return $query; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param IntegrationAssociationQuery $query |
77
|
|
|
* @param $value |
78
|
|
|
* @param ElementInterface|null $element |
79
|
|
|
*/ |
80
|
|
|
protected function normalizeQueryValue( |
81
|
|
|
IntegrationAssociationQuery $query, |
82
|
|
|
$value, |
83
|
|
|
ElementInterface $element = null |
84
|
|
|
) |
85
|
|
|
{ |
86
|
|
|
|
87
|
|
|
if (is_array($value)) { |
88
|
|
|
$this->normalizeQueryInputValues($query, $value, $element); |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($value === '') { |
93
|
|
|
$this->normalizeQueryEmptyValue($query); |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param IntegrationAssociationQuery $query |
100
|
|
|
* @param array $value |
101
|
|
|
* @param ElementInterface|null $element |
102
|
|
|
*/ |
103
|
|
|
protected function normalizeQueryInputValues( |
104
|
|
|
IntegrationAssociationQuery $query, |
105
|
|
|
array $value, |
106
|
|
|
ElementInterface $element = null |
107
|
|
|
) |
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
$models = []; |
111
|
|
|
$sortOrder = 1; |
112
|
|
|
foreach ($value as $val) { |
113
|
|
|
$models[] = $this->normalizeQueryInputValue($val, $sortOrder, $element); |
114
|
|
|
} |
115
|
|
|
$query->setCachedResult($models); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $value |
120
|
|
|
* @param int $sortOrder |
121
|
|
|
* @param ElementInterface|null $element |
122
|
|
|
* @return IntegrationAssociation |
123
|
|
|
*/ |
124
|
|
|
protected function normalizeQueryInputValue( |
125
|
|
|
$value, |
126
|
|
|
int &$sortOrder, |
127
|
|
|
ElementInterface $element = null |
128
|
|
|
): IntegrationAssociation |
129
|
|
|
{ |
130
|
|
|
|
131
|
|
|
if (is_array($value)) { |
132
|
|
|
$value = StringHelper::toString($value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** @var IntegrationAssociation $recordClass */ |
136
|
|
|
$recordClass = static::recordClass(); |
137
|
|
|
|
138
|
|
|
/** @var IntegrationAssociation $association */ |
139
|
|
|
$association = new $recordClass(); |
140
|
|
|
$association->setField($this) |
141
|
|
|
->setElement($element) |
142
|
|
|
->setSiteId(SiteHelper::ensureSiteId($element === null ? null : $element->siteId)); |
143
|
|
|
|
144
|
|
|
$association->sortOrder = $sortOrder++; |
145
|
|
|
$association->objectId = $value; |
146
|
|
|
|
147
|
|
|
return $association; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param IntegrationAssociationQuery $query |
152
|
|
|
*/ |
153
|
|
|
protected function normalizeQueryEmptyValue( |
154
|
|
|
IntegrationAssociationQuery $query |
155
|
|
|
) |
156
|
|
|
{ |
157
|
|
|
$query->setCachedResult([]); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|