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\db; |
10
|
|
|
|
11
|
|
|
use craft\db\QueryAbortedException; |
12
|
|
|
use craft\helpers\Db; |
13
|
|
|
use flipbox\craft\ember\helpers\QueryHelper; |
14
|
|
|
use flipbox\craft\ember\queries\AuditAttributesTrait; |
15
|
|
|
use flipbox\craft\ember\queries\CacheableActiveQuery; |
16
|
|
|
use flipbox\craft\ember\queries\ElementAttributeTrait; |
17
|
|
|
use flipbox\craft\ember\queries\FieldAttributeTrait; |
18
|
|
|
use flipbox\craft\ember\queries\SiteAttributeTrait; |
19
|
|
|
use flipbox\craft\integration\records\IntegrationAssociation; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @method IntegrationAssociation[] getCachedResult() |
23
|
|
|
*/ |
24
|
|
|
abstract class IntegrationAssociationQuery extends CacheableActiveQuery |
25
|
|
|
{ |
26
|
|
|
use AuditAttributesTrait, |
27
|
|
|
FieldAttributeTrait, |
28
|
|
|
ElementAttributeTrait, |
29
|
|
|
SiteAttributeTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int|null Sort order |
33
|
|
|
*/ |
34
|
|
|
public $sortOrder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public function init() |
40
|
|
|
{ |
41
|
|
|
parent::init(); |
42
|
|
|
|
43
|
|
|
if ($this->orderBy === null) { |
44
|
|
|
$this->orderBy = ['sortOrder' => SORT_ASC]; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
* return static |
51
|
|
|
*/ |
52
|
|
|
public function sortOrder($value) |
53
|
|
|
{ |
54
|
|
|
$this->sortOrder = $value; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string|string[]|null |
60
|
|
|
*/ |
61
|
|
|
public $object; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string|string[]|null $value |
65
|
|
|
* @return static |
66
|
|
|
*/ |
67
|
|
|
public function setObjectId($value) |
68
|
|
|
{ |
69
|
|
|
return $this->setObject($value); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string|string[]|null $value |
74
|
|
|
* @return static |
75
|
|
|
*/ |
76
|
|
|
public function objectId($value) |
77
|
|
|
{ |
78
|
|
|
return $this->setObject($value); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string|string[]|null $value |
83
|
|
|
* @return static |
84
|
|
|
*/ |
85
|
|
|
public function setObject($value) |
86
|
|
|
{ |
87
|
|
|
$this->object = $value; |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string|string[]|null $value |
93
|
|
|
* @return static |
94
|
|
|
*/ |
95
|
|
|
public function object($value) |
96
|
|
|
{ |
97
|
|
|
return $this->setObject($value); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritdoc |
102
|
|
|
*/ |
103
|
|
|
protected function fixedOrderColumn(): string |
104
|
|
|
{ |
105
|
|
|
return 'objectId'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $config |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function configure(array $config) |
113
|
|
|
{ |
114
|
|
|
QueryHelper::configure( |
115
|
|
|
$this, |
116
|
|
|
$config |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritdoc |
124
|
|
|
* |
125
|
|
|
* @throws QueryAbortedException if it can be determined that there won’t be any results |
126
|
|
|
*/ |
127
|
|
|
public function prepare($builder) |
128
|
|
|
{ |
129
|
|
|
// Is the query already doomed? |
130
|
|
|
if (($this->field !== null && empty($this->field)) || |
131
|
|
|
($this->object !== null && empty($this->object)) || |
132
|
|
|
($this->element !== null && empty($this->element)) |
133
|
|
|
) { |
134
|
|
|
throw new QueryAbortedException(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->applyConditions(); |
138
|
|
|
|
139
|
|
|
return parent::prepare($builder); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Apply query specific conditions |
144
|
|
|
*/ |
145
|
|
|
protected function applyConditions() |
146
|
|
|
{ |
147
|
|
|
if ($this->object !== null) { |
148
|
|
|
$this->andWhere(Db::parseParam('objectId', $this->object)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($this->element !== null) { |
152
|
|
|
$this->andWhere(Db::parseParam('elementId', $this->parseElementValue($this->element))); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($this->field !== null) { |
156
|
|
|
$this->andWhere(Db::parseParam('fieldId', $this->parseFieldValue($this->field))); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($this->site !== null) { |
160
|
|
|
$this->andWhere(Db::parseParam('siteId', $this->parseSiteValue($this->site))); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ($this->sortOrder !== null) { |
164
|
|
|
$this->andWhere(Db::parseParam('sortOrder', $this->sortOrder)); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|