OrganizationQuery   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 13
dl 0
loc 237
ccs 0
cts 114
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A beforePrepare() 0 18 2
A prepareAttributes() 0 6 2
A prepareRelationsParams() 0 16 4
A joinOrganizationUserTable() 0 21 3
A joinOrganizationTypeTable() 0 11 1
A applyUserParam() 0 10 2
A applyUserGroupParam() 0 17 2
A applyUserTypeParam() 0 17 2
A applyUserStateParam() 0 10 2
A applyTypeParam() 0 13 2
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 $defaultOrderBy = ['dateJoined' => SORT_DESC];
51
52
    /**
53
     * @inheritdoc
54
     */
55
    protected function beforePrepare(): bool
56
    {
57
        if (false === ($result = parent::beforePrepare())) {
58
            return false;
59
        }
60
61
        $alias = OrganizationRecord::tableAlias();
62
        $this->joinElementTable($alias);
63
64
        $this->query->select([
65
            $alias . '.dateJoined'
66
        ]);
67
68
        $this->prepareRelationsParams();
69
        $this->prepareAttributes($alias);
70
71
        return true;
72
    }
73
74
    /**
75
     * Prepares simple attributes
76
     *
77
     * @var string $alias
78
     */
79
    protected function prepareAttributes(string $alias)
80
    {
81
        if ($this->dateJoined) {
82
            $this->subQuery->andWhere(Db::parseDateParam($alias . '.dateJoined', $this->dateJoined));
83
        }
84
    }
85
86
    /**
87
     * Prepares relation params
88
     */
89
    protected function prepareRelationsParams()
90
    {
91
        // Type
92
        $this->applyTypeParam();
93
94
        if (empty($this->user) && empty($this->userGroup) && empty($this->userType)) {
95
            return;
96
        }
97
98
        $alias = $this->joinOrganizationUserTable();
99
100
        $this->applyUserParam($alias);
101
        $this->applyUserGroupParam($alias);
102
        $this->applyUserTypeParam($alias);
103
        $this->applyUserStateParam($alias);
104
    }
105
106
107
    /************************************************************
108
     * JOIN TABLES
109
     ************************************************************/
110
111
    /**
112
     * @return string
113
     */
114
    protected function joinOrganizationUserTable(): string
115
    {
116
        $alias = OrganizationUsersRecord::tableAlias();
117
118
        $this->subQuery->innerJoin(
119
            OrganizationUsersRecord::tableName() . ' ' . $alias,
120
            '[[' . $alias . '.organizationId]] = [[elements.id]]'
121
        );
122
123
        // Check if we're ordering by one of the association tables order columns
124
        if (is_array($this->orderBy)) {
125
            $columns = ['userOrder' => 'userOrder', 'organizationOrder' => 'organizationOrder'];
126
            $matches = array_intersect_key($columns, $this->orderBy);
127
128
            foreach ($matches as $param => $select) {
129
                $this->subQuery->addSelect([$alias . '.' . $select]);
130
            }
131
        }
132
133
        return $alias;
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    protected function joinOrganizationTypeTable(): string
140
    {
141
        $alias = TypeAssociationRecord::tableAlias();
142
143
        $this->subQuery->leftJoin(
144
            TypeAssociationRecord::tableName() . ' ' . $alias,
145
            '[[' . $alias . '.organizationId]] = [[elements.id]]'
146
        );
147
148
        return $alias;
149
    }
150
151
152
    /************************************************************
153
     * USER
154
     ************************************************************/
155
156
    /**
157
     * @param string $alias
158
     *
159
     * @return void
160
     */
161
    protected function applyUserParam(string $alias)
162
    {
163
        if (empty($this->user)) {
164
            return;
165
        }
166
167
        $this->subQuery->andWhere(
168
            Db::parseParam($alias . '.userId', $this->parseUserValue($this->user))
169
        );
170
    }
171
172
173
    /************************************************************
174
     * USER GROUP
175
     ************************************************************/
176
177
    /**
178
     * @param string $alias
179
     *
180
     * @return void
181
     */
182
    protected function applyUserGroupParam(string $alias)
183
    {
184
        if (empty($this->userGroup)) {
185
            return;
186
        }
187
188
        $groupAlias = 'ug_user';
189
190
        $this->subQuery->innerJoin(
191
            UserGroupUsersRecord::tableName() . ' ' . $groupAlias,
192
            '[[' . $groupAlias . '.userId]] = [[' . $alias . '.userId]]'
193
        );
194
195
        $this->subQuery->andWhere(
196
            Db::parseParam($groupAlias . '.groupId', $this->parseUserGroupValue($this->userGroup))
197
        );
198
    }
199
200
201
    /************************************************************
202
     * USER TYPE
203
     ************************************************************/
204
205
    /**
206
     * @param string $alias
207
     *
208
     * @return void
209
     */
210
    protected function applyUserTypeParam(string $alias)
211
    {
212
        if (empty($this->userType)) {
213
            return;
214
        }
215
216
        $typeAlias = 'ut_user';
217
218
        $this->subQuery->innerJoin(
219
            UserTypeAssociationRecord::tableName() . ' ' . $typeAlias,
220
            '[[' . $typeAlias . '.userId]] = [[' . $alias . '.userId]]'
221
        );
222
223
        $this->subQuery->andWhere(
224
            Db::parseParam($typeAlias . '.typeId', $this->parseUserTypeValue($this->userType))
225
        );
226
    }
227
228
229
    /************************************************************
230
     * USER STATE
231
     ************************************************************/
232
233
    /**
234
     * @param string $alias
235
     *
236
     * @return void
237
     */
238
    protected function applyUserStateParam(string $alias)
239
    {
240
        if (empty($this->userState)) {
241
            return;
242
        }
243
244
        $this->subQuery->andWhere(
245
            Db::parseParam($alias . '.state', $this->userState)
246
        );
247
    }
248
249
250
    /************************************************************
251
     * TYPE
252
     ************************************************************/
253
254
    /**
255
     * @return void
256
     */
257
    protected function applyTypeParam()
258
    {
259
        if (empty($this->organizationType)) {
260
            return;
261
        }
262
263
        $alias = $this->joinOrganizationTypeTable();
264
265
        $this->subQuery
266
            ->andWhere(
267
                Db::parseParam($alias . '.typeId', $this->parseOrganizationTypeValue($this->organizationType))
268
            );
269
    }
270
}
271