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\queries; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\db\QueryAbortedException; |
13
|
|
|
use craft\helpers\Db; |
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
|
|
|
* @author Flipbox Factory <[email protected]> |
23
|
|
|
* @since 2.0.0 |
24
|
|
|
* |
25
|
|
|
* @method IntegrationAssociation[] getCachedResult() |
26
|
|
|
* @method IntegrationAssociation[] all() |
27
|
|
|
* @method IntegrationAssociation one() |
28
|
|
|
*/ |
29
|
|
|
class IntegrationAssociationQuery extends CacheableActiveQuery |
30
|
|
|
{ |
31
|
|
|
use AuditAttributesTrait, |
32
|
|
|
FieldAttributeTrait, |
33
|
|
|
ElementAttributeTrait, |
34
|
|
|
ObjectAttributeTrait, |
35
|
|
|
SiteAttributeTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The sort order attribute |
39
|
|
|
*/ |
40
|
|
|
const SORT_ORDER_ATTRIBUTE = 'sortOrder'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The sort order direction |
44
|
|
|
*/ |
45
|
|
|
const SORT_ORDER_DIRECTION = SORT_ASC; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int|null Sort order |
49
|
|
|
*/ |
50
|
|
|
public $sortOrder; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function init() |
56
|
|
|
{ |
57
|
|
|
parent::init(); |
58
|
|
|
|
59
|
|
|
if ($this->select === null) { |
60
|
|
|
$this->select = ['*']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($this->orderBy === null && static::SORT_ORDER_ATTRIBUTE !== null) { |
64
|
|
|
$this->orderBy = [static::SORT_ORDER_ATTRIBUTE => static::SORT_ORDER_DIRECTION]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $value |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function sortOrder($value) |
73
|
|
|
{ |
74
|
|
|
$this->sortOrder = $value; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $value |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function setSortOrder($value) |
83
|
|
|
{ |
84
|
|
|
return $this->sortOrder($value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
* @throws QueryAbortedException |
90
|
|
|
*/ |
91
|
|
|
public function prepare($builder) |
92
|
|
|
{ |
93
|
|
|
// Is the query already doomed? |
94
|
|
|
if (($this->field !== null && empty($this->field)) || |
95
|
|
|
($this->object !== null && empty($this->object)) || |
96
|
|
|
($this->element !== null && empty($this->element)) |
97
|
|
|
) { |
98
|
|
|
throw new QueryAbortedException(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($this->sortOrder !== null) { |
102
|
|
|
$this->andWhere(Db::parseParam(static::SORT_ORDER_ATTRIBUTE, $this->sortOrder)); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->applyElementConditions(); |
106
|
|
|
$this->applyFieldConditions(); |
107
|
|
|
$this->applyObjectConditions(); |
108
|
|
|
$this->applySiteConditions(); |
109
|
|
|
$this->applyAuditAttributeConditions(); |
110
|
|
|
|
111
|
|
|
return parent::prepare($builder); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Apply attribute conditions |
116
|
|
|
*/ |
117
|
|
|
protected function applyElementConditions() |
118
|
|
|
{ |
119
|
|
|
if ($this->element !== null) { |
120
|
|
|
$this->andWhere(Db::parseParam('elementId', $this->parseElementValue($this->element))); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Apply attribute conditions |
126
|
|
|
*/ |
127
|
|
|
protected function applyFieldConditions() |
128
|
|
|
{ |
129
|
|
|
if ($this->field !== null) { |
130
|
|
|
$this->andWhere(Db::parseParam('fieldId', $this->parseFieldValue($this->field))); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Apply attribute conditions |
136
|
|
|
*/ |
137
|
|
|
protected function applySiteConditions() |
138
|
|
|
{ |
139
|
|
|
if ($this->site !== null) { |
140
|
|
|
$this->andWhere(Db::parseParam('siteId', $this->parseSiteValue($this->site))); |
|
|
|
|
141
|
|
|
} else { |
142
|
|
|
$this->andWhere(Db::parseParam('siteId', Craft::$app->getSites()->currentSite->id)); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.