1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-element-lists/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-element-lists/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\element\lists\fields; |
10
|
|
|
|
11
|
|
|
use craft\elements\db\ElementQuery; |
12
|
|
|
use craft\elements\db\ElementQueryInterface; |
13
|
|
|
use craft\helpers\Db; |
14
|
|
|
use flipbox\craft\element\lists\records\Association; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 2.0.0 |
19
|
|
|
* |
20
|
|
|
* @property int|null id |
21
|
|
|
*/ |
22
|
|
|
trait ModifyElementQueryTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
public function modifyElementsQuery(ElementQueryInterface $query, $value) |
28
|
|
|
{ |
29
|
|
|
if ($value === null || !$query instanceof ElementQuery) { |
30
|
|
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($value === false) { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (is_string($value)) { |
38
|
|
|
$this->modifyElementsQueryForStringValue($query, $value); |
39
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->modifyElementsQueryForTargetValue($query, $value); |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ElementQuery $query |
48
|
|
|
* @param string $value |
49
|
|
|
*/ |
50
|
|
|
protected function modifyElementsQueryForStringValue( |
51
|
|
|
ElementQuery $query, |
52
|
|
|
string $value |
53
|
|
|
) { |
54
|
|
|
if ($value === 'not :empty:') { |
55
|
|
|
$value = ':notempty:'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($value === ':notempty:' || $value === ':empty:') { |
59
|
|
|
$this->modifyElementsQueryForEmptyValue($query, $value); |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->modifyElementsQueryForTargetValue($query, $value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ElementQuery $query |
68
|
|
|
* @param $value |
69
|
|
|
*/ |
70
|
|
|
protected function modifyElementsQueryForTargetValue( |
71
|
|
|
ElementQuery $query, |
72
|
|
|
$value |
73
|
|
|
) { |
74
|
|
|
$alias = Association::tableAlias(); |
75
|
|
|
$name = Association::tableName(); |
76
|
|
|
|
77
|
|
|
$joinTable = "{$name} {$alias}"; |
78
|
|
|
$query->query->innerJoin($joinTable, "[[{$alias}.targetId]] = [[subquery.elementsId]]"); |
79
|
|
|
$query->subQuery->innerJoin($joinTable, "[[{$alias}.targetId]] = [[elements.id]]"); |
80
|
|
|
|
81
|
|
|
$query->subQuery->addSelect(["{$alias}.sortOrder"]); |
82
|
|
|
|
83
|
|
|
$query->subQuery->andWhere( |
84
|
|
|
Db::parseParam($alias . '.fieldId', $this->id) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$query->subQuery->andWhere( |
88
|
|
|
Db::parseParam($alias . '.sourceId', $value) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$query->query->distinct(true); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param ElementQuery $query |
96
|
|
|
* @param string $value |
97
|
|
|
*/ |
98
|
|
|
protected function modifyElementsQueryForEmptyValue( |
99
|
|
|
ElementQuery $query, |
100
|
|
|
string $value |
101
|
|
|
) { |
102
|
|
|
$alias = Association::tableAlias(); |
103
|
|
|
$name = Association::tableName(); |
104
|
|
|
|
105
|
|
|
$operator = ($value === ':notempty:' ? '!=' : '='); |
106
|
|
|
$query->subQuery->andWhere( |
107
|
|
|
"(select count([[{$alias}.targetId]]) from " . |
108
|
|
|
$name . |
109
|
|
|
" {{{$alias}}} where [[{$alias}.targetId" . |
110
|
|
|
"]] = [[elements.id]] and [[{$alias}.fieldId]] = {$this->id}) {$operator} 0" |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|