Completed
Push — develop ( e9f396...2b8a13 )
by Nate
04:23
created

UsersAttributeTrait::userQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 16
cp 0
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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\elements;
10
11
use Craft;
12
use craft\db\Query;
13
use craft\elements\db\UserQuery;
14
use craft\elements\User;
15
use craft\helpers\ArrayHelper;
16
use flipbox\craft\ember\helpers\QueryHelper;
17
use flipbox\organizations\objects\UsersAssociatedToOrganizationManager;
18
use flipbox\organizations\records\UserAssociation;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @mixin Organization
25
 */
26
trait UsersAttributeTrait
27
{
28
    /**
29
     * @var UsersAssociatedToOrganizationManager
30
     */
31
    private $manager;
32
33
    /**
34
     * @return UsersAssociatedToOrganizationManager
35
     */
36
    public function getUserManager(): UsersAssociatedToOrganizationManager
37
    {
38
        if (null === $this->manager) {
39
            $this->manager = new UsersAssociatedToOrganizationManager($this);
40
        }
41
42
        return $this->manager;
43
    }
44
45
    /**
46
     * @param array $sourceElements
47
     * @return array
48
     */
49
    private static function eagerLoadingUsersMap(array $sourceElements)
50
    {
51
        // Get the source element IDs
52
        $sourceElementIds = ArrayHelper::getColumn($sourceElements, 'id');
53
54
        $map = (new Query())
55
            ->select(['organizationId as source', 'userId as target'])
56
            ->from(UserAssociation::tableName())
57
            ->where(['organizationId' => $sourceElementIds])
58
            ->all();
59
60
        return [
61
            'elementType' => User::class,
62
            'map' => $map
63
        ];
64
    }
65
66
    /************************************************************
67
     * REQUEST
68
     ************************************************************/
69
70
    /**
71
     * AssociateUserToOrganization an array of users from request input
72
     *
73
     * @param string $identifier
74
     * @return $this
75
     */
76
    public function setUsersFromRequest(string $identifier = 'users')
77
    {
78
        if (null !== ($users = Craft::$app->getRequest()->getBodyParam($identifier))) {
79
            $this->getUserManager()->setMany((array)$users);
80
        }
81
82
        return $this;
83
    }
84
85
86
    /************************************************************
87
     * USERS QUERY
88
     ************************************************************/
89
90
    /**
91
     * @param array $criteria
92
     * @return UserQuery
93
     */
94
    public function userQuery($criteria = []): UserQuery
95
    {
96
        /** @noinspection PhpUndefinedMethodInspection */
97
        $query = User::find()
98
            ->organization($this)
99
            ->orderBy([
100
                'userOrder' => SORT_ASC,
101
                'username' => SORT_ASC,
102
            ]);
103
104
        if (!empty($criteria)) {
105
            QueryHelper::configure(
106
                $query,
107
                $criteria
108
            );
109
        }
110
111
        return $query;
112
    }
113
114
    /**
115
     * Get an array of users associated to an organization
116
     *
117
     * @return User[]
118
     */
119
    public function getUsers(): array
120
    {
121
        return ArrayHelper::getColumn(
122
            $this->getUserManager()->findAll(),
123
            'user'
124
        );
125
    }
126
127
    /**
128
     * AssociateUserToOrganization users to an organization
129
     *
130
     * @param $users
131
     * @return $this
132
     *
133
     * @deprecated
134
     */
135
    public function setUsers($users)
136
    {
137
        $this->getUserManager()->setMany($users);
138
        return $this;
139
    }
140
141
    /**
142
     * AssociateUserToOrganization an array of users to an organization
143
     *
144
     * @param $users
145
     * @return $this
146
     *
147
     * @deprecated
148
     */
149
    public function addUsers(array $users)
150
    {
151
        $this->getUserManager()->addMany($users);
152
        return $this;
153
    }
154
155
    /**
156
     * AssociateUserToOrganization a user to an organization
157
     *
158
     * @param User $user
159
     * @return $this
160
     *
161
     * @deprecated
162
     */
163
    public function addUser(User $user)
164
    {
165
        $this->getUserManager()->addOne($user);
166
        return $this;
167
    }
168
169
    /**
170
     * DissociateUserFromOrganization a user from an organization
171
     *
172
     * @param array $users
173
     * @return $this
174
     *
175
     * @deprecated
176
     */
177
    public function removeUsers(array $users)
178
    {
179
        $this->getUserManager()->removeMany($users);
180
        return $this;
181
    }
182
183
    /**
184
     * DissociateUserFromOrganization a user from an organization
185
     *
186
     * @param User $user
187
     * @return $this
188
     *
189
     * @deprecated
190
     */
191
    public function removeUser(User $user)
192
    {
193
        $this->getUserManager()->removeOne($user);
194
        return $this;
195
    }
196
197
    /**
198
     * @return $this
199
     *
200
     * @deprecated
201
     */
202
    public function resetUsers()
203
    {
204
        $this->getUserManager()->reset();
205
        return $this;
206
    }
207
208
209
    /*******************************************
210
     * ASSOCIATE and/or DISASSOCIATE
211
     *******************************************/
212
213
    /**
214
     * @return bool
215
     * @throws \Throwable
216
     * @throws \yii\db\StaleObjectException
217
     *
218
     * @deprecated
219
     */
220
    public function saveUsers()
221
    {
222
        return $this->getUserManager()->save();
223
    }
224
225
    /**
226
     * @param User $user
227
     * @param int|null $sortOrder
228
     * @return bool
229
     *
230
     * @deprecated
231
     */
232
    public function associateUser(User $user, int $sortOrder = null): bool
233
    {
234
        return $this->getUserManager()->associateOne($user, $sortOrder);
235
    }
236
237
    /**
238
     * @param UserQuery $query
239
     * @return bool
240
     * @throws \Throwable
241
     *
242
     * @deprecated
243
     */
244
    public function associateUsers(UserQuery $query)
245
    {
246
        return $this->getUserManager()->associateMany($query);
247
    }
248
249
    /**
250
     * @param User $user
251
     * @return bool
252
     * @throws \Throwable
253
     * @throws \yii\db\StaleObjectException
254
     *
255
     * @deprecated
256
     */
257
    public function dissociateUser(User $user): bool
258
    {
259
        return $this->getUserManager()->dissociateOne($user);
260
    }
261
262
    /**
263
     * @param UserQuery $query
264
     * @return bool
265
     * @throws \Throwable
266
     *
267
     * @deprecated
268
     */
269
    public function dissociateUsers(UserQuery $query)
270
    {
271
        return $this->getUserManager()->dissociateMany($query);
272
    }
273
}
274