1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/organization/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/organization/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organizations\queries; |
10
|
|
|
|
11
|
|
|
use craft\db\QueryAbortedException; |
12
|
|
|
use craft\elements\db\UserQuery; |
13
|
|
|
use craft\helpers\Db; |
14
|
|
|
use flipbox\organizations\records\UserAssociation as OrganizationUsersRecord; |
15
|
|
|
use flipbox\organizations\records\UserTypeAssociation as UserCollectionUsersRecord; |
16
|
|
|
use yii\base\BaseObject; |
17
|
|
|
use yii\db\Query; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class UserQueryParamHandler extends BaseObject |
24
|
|
|
{ |
25
|
|
|
use OrganizationAttributeTrait, |
26
|
|
|
OrganizationTypeAttributeTrait, |
27
|
|
|
UserTypeAttributeTrait { |
28
|
|
|
setUserType as parentSetUserType; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var OrganizationAttributesToUserQueryBehavior |
33
|
|
|
*/ |
34
|
|
|
private $owner; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string|string[]|null |
38
|
|
|
*/ |
39
|
|
|
public $userState; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string|string[]|null $value |
43
|
|
|
* @return UserQuery |
44
|
|
|
*/ |
45
|
|
|
public function setUserState($value): UserQuery |
46
|
|
|
{ |
47
|
|
|
$this->userState = $value; |
48
|
|
|
return $this->owner->owner; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string|string[]|null $value |
53
|
|
|
* @return UserQuery |
54
|
|
|
*/ |
55
|
|
|
public function userState($value): UserQuery |
56
|
|
|
{ |
57
|
|
|
return $this->setUserState($value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
* @param OrganizationAttributesToUserQueryBehavior $owner |
63
|
|
|
*/ |
64
|
|
|
public function __construct(OrganizationAttributesToUserQueryBehavior $owner, array $config = []) |
65
|
|
|
{ |
66
|
|
|
$this->owner = $owner; |
67
|
|
|
parent::__construct($config); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function setUserType($value): UserQuery |
74
|
|
|
{ |
75
|
|
|
$this->parentSetUserType($value); |
76
|
|
|
return $this->owner->owner; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param UserQuery $query |
81
|
|
|
* @throws QueryAbortedException |
82
|
|
|
*/ |
83
|
|
|
public function applyParams(UserQuery $query) |
84
|
|
|
{ |
85
|
|
|
if ($query->subQuery === null || |
86
|
|
|
( |
87
|
|
|
$this->organization === null && |
88
|
|
|
$this->organizationType === null && |
89
|
|
|
$this->userType === null && |
90
|
|
|
$this->userState === null |
91
|
|
|
) |
92
|
|
|
) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->joinOrganizationUserTable($query); |
97
|
|
|
|
98
|
|
|
$this->applyOrganizationParam( |
99
|
|
|
$query, |
100
|
|
|
$this->organization |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$this->applyUserTypeParam( |
104
|
|
|
$query, |
105
|
|
|
$this->userType |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$this->applyUserStateParam( |
109
|
|
|
$query, |
110
|
|
|
$this->userState |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/************************************************************ |
115
|
|
|
* JOIN TABLES |
116
|
|
|
************************************************************/ |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritdoc |
120
|
|
|
*/ |
121
|
|
|
protected function joinOrganizationUserTable(UserQuery $query) |
122
|
|
|
{ |
123
|
|
|
$alias = OrganizationUsersRecord::tableAlias(); |
124
|
|
|
|
125
|
|
|
$query->subQuery->leftJoin( |
126
|
|
|
OrganizationUsersRecord::tableName() . ' ' . $alias, |
127
|
|
|
'[[' . $alias . '.userId]] = [[elements.id]]' |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
// Check if we're ordering by one of the association table's order columns |
131
|
|
|
if (is_array($query->orderBy)) { |
132
|
|
|
$columns = ['userOrder' => 'userOrder', 'organizationOrder' => 'organizationOrder']; |
133
|
|
|
$matches = array_intersect_key($columns, $query->orderBy); |
134
|
|
|
|
135
|
|
|
foreach ($matches as $param => $select) { |
136
|
|
|
$query->subQuery->addSelect([$alias . '.' . $select]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $alias; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritdoc |
145
|
|
|
*/ |
146
|
|
|
protected function joinOrganizationUserTypeTable(Query $query) |
147
|
|
|
{ |
148
|
|
|
$alias = UserCollectionUsersRecord::tableAlias(); |
149
|
|
|
$query->leftJoin( |
150
|
|
|
UserCollectionUsersRecord::tableName() . ' ' . $alias, |
151
|
|
|
'[[' . $alias . '.userId]] = [[' . OrganizationUsersRecord::tableAlias() . '.id]]' |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/************************************************************ |
157
|
|
|
* ORGANIZATION |
158
|
|
|
************************************************************/ |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param UserQuery $query |
162
|
|
|
* @param $organization |
163
|
|
|
* |
164
|
|
|
* @throws QueryAbortedException |
165
|
|
|
*/ |
166
|
|
|
protected function applyOrganizationParam(UserQuery $query, $organization) |
167
|
|
|
{ |
168
|
|
|
// Is the query already doomed? |
169
|
|
|
if ($organization !== null && empty($organization)) { |
170
|
|
|
throw new QueryAbortedException(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (empty($organization)) { |
174
|
|
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$query->subQuery->andWhere( |
178
|
|
|
Db::parseParam( |
179
|
|
|
OrganizationUsersRecord::tableAlias() . '.organizationId', |
180
|
|
|
$this->parseOrganizationValue($organization) |
181
|
|
|
) |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/************************************************************ |
187
|
|
|
* USER TYPE |
188
|
|
|
************************************************************/ |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param UserQuery $query |
192
|
|
|
* @param $type |
193
|
|
|
* |
194
|
|
|
* @throws QueryAbortedException |
195
|
|
|
*/ |
196
|
|
|
protected function applyUserTypeParam(UserQuery $query, $type) |
197
|
|
|
{ |
198
|
|
|
// Is the query already doomed? |
199
|
|
|
if ($type !== null && empty($type)) { |
200
|
|
|
throw new QueryAbortedException(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (empty($type)) { |
204
|
|
|
return; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this->joinOrganizationUserTypeTable($query->subQuery); |
|
|
|
|
208
|
|
|
$query->subQuery |
209
|
|
|
->andWhere( |
210
|
|
|
Db::parseParam( |
211
|
|
|
UserCollectionUsersRecord::tableAlias() . '.typeId', |
212
|
|
|
$this->parseUserTypeValue($type) |
213
|
|
|
) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
/************************************************************ |
219
|
|
|
* USER STATE |
220
|
|
|
************************************************************/ |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param UserQuery $query |
224
|
|
|
* @param $state |
225
|
|
|
* |
226
|
|
|
* @throws QueryAbortedException |
227
|
|
|
*/ |
228
|
|
|
protected function applyUserStateParam(UserQuery $query, $state) |
229
|
|
|
{ |
230
|
|
|
// Is the query already doomed? |
231
|
|
|
if ($state !== null && empty($state)) { |
232
|
|
|
throw new QueryAbortedException(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if (empty($state)) { |
236
|
|
|
return; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$query->subQuery->andWhere( |
240
|
|
|
Db::parseParam( |
241
|
|
|
OrganizationUsersRecord::tableAlias() . '.state', |
242
|
|
|
$state |
243
|
|
|
) |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: