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\organization\helpers; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\elements\db\ElementQuery; |
13
|
|
|
use craft\elements\db\ElementQueryInterface; |
14
|
|
|
use craft\helpers\Db; |
15
|
|
|
use craft\helpers\StringHelper; |
16
|
|
|
use flipbox\organization\records\Organization as OrganizationRecord; |
17
|
|
|
use flipbox\organization\records\User as OrganizationUsersRecord; |
18
|
|
|
use flipbox\spark\helpers\ArrayHelper; |
19
|
|
|
use flipbox\spark\helpers\QueryHelper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Flipbox Factory <[email protected]> |
23
|
|
|
* @since 1.0.0 |
24
|
|
|
*/ |
25
|
|
|
class Query extends QueryHelper |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $operators = ['not ', '!=', '<=', '>=', '<', '>', '=']; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ElementQueryInterface $query |
35
|
|
|
* @param array $params |
36
|
|
|
*/ |
37
|
|
|
public static function applyOrganizationParam(ElementQueryInterface $query, array $params = []) |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
if (array_key_exists('owner', $params)) { |
41
|
|
|
self::applyOrganizationOwnerParam($query, $params['owner']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (array_key_exists('user', $params)) { |
45
|
|
|
self::applyOrganizationUserParam($query, $params['user']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (array_key_exists('member', $params)) { |
49
|
|
|
self::applyOrganizationMemberParam($query, $params['member']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $value |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public static function parseUserValue($value) |
60
|
|
|
{ |
61
|
|
|
|
62
|
|
|
// Default join type |
63
|
|
|
$join = 'and'; |
64
|
|
|
|
65
|
|
|
// Parse as single param? |
66
|
|
|
if (false === static::parseBaseParam($value, $join)) { |
67
|
|
|
// Add one by one |
68
|
|
|
foreach ($value as $operator => &$v) { |
69
|
|
|
// attempt to assemble value (return false if it's a handle) |
70
|
|
|
if (false === static::findParamValue($v, $operator)) { |
71
|
|
|
// get element by string |
72
|
|
|
if (is_string($v)) { |
73
|
|
|
if ($element = Craft::$app->getUsers()->getUserByUsernameOrEmail($v)) { |
74
|
|
|
$v = $element->id; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($v instanceof static) { |
79
|
|
|
$v = $v->id; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($v) { |
83
|
|
|
$v = static::assembleParamValue($v, $operator); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// parse param to allow for mixed variables |
90
|
|
|
return array_merge([$join], ArrayHelper::filterEmptyStringsFromArray($value)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Standard param parsing. |
95
|
|
|
* |
96
|
|
|
* @param $value |
97
|
|
|
* @param $join |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public static function parseBaseParam(&$value, &$join) |
101
|
|
|
{ |
102
|
|
|
|
103
|
|
|
// Force array |
104
|
|
|
// This is causing some crazy recursive error issue |
105
|
|
|
// ... adding a simple array cast below. |
106
|
|
|
/* $value = ArrayHelper::toArray($value); */ |
107
|
|
|
if (!is_array($value)) { |
108
|
|
|
$value = [$value]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Get join type ('and' , 'or') |
112
|
|
|
$join = static::getJoinType($value, $join); |
|
|
|
|
113
|
|
|
|
114
|
|
|
// Check for object array (via 'id' key) |
115
|
|
|
if ($id = static::findIdFromObjectArray($value)) { |
|
|
|
|
116
|
|
|
$value = [$id]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Format the param value so that we return a string w/ a prepended operator. |
124
|
|
|
* |
125
|
|
|
* @param $value |
126
|
|
|
* @param $operator |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public static function assembleParamValue($value, $operator) |
130
|
|
|
{ |
131
|
|
|
|
132
|
|
|
// Handle arrays as values |
133
|
|
|
if (is_array($value) || is_object($value)) { |
134
|
|
|
// Look for an 'id' key in an array |
135
|
|
|
if ($id = static::findIdFromObjectArray($value, $operator)) { |
|
|
|
|
136
|
|
|
// Prepend the operator |
137
|
|
|
return static::prependOperator($id, $operator); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return static::prependOperator($value, $operator); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Attempt to resolve a param value by the value. |
146
|
|
|
* Return false if a 'handle' or other string identifier is detected. |
147
|
|
|
* |
148
|
|
|
* @param $value |
149
|
|
|
* @param $operator |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public static function findParamValue(&$value, &$operator) |
153
|
|
|
{ |
154
|
|
|
|
155
|
|
|
if (is_array($value) || is_object($value)) { |
156
|
|
|
$value = static::assembleParamValue($value, $operator); |
157
|
|
|
} else { |
158
|
|
|
static::normalizeEmptyValue($value); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$operator = static::parseParamOperator($value); |
|
|
|
|
161
|
|
|
|
162
|
|
|
if (is_numeric($value)) { |
163
|
|
|
$value = static::assembleParamValue($value, $operator); |
164
|
|
|
} else { |
165
|
|
|
$value = StringHelper::toLowerCase($value); |
166
|
|
|
|
167
|
|
|
if ($value !== ':empty:' || $value !== 'not :empty:') { |
168
|
|
|
// Trim any whitespace from the value |
169
|
|
|
$value = StringHelper::trim($value); |
170
|
|
|
|
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return true; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Attempt to resolve a param value by the value. |
181
|
|
|
* Return false if a 'handle' or other string identifier is detected. |
182
|
|
|
* |
183
|
|
|
* @param $value |
184
|
|
|
* @param $operator |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public static function prepParamValue(&$value, &$operator) |
188
|
|
|
{ |
189
|
|
|
|
190
|
|
|
if (is_array($value)) { |
191
|
|
|
return true; |
192
|
|
|
} else { |
193
|
|
|
static::normalizeEmptyValue($value); |
|
|
|
|
194
|
|
|
$operator = static::parseParamOperator($value); |
|
|
|
|
195
|
|
|
|
196
|
|
|
if (is_numeric($value)) { |
197
|
|
|
return true; |
198
|
|
|
} else { |
199
|
|
|
$value = StringHelper::toLowerCase($value); |
200
|
|
|
|
201
|
|
|
if ($value !== ':empty:' || $value !== 'not :empty:') { |
202
|
|
|
// Trim any whitespace from the value |
203
|
|
|
$value = StringHelper::trim($value); |
204
|
|
|
|
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return true; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param $value |
215
|
|
|
* @param string $default |
216
|
|
|
* @return mixed|string |
217
|
|
|
*/ |
218
|
|
|
private static function getJoinType(&$value, $default = 'or') |
219
|
|
|
{ |
220
|
|
|
|
221
|
|
|
// Get first value in array |
222
|
|
|
$joinType = ArrayHelper::firstValue($value); |
223
|
|
|
|
224
|
|
|
// Make sure first value is a string |
225
|
|
|
$firstVal = is_string($joinType) ? StringHelper::toLowerCase($joinType) : ''; |
226
|
|
|
|
227
|
|
|
if ($firstVal == 'and' || $firstVal == 'or') { |
228
|
|
|
$join = array_shift($value); |
229
|
|
|
} else { |
230
|
|
|
$join = $default; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $join; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Attempt to get a numeric value from an object array. |
238
|
|
|
* @param $value |
239
|
|
|
* @param null $operator |
240
|
|
|
* @return mixed|string |
241
|
|
|
*/ |
242
|
|
|
private static function findIdFromObjectArray($value, $operator = null) |
243
|
|
|
{ |
244
|
|
|
|
245
|
|
|
if ($id = ArrayHelper::getValue($value, 'id', '')) { |
246
|
|
|
return static::prependOperator($id, $operator); |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $id; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Prepend the operator to a value |
254
|
|
|
* |
255
|
|
|
* @param $value |
256
|
|
|
* @param null $operator |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
private static function prependOperator($value, $operator = null) |
260
|
|
|
{ |
261
|
|
|
|
262
|
|
|
if ($operator) { |
263
|
|
|
$operator = StringHelper::toLowerCase($operator); |
264
|
|
|
|
265
|
|
|
if (in_array($operator, static::$operators) || $operator === 'not') { |
|
|
|
|
266
|
|
|
if (is_array($value)) { |
267
|
|
|
$values = []; |
268
|
|
|
|
269
|
|
|
foreach ($value as $v) { |
270
|
|
|
$values[] = $operator . ($operator === 'not' ? ' ' : '') . $v; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $values; |
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $operator . ($operator === 'not' ? ' ' : '') . $value; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $value; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Normalizes “empty” values. |
285
|
|
|
* |
286
|
|
|
* @param string &$value The param value. |
287
|
|
|
*/ |
288
|
|
|
private static function normalizeEmptyValue(&$value) |
289
|
|
|
{ |
290
|
|
|
if ($value === null) { |
291
|
|
|
$value = ':empty:'; |
292
|
|
|
} else { |
293
|
|
|
if (StringHelper::toLowerCase($value) == ':notempty:') { |
294
|
|
|
$value = 'not :empty:'; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Extracts the operator from a DB param and returns it. |
301
|
|
|
* |
302
|
|
|
* @param string &$value Te param value. |
303
|
|
|
* |
304
|
|
|
* @return string The operator. |
305
|
|
|
*/ |
306
|
|
|
private static function parseParamOperator(&$value) |
307
|
|
|
{ |
308
|
|
|
foreach (static::$operators as $testOperator) { |
|
|
|
|
309
|
|
|
// Does the value start with this operator? |
310
|
|
|
$operatorLength = strlen($testOperator); |
311
|
|
|
|
312
|
|
|
if (strncmp( |
313
|
|
|
StringHelper::toLowerCase($value), |
314
|
|
|
$testOperator, |
315
|
|
|
$operatorLength |
316
|
|
|
) == 0 |
317
|
|
|
) { |
318
|
|
|
$value = mb_substr($value, $operatorLength); |
319
|
|
|
|
320
|
|
|
if ($testOperator == 'not ') { |
321
|
|
|
return 'not'; |
322
|
|
|
} else { |
323
|
|
|
return $testOperator; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return ''; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param ElementQueryInterface $query |
334
|
|
|
* @param $owner |
335
|
|
|
* |
336
|
|
|
* @return void |
337
|
|
|
*/ |
338
|
|
|
private static function applyOrganizationOwnerParam(ElementQueryInterface $query, $owner) |
339
|
|
|
{ |
340
|
|
|
|
341
|
|
|
/** @var ElementQuery $query */ |
342
|
|
|
|
343
|
|
|
$value = self::parseUserValue($owner); |
344
|
|
|
|
345
|
|
|
$alias = OrganizationUsersRecord::tableAlias(); |
346
|
|
|
|
347
|
|
|
$query->subQuery->leftJoin( |
348
|
|
|
OrganizationRecord::tableName() . ' ' . $alias, |
349
|
|
|
$alias . '.ownerId=users.id' |
350
|
|
|
); |
351
|
|
|
$query->subQuery->andWhere(Db::parseParam($alias . '.id', $value)); |
352
|
|
|
|
353
|
|
|
return; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @param ElementQueryInterface $query |
358
|
|
|
* @param $user |
359
|
|
|
* |
360
|
|
|
* @return void |
361
|
|
|
*/ |
362
|
|
|
private static function applyOrganizationUserParam(ElementQueryInterface $query, $user) |
363
|
|
|
{ |
364
|
|
|
|
365
|
|
|
/** @var ElementQuery $query */ |
366
|
|
|
|
367
|
|
|
$value = self::parseUserValue($user); |
368
|
|
|
|
369
|
|
|
$alias = OrganizationUsersRecord::tableAlias() . StringHelper::randomString(12); |
370
|
|
|
|
371
|
|
|
$query->subQuery->leftJoin( |
372
|
|
|
OrganizationUsersRecord::tableName() . ' ' . $alias, |
373
|
|
|
$alias . '.userId=users.id' |
374
|
|
|
); |
375
|
|
|
$query->subQuery->andWhere(Db::parseParam($alias . '.organizationId', $value)); |
376
|
|
|
|
377
|
|
|
return; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @param ElementQueryInterface $query |
382
|
|
|
* @param $member |
383
|
|
|
* |
384
|
|
|
* @return void |
385
|
|
|
*/ |
386
|
|
|
private static function applyOrganizationMemberParam(ElementQueryInterface $query, $member) |
387
|
|
|
{ |
388
|
|
|
|
389
|
|
|
/** @var ElementQuery $query */ |
390
|
|
|
|
391
|
|
|
$value = self::parseUserValue($member); |
392
|
|
|
|
393
|
|
|
$userAlias = OrganizationUsersRecord::tableAlias(); |
394
|
|
|
$alias = OrganizationRecord::tableAlias(); |
395
|
|
|
|
396
|
|
|
$query->subQuery->leftJoin( |
397
|
|
|
OrganizationUsersRecord::tableName() . ' ' . $userAlias, |
398
|
|
|
$userAlias . '.userId=users.id' |
399
|
|
|
); |
400
|
|
|
|
401
|
|
|
$query->subQuery->leftJoin( |
402
|
|
|
OrganizationRecord::tableName() . ' ' . $alias, |
403
|
|
|
$alias . '.ownerId=users.id' |
404
|
|
|
); |
405
|
|
|
|
406
|
|
|
// If looking for empty, join on 'and' |
407
|
|
|
$joinType = in_array(':empty:', $value, true) ? 'and' : 'or'; |
408
|
|
|
|
409
|
|
|
|
410
|
|
|
$query->subQuery->andWhere([ |
411
|
|
|
$joinType, |
412
|
|
|
Db::parseParam($userAlias . '.organizationId', $value), |
413
|
|
|
Db::parseParam($alias . '.id', $value) |
414
|
|
|
]); |
415
|
|
|
|
416
|
|
|
return; |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: