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\Query; |
12
|
|
|
use craft\elements\db\ElementQuery; |
13
|
|
|
use craft\helpers\Db; |
14
|
|
|
use craft\records\UserGroup_User as UserGroupUsersRecord; |
15
|
|
|
use flipbox\craft\ember\queries\UserAttributeTrait; |
16
|
|
|
use flipbox\craft\ember\queries\UserGroupAttributeTrait; |
17
|
|
|
use flipbox\organizations\elements\Organization as OrganizationElement; |
18
|
|
|
use flipbox\organizations\records\Organization as OrganizationRecord; |
19
|
|
|
use flipbox\organizations\records\OrganizationTypeAssociation as TypeAssociationRecord; |
20
|
|
|
use flipbox\organizations\records\UserAssociation as OrganizationUsersRecord; |
21
|
|
|
use flipbox\organizations\records\UserTypeAssociation as UserTypeAssociationRecord; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Flipbox Factory <[email protected]> |
25
|
|
|
* @since 1.0.0 |
26
|
|
|
* |
27
|
|
|
* @property Query $query |
28
|
|
|
* @property Query $subQuery |
29
|
|
|
* |
30
|
|
|
* @method OrganizationElement one($db = null) |
31
|
|
|
* @method OrganizationElement[] all($db = null) |
32
|
|
|
* @method OrganizationElement[] getCachedResult() |
33
|
|
|
*/ |
34
|
|
|
class OrganizationQuery extends ElementQuery |
35
|
|
|
{ |
36
|
|
|
use UserAttributeTrait, |
37
|
|
|
UserGroupAttributeTrait, |
38
|
|
|
UserTypeAttributeTrait, |
39
|
|
|
UserStateAttributeTrait, |
40
|
|
|
OrganizationTypeAttributeTrait; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var mixed When the resulting organizations must have joined. |
44
|
|
|
*/ |
45
|
|
|
public $dateJoined; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
protected function beforePrepare(): bool |
51
|
|
|
{ |
52
|
|
|
if (false === ($result = parent::beforePrepare())) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$alias = OrganizationRecord::tableAlias(); |
57
|
|
|
$this->joinElementTable($alias); |
58
|
|
|
|
59
|
|
|
$this->query->select([ |
60
|
|
|
$alias . '.dateJoined' |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
$this->prepareRelationsParams(); |
64
|
|
|
$this->prepareAttributes($alias); |
65
|
|
|
|
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Prepares simple attributes |
71
|
|
|
* |
72
|
|
|
* @var string $alias |
73
|
|
|
*/ |
74
|
|
|
protected function prepareAttributes(string $alias) |
75
|
|
|
{ |
76
|
|
|
if ($this->dateJoined) { |
77
|
|
|
$this->subQuery->andWhere(Db::parseDateParam($alias . '.dateJoined', $this->dateJoined)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Prepares relation params |
83
|
|
|
*/ |
84
|
|
|
protected function prepareRelationsParams() |
85
|
|
|
{ |
86
|
|
|
// Type |
87
|
|
|
$this->applyTypeParam(); |
88
|
|
|
|
89
|
|
|
if (empty($this->user) && empty($this->userGroup) && empty($this->userType)) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$alias = $this->joinOrganizationUserTable(); |
94
|
|
|
|
95
|
|
|
$this->applyUserParam($alias); |
96
|
|
|
$this->applyUserGroupParam($alias); |
97
|
|
|
$this->applyUserTypeParam($alias); |
98
|
|
|
$this->applyUserStateParam($alias); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/************************************************************ |
103
|
|
|
* JOIN TABLES |
104
|
|
|
************************************************************/ |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected function joinOrganizationUserTable(): string |
110
|
|
|
{ |
111
|
|
|
$alias = OrganizationUsersRecord::tableAlias(); |
112
|
|
|
|
113
|
|
|
$this->subQuery->innerJoin( |
114
|
|
|
OrganizationUsersRecord::tableName() . ' ' . $alias, |
115
|
|
|
'[[' . $alias . '.organizationId]] = [[elements.id]]' |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
// Check if we're ordering by one of the association tables order columns |
119
|
|
|
if (is_array($this->orderBy)) { |
120
|
|
|
$columns = ['userOrder' => 'userOrder', 'organizationOrder' => 'organizationOrder']; |
121
|
|
|
$matches = array_intersect_key($columns, $this->orderBy); |
122
|
|
|
|
123
|
|
|
foreach ($matches as $param => $select) { |
124
|
|
|
$this->subQuery->addSelect([$alias . '.' . $select]); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $alias; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
protected function joinOrganizationTypeTable(): string |
135
|
|
|
{ |
136
|
|
|
$alias = TypeAssociationRecord::tableAlias(); |
137
|
|
|
|
138
|
|
|
$this->subQuery->leftJoin( |
139
|
|
|
TypeAssociationRecord::tableName() . ' ' . $alias, |
140
|
|
|
'[[' . $alias . '.organizationId]] = [[elements.id]]' |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
return $alias; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/************************************************************ |
148
|
|
|
* USER |
149
|
|
|
************************************************************/ |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $alias |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
protected function applyUserParam(string $alias) |
157
|
|
|
{ |
158
|
|
|
if (empty($this->user)) { |
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->subQuery->andWhere( |
163
|
|
|
Db::parseParam($alias . '.userId', $this->parseUserValue($this->user)) |
164
|
|
|
); |
165
|
|
|
$this->subQuery->distinct(true); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/************************************************************ |
170
|
|
|
* USER GROUP |
171
|
|
|
************************************************************/ |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $alias |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
protected function applyUserGroupParam(string $alias) |
179
|
|
|
{ |
180
|
|
|
if (empty($this->userGroup)) { |
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$groupAlias = 'ug_user'; |
185
|
|
|
|
186
|
|
|
$this->subQuery->innerJoin( |
187
|
|
|
UserGroupUsersRecord::tableName() . ' ' . $groupAlias, |
188
|
|
|
'[[' . $groupAlias . '.userId]] = [[' . $alias . '.userId]]' |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$this->subQuery->andWhere( |
192
|
|
|
Db::parseParam($groupAlias . '.groupId', $this->parseUserGroupValue($this->userGroup)) |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
/************************************************************ |
198
|
|
|
* USER TYPE |
199
|
|
|
************************************************************/ |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $alias |
203
|
|
|
* |
204
|
|
|
* @return void |
205
|
|
|
*/ |
206
|
|
|
protected function applyUserTypeParam(string $alias) |
207
|
|
|
{ |
208
|
|
|
if (empty($this->userType)) { |
209
|
|
|
return; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$typeAlias = 'ut_user'; |
213
|
|
|
|
214
|
|
|
$this->subQuery->innerJoin( |
215
|
|
|
UserTypeAssociationRecord::tableName() . ' ' . $typeAlias, |
216
|
|
|
'[[' . $typeAlias . '.userId]] = [[' . $alias . '.userId]]' |
217
|
|
|
); |
218
|
|
|
|
219
|
|
|
$this->subQuery->andWhere( |
220
|
|
|
Db::parseParam($typeAlias . '.typeId', $this->parseUserTypeValue($this->userType)) |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/************************************************************ |
226
|
|
|
* USER STATE |
227
|
|
|
************************************************************/ |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param string $alias |
231
|
|
|
* |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
protected function applyUserStateParam(string $alias) |
235
|
|
|
{ |
236
|
|
|
if (empty($this->userState)) { |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$this->subQuery->andWhere( |
241
|
|
|
Db::parseParam($alias . '.state', $this->userState) |
242
|
|
|
); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/************************************************************ |
247
|
|
|
* TYPE |
248
|
|
|
************************************************************/ |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return void |
252
|
|
|
*/ |
253
|
|
|
protected function applyTypeParam() |
254
|
|
|
{ |
255
|
|
|
if (empty($this->organizationType)) { |
256
|
|
|
return; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$alias = $this->joinOrganizationTypeTable(); |
260
|
|
|
|
261
|
|
|
$this->subQuery |
262
|
|
|
->distinct(true) |
263
|
|
|
->andWhere( |
264
|
|
|
Db::parseParam($alias . '.typeId', $this->parseOrganizationTypeValue($this->organizationType)) |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|