modifyElementsQueryForStringValue()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 14
cp 0
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 20
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 flipbox\craft\element\lists\records\Association;
14
use flipbox\craft\ember\helpers\ArrayHelper;
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_array($value)) {
38
            $this->modifyElementsQueryForArrayValue($query, $value);
39
            return null;
40
        }
41
42
        if (is_string($value)) {
43
            $this->modifyElementsQueryForStringValue($query, $value);
44
            return null;
45
        }
46
47
        $this->modifyElementsQueryForTargetValue($query, $value);
48
        return null;
49
    }
50
51
    /**
52
     * @param ElementQuery $query
53
     * @param array $value
54
     * @return void
55
     */
56
    protected function modifyElementsQueryForArrayValue(
57
        ElementQuery $query,
58
        array $value
59
    ) {
60
        if (array_key_exists('source', $value)) {
61
            $this->modifyElementsQueryForStringValue(
62
                $query,
63
                $value['source'] ?: ':empty:',
64
                ArrayHelper::remove($value, 'sourceSiteId')
65
            );
66
            return null;
67
        }
68
69
        $this->modifyElementsQueryForTargetValue($query, $value);
70
    }
71
72
    /**
73
     * @param ElementQuery $query
74
     * @param string $value
75
     * @param $siteId
76
     */
77
    protected function modifyElementsQueryForStringValue(
78
        ElementQuery $query,
79
        string $value,
80
        $siteId = null
81
    ) {
82
        if ($value === 'not :empty:') {
83
            $value = ':notempty:';
84
        }
85
86
        if ($value === ':notempty:' || $value === ':empty:') {
87
            $this->modifyElementsQueryForEmptyValue($query, $value);
88
            return;
89
        }
90
91
        $this->modifyElementsQueryForTargetValue($query, $value, $siteId);
92
    }
93
94
    /**
95
     * @param ElementQuery $query
96
     * @param $sourceId
97
     * @param $siteId
98
     */
99
    protected function modifyElementsQueryForTargetValue(
100
        ElementQuery $query,
101
        $sourceId,
102
        $siteId = null
103
    ) {
104
        $alias = Association::tableAlias();
105
        $name = Association::tableName();
106
107
        $table = "{$name} {$alias}";
108
109
        if (!is_array($query->join) || !$query->isJoined($table)) {
110
            $query->innerJoin(
111
                $table,
112
                [
113
                    'and',
114
                    '[[' . $alias . '.targetId]] = [[elements.id]]',
115
                    [
116
                        $alias . '.sourceId' => $sourceId,
117
                        $alias . '.fieldId' => $this->id,
118
                    ],
119
                    [
120
                        'or',
121
                        [$alias . '.sourceSiteId' => null],
122
                        [$alias . '.sourceSiteId' => $siteId]
123
                    ]
124
                ]
125
            );
126
        }
127
    }
128
129
    /**
130
     * @param ElementQuery $query
131
     * @param string $value
132
     */
133
    protected function modifyElementsQueryForEmptyValue(
134
        ElementQuery $query,
135
        string $value
136
    ) {
137
        $alias = Association::tableAlias();
138
        $name = Association::tableName();
139
140
        $operator = ($value === ':notempty:' ? '!=' : '=');
141
        $query->subQuery->andWhere(
142
            "(select count([[{$alias}.targetId]]) from " .
143
            $name .
144
            " {{{$alias}}} where [[{$alias}.targetId" .
145
            "]] = [[elements.id]] and [[{$alias}.fieldId]] = {$this->id}) {$operator} 0"
146
        );
147
    }
148
}
149