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