|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace flipbox\organizations\behaviors; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\elements\User; |
|
7
|
|
|
use craft\events\ModelEvent; |
|
8
|
|
|
use flipbox\craft\ember\helpers\QueryHelper; |
|
9
|
|
|
use flipbox\organizations\elements\Organization; |
|
10
|
|
|
use flipbox\organizations\relationships\RelationshipInterface; |
|
11
|
|
|
use flipbox\organizations\relationships\OrganizationRelationship; |
|
12
|
|
|
use flipbox\organizations\Organizations as OrganizationPlugin; |
|
13
|
|
|
use flipbox\organizations\queries\OrganizationQuery; |
|
14
|
|
|
use flipbox\organizations\validators\OrganizationsValidator; |
|
15
|
|
|
use Tightenco\Collect\Support\Collection; |
|
16
|
|
|
use yii\base\Behavior; |
|
17
|
|
|
use yii\base\Event; |
|
18
|
|
|
use yii\base\Exception; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class UserOrganizationsBehavior |
|
22
|
|
|
* @package flipbox\organizations\behaviors |
|
23
|
|
|
* |
|
24
|
|
|
* @property User $owner; |
|
25
|
|
|
*/ |
|
26
|
|
|
class OrganizationsAssociatedToUserBehavior extends Behavior |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var RelationshipInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $manager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Whether associated organizations should be saved |
|
35
|
|
|
* |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
private $saveOrganizations = true; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return static |
|
42
|
|
|
*/ |
|
43
|
|
|
public function withOrganizations(): self |
|
44
|
|
|
{ |
|
45
|
|
|
$this->saveOrganizations = true; |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return static |
|
51
|
|
|
*/ |
|
52
|
|
|
public function withoutUsers(): self |
|
53
|
|
|
{ |
|
54
|
|
|
$this->saveOrganizations = false; |
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
|
|
public function init() |
|
62
|
|
|
{ |
|
63
|
|
|
parent::init(); |
|
64
|
|
|
|
|
65
|
|
|
// Validate organizations |
|
66
|
|
|
Event::on( |
|
67
|
|
|
User::class, |
|
68
|
|
|
User::EVENT_AFTER_VALIDATE, |
|
69
|
|
|
function (Event $e) { |
|
70
|
|
|
/** @var User $user */ |
|
71
|
|
|
$user = $e->sender; |
|
72
|
|
|
$this->onAfterValidate($user); |
|
73
|
|
|
} |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
// Associate |
|
77
|
|
|
Event::on( |
|
78
|
|
|
User::class, |
|
79
|
|
|
User::EVENT_AFTER_SAVE, |
|
80
|
|
|
function (ModelEvent $e) { |
|
81
|
|
|
/** @var User $user */ |
|
82
|
|
|
$user = $e->sender; |
|
83
|
|
|
$this->onAfterSave($user); |
|
84
|
|
|
} |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
// Dissociate |
|
88
|
|
|
Event::on( |
|
89
|
|
|
User::class, |
|
90
|
|
|
User::EVENT_AFTER_DELETE, |
|
91
|
|
|
function (Event $e) { |
|
92
|
|
|
/** @var User $user */ |
|
93
|
|
|
$user = $e->sender; |
|
94
|
|
|
$this->onAfterDelete($user); |
|
95
|
|
|
} |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param User $user |
|
101
|
|
|
* @return void |
|
102
|
|
|
* @throws \Throwable |
|
103
|
|
|
*/ |
|
104
|
|
|
private function onAfterDelete(User $user) |
|
105
|
|
|
{ |
|
106
|
|
|
/** @var static $user */ |
|
107
|
|
|
$user->getOrganizations() |
|
108
|
|
|
->clear() |
|
109
|
|
|
->save(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param User|self $user |
|
114
|
|
|
* @throws Exception |
|
115
|
|
|
* @throws \Exception |
|
116
|
|
|
* @throws \Throwable |
|
117
|
|
|
* @throws \craft\errors\ElementNotFoundException |
|
118
|
|
|
*/ |
|
119
|
|
|
private function onAfterSave(User $user) |
|
120
|
|
|
{ |
|
121
|
|
|
// Check cache for explicitly set (and possibly not saved) organizations |
|
122
|
|
|
if (true === $this->saveOrganizations && $user->getOrganizations()->isMutated()) { |
|
123
|
|
|
|
|
124
|
|
|
/** @var Organization $organization */ |
|
125
|
|
|
foreach ($user->getOrganizations()->getCollection() as $organization) { |
|
126
|
|
|
if (null === $organization->getId()) { |
|
127
|
|
|
if (!Craft::$app->getElements()->saveElement($organization)) { |
|
128
|
|
|
$user->addError( |
|
129
|
|
|
'organizations', |
|
130
|
|
|
OrganizationPlugin::t('Unable to save organization.') |
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
|
|
throw new Exception('Unable to save organization.'); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$user->getOrganizations()->save(); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param User|self $user |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
|
|
private function onAfterValidate(User $user) |
|
147
|
|
|
{ |
|
148
|
|
|
(new OrganizationsValidator())->validateAttribute($user, 'organizations'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get an array of associated organizations |
|
153
|
|
|
* |
|
154
|
|
|
* @return OrganizationRelationship|RelationshipInterface |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getOrganizations(): RelationshipInterface |
|
157
|
|
|
{ |
|
158
|
|
|
if (null === $this->manager) { |
|
159
|
|
|
$this->manager = new OrganizationRelationship($this->owner); |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $this->manager; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Set an array or query of organizations to a user |
|
167
|
|
|
* |
|
168
|
|
|
* @param $organizations |
|
169
|
|
|
* @return $this |
|
170
|
|
|
*/ |
|
171
|
|
|
public function setOrganizations($organizations) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->getOrganizations()->clear()->add($organizations); |
|
174
|
|
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: