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\relationships; |
10
|
|
|
|
11
|
|
|
use craft\elements\db\UserQuery; |
12
|
|
|
use craft\elements\User; |
13
|
|
|
use craft\helpers\ArrayHelper; |
14
|
|
|
use flipbox\organizations\elements\Organization; |
15
|
|
|
use flipbox\organizations\Organizations; |
16
|
|
|
use flipbox\organizations\queries\UserAssociationQuery; |
17
|
|
|
use flipbox\organizations\records\UserAssociation; |
18
|
|
|
use Tightenco\Collect\Support\Collection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Manages Users associated to Organizations |
22
|
|
|
* |
23
|
|
|
* @author Flipbox Factory <[email protected]> |
24
|
|
|
* @since 2.0.0 |
25
|
|
|
* |
26
|
|
|
* @method UserAssociation findOrCreate($object) |
27
|
|
|
* @method UserAssociation findOne($object = null) |
28
|
|
|
* @method UserAssociation findOrFail($object) |
29
|
|
|
*/ |
30
|
|
|
class UserRelationship implements RelationshipInterface |
31
|
|
|
{ |
32
|
|
|
use RelationshipTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Organization |
36
|
|
|
*/ |
37
|
|
|
private $organization; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Organization $organization |
41
|
|
|
*/ |
42
|
|
|
public function __construct(Organization $organization) |
43
|
|
|
{ |
44
|
|
|
$this->organization = $organization; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/************************************************************ |
49
|
|
|
* COLLECTION |
50
|
|
|
************************************************************/ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a collection of associated users |
54
|
|
|
* |
55
|
|
|
* @return User[]|Collection |
56
|
|
|
*/ |
57
|
|
|
public function getCollection(): Collection |
58
|
|
|
{ |
59
|
|
|
if (null === $this->relations) { |
60
|
|
|
return new Collection( |
61
|
|
|
$this->elementQuery() |
62
|
|
|
->all() |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->createCollectionFromRelations(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return Collection |
71
|
|
|
*/ |
72
|
|
|
protected function createCollectionFromRelations() |
73
|
|
|
{ |
74
|
|
|
$ids = $this->getRelationships()->pluck('userId')->all(); |
75
|
|
|
if (empty($ids)) { |
76
|
|
|
return $this->getRelationships()->pluck('user'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// 'eager' load where we'll pre-populate all of the elements |
80
|
|
|
$elements = $this->elementQuery() |
81
|
|
|
->id($ids) |
82
|
|
|
->indexBy('id') |
83
|
|
|
->all(); |
84
|
|
|
|
85
|
|
|
return $this->getRelationships() |
86
|
|
|
->transform(function (UserAssociation $association) use ($elements) { |
87
|
|
|
if (!$association->isUserSet() && isset($elements[$association->getUserId()])) { |
|
|
|
|
88
|
|
|
$association->setUser($elements[$association->getUserId()]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$association->setOrganization($this->organization); |
92
|
|
|
|
93
|
|
|
return $association; |
94
|
|
|
}) |
95
|
|
|
->pluck('user'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return Collection |
100
|
|
|
*/ |
101
|
|
|
protected function existingRelationships(): Collection |
102
|
|
|
{ |
103
|
|
|
$relationships = $this->associationQuery() |
104
|
|
|
->with('types') |
105
|
|
|
->all(); |
106
|
|
|
|
107
|
|
|
return $this->createRelations($relationships); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/************************************************************ |
111
|
|
|
* QUERY |
112
|
|
|
************************************************************/ |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return UserQuery |
116
|
|
|
*/ |
117
|
|
|
private function elementQuery(): UserQuery |
118
|
|
|
{ |
119
|
|
|
return User::find() |
120
|
|
|
->organizationId($this->organization->getId() ?: false) |
121
|
|
|
->anyStatus() |
122
|
|
|
->limit(null); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return UserAssociationQuery |
127
|
|
|
*/ |
128
|
|
|
private function associationQuery(): UserAssociationQuery |
129
|
|
|
{ |
130
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
131
|
|
|
return UserAssociation::find() |
132
|
|
|
->setOrganizationId($this->organization->getId() ?: false) |
|
|
|
|
133
|
|
|
->orderBy([ |
134
|
|
|
'organizationOrder' => SORT_ASC |
135
|
|
|
]) |
136
|
|
|
->limit(null); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/************************************************************ |
140
|
|
|
* CREATE |
141
|
|
|
************************************************************/ |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $object |
145
|
|
|
* @return UserAssociation |
146
|
|
|
*/ |
147
|
|
|
protected function create($object): UserAssociation |
148
|
|
|
{ |
149
|
|
|
if ($object instanceof UserAssociation) { |
150
|
|
|
return $object; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return (new UserAssociation()) |
154
|
|
|
->setOrganization($this->organization) |
155
|
|
|
->setUser($this->resolveObject($object)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/******************************************* |
160
|
|
|
* DELTA |
161
|
|
|
*******************************************/ |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @inheritDoc |
165
|
|
|
*/ |
166
|
|
|
protected function delta(): array |
167
|
|
|
{ |
168
|
|
|
$existingAssociations = $this->associationQuery() |
169
|
|
|
->indexBy('userId') |
170
|
|
|
->all(); |
171
|
|
|
|
172
|
|
|
$associations = []; |
173
|
|
|
$order = 1; |
174
|
|
|
|
175
|
|
|
/** @var UserAssociation $newAssociation */ |
176
|
|
|
foreach ($this->getRelationships() as $newAssociation) { |
177
|
|
|
$association = ArrayHelper::remove( |
178
|
|
|
$existingAssociations, |
179
|
|
|
$newAssociation->getUserId() |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
$newAssociation->userOrder = $order++; |
183
|
|
|
|
184
|
|
|
/** @var UserAssociation $association */ |
185
|
|
|
$association = $association ?: $newAssociation; |
186
|
|
|
|
187
|
|
|
// Has anything changed? |
188
|
|
|
if (!$association->getIsNewRecord() && !$this->hasChanged($newAssociation, $association)) { |
189
|
|
|
continue; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$associations[] = $this->sync($association, $newAssociation); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return [$associations, $existingAssociations]; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param UserAssociation $new |
200
|
|
|
* @param UserAssociation $existing |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
private function hasChanged(UserAssociation $new, UserAssociation $existing): bool |
204
|
|
|
{ |
205
|
|
|
return (Organizations::getInstance()->getSettings()->getEnforceUserSortOrder() && |
206
|
|
|
$new->userOrder != $existing->userOrder |
207
|
|
|
) || |
208
|
|
|
$new->state != $existing->state || |
209
|
|
|
$new->getTypes()->isMutated(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param UserAssociation $from |
214
|
|
|
* @param UserAssociation $to |
215
|
|
|
* |
216
|
|
|
* @return UserAssociation |
217
|
|
|
*/ |
218
|
|
|
private function sync(UserAssociation $to, UserAssociation $from): UserAssociation |
219
|
|
|
{ |
220
|
|
|
$to->userOrder = $from->userOrder; |
221
|
|
|
$to->state = $from->state; |
222
|
|
|
|
223
|
|
|
if ($from->getTypes()->isMutated()) { |
224
|
|
|
$to->getTypes()->clear()->add( |
225
|
|
|
$from->getTypes()->getCollection() |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$to->ignoreSortOrder(); |
230
|
|
|
|
231
|
|
|
return $to; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
/******************************************* |
236
|
|
|
* COLLECTION UTILS |
237
|
|
|
*******************************************/ |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @inheritDoc |
241
|
|
|
*/ |
242
|
|
|
protected function insertCollection(Collection $collection, UserAssociation $association) |
243
|
|
|
{ |
244
|
|
|
if (Organizations::getInstance()->getSettings()->getEnforceUserSortOrder() && $association->userOrder > 0) { |
245
|
|
|
$collection->splice($association->userOrder - 1, 0, [$association]); |
246
|
|
|
return; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$collection->push($association); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @inheritDoc |
254
|
|
|
*/ |
255
|
|
|
protected function updateCollection(Collection $collection, UserAssociation $association) |
256
|
|
|
{ |
257
|
|
|
if (!Organizations::getInstance()->getSettings()->getEnforceUserSortOrder()) { |
258
|
|
|
return; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
if (null !== ($key = $this->findKey($association))) { |
262
|
|
|
$collection->offsetUnset($key); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$this->insertCollection($collection, $association); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
/******************************************* |
270
|
|
|
* UTILS |
271
|
|
|
*******************************************/ |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param UserAssociation|User|int|array|null $object |
275
|
|
|
* @return int|null |
276
|
|
|
*/ |
277
|
|
|
protected function findKey($object = null) |
278
|
|
|
{ |
279
|
|
|
if ($object instanceof UserAssociation) { |
280
|
|
|
if (!$object->getUser()) { |
281
|
|
|
var_dump("NOT FOUND"); |
|
|
|
|
282
|
|
|
return null; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $this->findRelationshipKey($object->getUser()->email); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if (null === ($element = $this->resolveObject($object))) { |
289
|
|
|
return null; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return $this->findRelationshipKey($element->email); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param $identifier |
297
|
|
|
* @return int|string|null |
298
|
|
|
*/ |
299
|
|
|
private function findRelationshipKey($identifier) |
300
|
|
|
{ |
301
|
|
|
/** @var UserAssociation $association */ |
302
|
|
|
foreach ($this->getRelationships()->all() as $key => $association) { |
303
|
|
|
if (null !== $association->getUser() && $association->getUser()->email == $identifier) { |
304
|
|
|
return $key; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return null; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param UserAssociation|User|int|array|null $user |
313
|
|
|
* @return User|null |
314
|
|
|
*/ |
315
|
|
|
protected function resolveObjectInternal($user) |
316
|
|
|
{ |
317
|
|
|
if ($user instanceof UserAssociation) { |
318
|
|
|
return $user->getUser(); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
if ($user instanceof User) { |
322
|
|
|
return $user; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if (is_numeric($user)) { |
326
|
|
|
return \Craft::$app->getUsers()->getUserById($user); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
if (is_string($user)) { |
330
|
|
|
return \Craft::$app->getUsers()->getUserByUsernameOrEmail($user); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return User::findOne($user); |
|
|
|
|
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: