1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organizations\events\handlers; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\events\ConfigEvent; |
13
|
|
|
use flipbox\organizations\events\ManageOrganizationTypeProjectConfig; |
14
|
|
|
use flipbox\organizations\events\ManageUserTypeProjectConfig; |
15
|
|
|
use flipbox\organizations\records\OrganizationType; |
16
|
|
|
use flipbox\organizations\records\UserType; |
17
|
|
|
use yii\base\Event; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 3.0.0 |
22
|
|
|
*/ |
23
|
|
|
class ProjectConfigHandler |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param ConfigEvent $event |
27
|
|
|
*/ |
28
|
|
|
public static function handleChangedOrganizationType(ConfigEvent $event) |
29
|
|
|
{ |
30
|
|
|
Event::off( |
31
|
|
|
OrganizationType::class, |
32
|
|
|
OrganizationType::EVENT_AFTER_INSERT, |
33
|
|
|
[ |
34
|
|
|
ManageOrganizationTypeProjectConfig::class, |
35
|
|
|
'save' |
36
|
|
|
] |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
Event::off( |
40
|
|
|
OrganizationType::class, |
41
|
|
|
OrganizationType::EVENT_AFTER_UPDATE, |
42
|
|
|
[ |
43
|
|
|
ManageOrganizationTypeProjectConfig::class, |
44
|
|
|
'save' |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
// Get the UID that was matched in the config path |
49
|
|
|
$uid = $event->tokenMatches[0]; |
50
|
|
|
|
51
|
|
|
if (null === ($provider = OrganizationType::findOne([ |
52
|
|
|
'uid' => $uid |
53
|
|
|
]))) { |
54
|
|
|
$provider = new OrganizationType(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
Craft::configure($provider, $event->newValue); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$provider->save(); |
60
|
|
|
|
61
|
|
|
Event::on( |
62
|
|
|
OrganizationType::class, |
63
|
|
|
OrganizationType::EVENT_AFTER_INSERT, |
64
|
|
|
[ |
65
|
|
|
ManageOrganizationTypeProjectConfig::class, |
66
|
|
|
'save' |
67
|
|
|
] |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
Event::on( |
71
|
|
|
OrganizationType::class, |
72
|
|
|
OrganizationType::EVENT_AFTER_UPDATE, |
73
|
|
|
[ |
74
|
|
|
ManageOrganizationTypeProjectConfig::class, |
75
|
|
|
'save' |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ConfigEvent $event |
82
|
|
|
* @throws \Throwable |
83
|
|
|
* @throws \yii\db\StaleObjectException |
84
|
|
|
*/ |
85
|
|
|
public static function handleDeletedOrganizationType(ConfigEvent $event) |
86
|
|
|
{ |
87
|
|
|
Event::off( |
88
|
|
|
OrganizationType::class, |
89
|
|
|
OrganizationType::EVENT_AFTER_DELETE, |
90
|
|
|
[ |
91
|
|
|
ManageOrganizationTypeProjectConfig::class, |
92
|
|
|
'delete' |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
// Get the UID that was matched in the config path |
97
|
|
|
$uid = $event->tokenMatches[0]; |
98
|
|
|
|
99
|
|
|
if (null === $provider = OrganizationType::findOne([ |
100
|
|
|
'uid' => $uid |
101
|
|
|
])) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$provider->delete(); |
106
|
|
|
|
107
|
|
|
Event::on( |
108
|
|
|
OrganizationType::class, |
109
|
|
|
OrganizationType::EVENT_AFTER_DELETE, |
110
|
|
|
[ |
111
|
|
|
ManageOrganizationTypeProjectConfig::class, |
112
|
|
|
'delete' |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param ConfigEvent $event |
119
|
|
|
*/ |
120
|
|
|
public static function handleChangedUserType(ConfigEvent $event) |
121
|
|
|
{ |
122
|
|
|
Event::off( |
123
|
|
|
UserType::class, |
124
|
|
|
UserType::EVENT_AFTER_INSERT, |
125
|
|
|
[ |
126
|
|
|
ManageUserTypeProjectConfig::class, |
127
|
|
|
'save' |
128
|
|
|
] |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
Event::off( |
132
|
|
|
UserType::class, |
133
|
|
|
UserType::EVENT_AFTER_UPDATE, |
134
|
|
|
[ |
135
|
|
|
ManageUserTypeProjectConfig::class, |
136
|
|
|
'save' |
137
|
|
|
] |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
// Get the UID that was matched in the config path |
141
|
|
|
$uid = $event->tokenMatches[0]; |
142
|
|
|
|
143
|
|
|
if (null === ($token = UserType::findOne([ |
144
|
|
|
'uid' => $uid |
145
|
|
|
]))) { |
146
|
|
|
$token = new UserType(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
Craft::configure($token, $event->newValue); |
|
|
|
|
150
|
|
|
|
151
|
|
|
$token->save(); |
152
|
|
|
|
153
|
|
|
Event::on( |
154
|
|
|
UserType::class, |
155
|
|
|
UserType::EVENT_AFTER_INSERT, |
156
|
|
|
[ |
157
|
|
|
ManageUserTypeProjectConfig::class, |
158
|
|
|
'save' |
159
|
|
|
] |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
Event::on( |
163
|
|
|
UserType::class, |
164
|
|
|
UserType::EVENT_AFTER_UPDATE, |
165
|
|
|
[ |
166
|
|
|
ManageUserTypeProjectConfig::class, |
167
|
|
|
'save' |
168
|
|
|
] |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param ConfigEvent $event |
174
|
|
|
* @throws \Throwable |
175
|
|
|
* @throws \yii\db\StaleObjectException |
176
|
|
|
*/ |
177
|
|
|
public static function handleDeletedUserType(ConfigEvent $event) |
178
|
|
|
{ |
179
|
|
|
Event::off( |
180
|
|
|
UserType::class, |
181
|
|
|
UserType::EVENT_AFTER_DELETE, |
182
|
|
|
[ |
183
|
|
|
ManageUserTypeProjectConfig::class, |
184
|
|
|
'delete' |
185
|
|
|
] |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
// Get the UID that was matched in the config path |
189
|
|
|
$uid = $event->tokenMatches[0]; |
190
|
|
|
|
191
|
|
|
if (null === $token = UserType::findOne([ |
192
|
|
|
'uid' => $uid |
193
|
|
|
])) { |
194
|
|
|
return; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$token->delete(); |
198
|
|
|
|
199
|
|
|
Event::on( |
200
|
|
|
UserType::class, |
201
|
|
|
UserType::EVENT_AFTER_DELETE, |
202
|
|
|
[ |
203
|
|
|
ManageUserTypeProjectConfig::class, |
204
|
|
|
'delete' |
205
|
|
|
] |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
|
|
public static function rebuild(): array |
213
|
|
|
{ |
214
|
|
|
$return = []; |
215
|
|
|
|
216
|
|
|
foreach (OrganizationType::findAll([]) as $record) { |
217
|
|
|
$return['plugins']['organizations']['organizationTypes'][$record->uid] = $record->toProjectConfig(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
foreach (UserType::findAll([]) as $token) { |
221
|
|
|
$return['plugins']['organizations']['userTypes'][$record->uid] = $record->toProjectConfig(); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $return; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.