|
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\managers; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\elements\User; |
|
13
|
|
|
use craft\helpers\ArrayHelper; |
|
14
|
|
|
use craft\helpers\Json; |
|
15
|
|
|
use flipbox\craft\ember\helpers\QueryHelper; |
|
16
|
|
|
use flipbox\organizations\behaviors\OrganizationsAssociatedToUserBehavior; |
|
17
|
|
|
use flipbox\organizations\elements\Organization; |
|
18
|
|
|
use flipbox\organizations\Organizations; |
|
19
|
|
|
use flipbox\organizations\queries\UserAssociationQuery; |
|
20
|
|
|
use flipbox\organizations\records\UserAssociation; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Manages Users associated to Organizations |
|
24
|
|
|
* |
|
25
|
|
|
* @author Flipbox Factory <[email protected]> |
|
26
|
|
|
* @since 2.0.0 |
|
27
|
|
|
* |
|
28
|
|
|
* @property UserAssociation[] $associations |
|
29
|
|
|
* |
|
30
|
|
|
* @method UserAssociation findOrCreate($object) |
|
31
|
|
|
* @method UserAssociation findOne($object = null) |
|
32
|
|
|
* @method UserAssociation findOrFail($object) |
|
33
|
|
|
*/ |
|
34
|
|
|
class UserRelationshipManager implements RelationshipManagerInterface |
|
35
|
|
|
{ |
|
36
|
|
|
use RelationshipManagerTrait; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Organization |
|
40
|
|
|
*/ |
|
41
|
|
|
private $organization; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Organization $organization |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(Organization $organization) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->organization = $organization; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param array $criteria |
|
53
|
|
|
* @return UserAssociationQuery |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function query(array $criteria = []): UserAssociationQuery |
|
56
|
|
|
{ |
|
57
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
58
|
|
|
$query = UserAssociation::find() |
|
59
|
|
|
->setOrganizationId($this->organization->getId() ?: false) |
|
|
|
|
|
|
60
|
|
|
->orderBy([ |
|
61
|
|
|
'userOrder' => SORT_ASC |
|
62
|
|
|
]); |
|
63
|
|
|
|
|
64
|
|
|
if (!empty($criteria)) { |
|
65
|
|
|
QueryHelper::configure( |
|
66
|
|
|
$query, |
|
67
|
|
|
$criteria |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $query; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $object |
|
76
|
|
|
* @return UserAssociation |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function create($object): UserAssociation |
|
79
|
|
|
{ |
|
80
|
|
|
return (new UserAssociation()) |
|
81
|
|
|
->setOrganization($this->organization) |
|
82
|
|
|
->setUser($this->resolveUser($object)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritDoc |
|
87
|
|
|
* |
|
88
|
|
|
* @param bool $addToUser |
|
89
|
|
|
*/ |
|
90
|
|
|
public function addOne($object, array $attributes = [], bool $addToUser = false) |
|
91
|
|
|
{ |
|
92
|
|
|
if (null === ($association = $this->findOne($object))) { |
|
93
|
|
|
$association = $this->create($object); |
|
94
|
|
|
$this->addToCache($association); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (!empty($attributes)) { |
|
98
|
|
|
Craft::configure( |
|
99
|
|
|
$association, |
|
100
|
|
|
$attributes |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Add user to user as well? |
|
105
|
|
|
if ($addToUser && null !== ($use = $association->getUser())) { |
|
106
|
|
|
/** @var OrganizationsAssociatedToUserBehavior $use */ |
|
107
|
|
|
$use->getOrganizationManager()->addOne($this->organization, [], false); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/******************************************* |
|
115
|
|
|
* SAVE |
|
116
|
|
|
*******************************************/ |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @inheritDoc |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function associationDelta(): array |
|
122
|
|
|
{ |
|
123
|
|
|
$existingAssociations = $this->query() |
|
124
|
|
|
->indexBy('userId') |
|
125
|
|
|
->all(); |
|
126
|
|
|
|
|
127
|
|
|
$associations = []; |
|
128
|
|
|
$order = 1; |
|
129
|
|
|
foreach ($this->findAll() as $newAssociation) { |
|
130
|
|
|
if (null === ($association = ArrayHelper::remove( |
|
131
|
|
|
$existingAssociations, |
|
132
|
|
|
$newAssociation->getUserId() |
|
133
|
|
|
))) { |
|
134
|
|
|
$association = $newAssociation; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$association->userOrder = $order++; |
|
138
|
|
|
$association->organizationOrder = $newAssociation->organizationOrder; |
|
139
|
|
|
|
|
140
|
|
|
$associations[] = $association; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return [$associations, $existingAssociations]; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @inheritDoc |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function handleAssociationError() |
|
150
|
|
|
{ |
|
151
|
|
|
$this->organization->addError('users', 'Unable to save user organizations.'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
/******************************************* |
|
156
|
|
|
* UTILS |
|
157
|
|
|
*******************************************/ |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param UserAssociation|User|int|array|null $object |
|
161
|
|
|
* @return int|null |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function findKey($object = null) |
|
164
|
|
|
{ |
|
165
|
|
|
if (null === ($element = $this->resolveUser($object))) { |
|
166
|
|
|
Organizations::info(sprintf( |
|
167
|
|
|
"Unable to resolve user: %s", |
|
168
|
|
|
(string)Json::encode($object) |
|
169
|
|
|
)); |
|
170
|
|
|
return null; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
foreach ($this->findAll() as $key => $association) { |
|
174
|
|
|
if (null !== $association->getUser() && $association->getUser()->email == $element->email) { |
|
175
|
|
|
return $key; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return null; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param UserAssociation|User|int|array|null $user |
|
184
|
|
|
* @return User|null |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function resolveUser($user = null) |
|
187
|
|
|
{ |
|
188
|
|
|
if (null === $user) { |
|
189
|
|
|
return null; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
if ($user instanceof UserAssociation) { |
|
193
|
|
|
return $user->getUser(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if ($user instanceof User) { |
|
197
|
|
|
return $user; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if (is_array($user) && |
|
201
|
|
|
null !== ($id = ArrayHelper::getValue($user, 'id')) |
|
202
|
|
|
) { |
|
203
|
|
|
$user = ['id' => $id]; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return User::findOne($user); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|