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\cp\controllers; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\elements\User; |
13
|
|
|
use craft\helpers\ArrayHelper; |
14
|
|
|
use flipbox\organizations\actions\users\AssociateUserToOrganization; |
15
|
|
|
use flipbox\organizations\actions\users\DissociateUserFromOrganization; |
16
|
|
|
use flipbox\organizations\behaviors\UserTypesAssociatedToUserBehavior; |
17
|
|
|
use flipbox\organizations\events\handlers\RegisterOrganizationUserElementDefaultTableAttributes; |
18
|
|
|
use flipbox\organizations\events\handlers\RegisterOrganizationUserElementTableAttributes; |
19
|
|
|
use flipbox\organizations\events\handlers\SetOrganizationUserElementTableAttributeHtml; |
20
|
|
|
use flipbox\organizations\records\UserAssociation; |
21
|
|
|
use flipbox\organizations\records\UserType; |
22
|
|
|
use yii\base\Event; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Flipbox Factory <[email protected]> |
26
|
|
|
* @since 1.0.0 |
27
|
|
|
*/ |
28
|
|
|
class UsersController extends AbstractController |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function behaviors() |
34
|
|
|
{ |
35
|
|
|
return ArrayHelper::merge( |
36
|
|
|
parent::behaviors(), |
37
|
|
|
[ |
38
|
|
|
'error' => [ |
39
|
|
|
'default' => 'organization' |
40
|
|
|
], |
41
|
|
|
'redirect' => [ |
42
|
|
|
'only' => ['associate', 'dissociate'], |
43
|
|
|
'actions' => [ |
44
|
|
|
'associate' => [204], |
45
|
|
|
'dissociate' => [204], |
46
|
|
|
] |
47
|
|
|
], |
48
|
|
|
'flash' => [ |
49
|
|
|
'actions' => [ |
50
|
|
|
'associate' => [ |
51
|
|
|
204 => Craft::t('organizations', "Successfully associated user."), |
52
|
|
|
401 => Craft::t('organizations', "Failed to associate user.") |
53
|
|
|
], |
54
|
|
|
'dissociate' => [ |
55
|
|
|
204 => Craft::t('organizations', "Successfully dissociated user."), |
56
|
|
|
401 => Craft::t('organizations', "Failed to dissociate user.") |
57
|
|
|
] |
58
|
|
|
] |
59
|
|
|
] |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
protected function verbs(): array |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
'associate' => ['post'] |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param int|string|null $user |
76
|
|
|
* @param int|string|null $organization |
77
|
|
|
* @return mixed |
78
|
|
|
* @throws \yii\base\InvalidConfigException |
79
|
|
|
*/ |
80
|
|
|
public function actionAssociate($user = null, $organization = null) |
81
|
|
|
{ |
82
|
|
|
if (null === $organization) { |
83
|
|
|
$organization = Craft::$app->getRequest()->getBodyParam('organization'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (null === $user) { |
87
|
|
|
$user = Craft::$app->getRequest()->getBodyParam('user'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** @var AssociateUserToOrganization $action */ |
91
|
|
|
$action = Craft::createObject([ |
92
|
|
|
'class' => AssociateUserToOrganization::class |
93
|
|
|
], [ |
94
|
|
|
'associate', |
95
|
|
|
$this |
96
|
|
|
]); |
97
|
|
|
|
98
|
|
|
return $action->runWithParams([ |
99
|
|
|
'organization' => $organization, |
100
|
|
|
'user' => $user |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param int|string|null $user |
106
|
|
|
* @param int|string|null $organization |
107
|
|
|
* @return mixed |
108
|
|
|
* @throws \yii\base\InvalidConfigException |
109
|
|
|
*/ |
110
|
|
|
public function actionDissociate($user = null, $organization = null) |
111
|
|
|
{ |
112
|
|
|
if (null === $organization) { |
113
|
|
|
$organization = Craft::$app->getRequest()->getBodyParam('organization'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (null === $user) { |
117
|
|
|
$user = Craft::$app->getRequest()->getBodyParam('user'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** @var DissociateUserFromOrganization $action */ |
121
|
|
|
$action = Craft::createObject([ |
122
|
|
|
'class' => DissociateUserFromOrganization::class |
123
|
|
|
], [ |
124
|
|
|
'dissociate', |
125
|
|
|
$this |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
return $action->runWithParams([ |
129
|
|
|
'organization' => $organization, |
130
|
|
|
'user' => $user |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param null $user |
136
|
|
|
* @param null $organization |
137
|
|
|
* @return \yii\web\Response |
138
|
|
|
* @throws \yii\base\Exception |
139
|
|
|
*/ |
140
|
|
|
public function actionSaveAssociation($user = null, $organization = null) |
141
|
|
|
{ |
142
|
|
|
if (null === $organization) { |
143
|
|
|
$organization = Craft::$app->getRequest()->getBodyParam('organization'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (null === $user) { |
147
|
|
|
$user = Craft::$app->getRequest()->getBodyParam( |
148
|
|
|
'user', |
149
|
|
|
Craft::$app->getRequest()->getBodyParam('elementId') |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$userAssociation = UserAssociation::findOne([ |
154
|
|
|
'user' => $user, |
155
|
|
|
'organization' => $organization |
156
|
|
|
]); |
157
|
|
|
|
158
|
|
|
$success = true; |
159
|
|
|
|
160
|
|
|
$userAssociation->state = Craft::$app->getRequest()->getBodyParam('state', $userAssociation->state); |
161
|
|
|
|
162
|
|
|
$userAssociation->getTypes()->clear()->add( |
163
|
|
|
Craft::$app->getRequest()->getBodyParam('types') |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
if (!$userAssociation->save()) { |
167
|
|
|
$success = false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$user = $userAssociation->getUser(); |
171
|
|
|
|
172
|
|
|
$response = [ |
173
|
|
|
'success' => $success, |
174
|
|
|
'id' => $user->getId(), |
175
|
|
|
'newTitle' => (string)$user, |
176
|
|
|
'cpEditUrl' => $user->getCpEditUrl(), |
177
|
|
|
]; |
178
|
|
|
|
179
|
|
|
// Should we be including table attributes too? |
180
|
|
|
$sourceKey = Craft::$app->getRequest()->getBodyParam('includeTableAttributesForSource'); |
181
|
|
|
|
182
|
|
|
if ($sourceKey) { |
183
|
|
|
Event::on( |
184
|
|
|
User::class, |
185
|
|
|
User::EVENT_REGISTER_DEFAULT_TABLE_ATTRIBUTES, |
186
|
|
|
[ |
187
|
|
|
RegisterOrganizationUserElementDefaultTableAttributes::class, |
188
|
|
|
'handle' |
189
|
|
|
] |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
// Add attributes the user index |
193
|
|
|
Event::on( |
194
|
|
|
User::class, |
195
|
|
|
User::EVENT_REGISTER_TABLE_ATTRIBUTES, |
196
|
|
|
[ |
197
|
|
|
RegisterOrganizationUserElementTableAttributes::class, |
198
|
|
|
'handle' |
199
|
|
|
] |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
// Add 'organizations' on the user html element |
203
|
|
|
Event::on( |
204
|
|
|
User::class, |
205
|
|
|
User::EVENT_SET_TABLE_ATTRIBUTE_HTML, |
206
|
|
|
[ |
207
|
|
|
SetOrganizationUserElementTableAttributeHtml::class, |
208
|
|
|
'handle' |
209
|
|
|
] |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
$attributes = Craft::$app->getElementIndexes()->getTableAttributes(get_class($user), $sourceKey); |
213
|
|
|
|
214
|
|
|
// Drop the first one |
215
|
|
|
array_shift($attributes); |
216
|
|
|
|
217
|
|
|
foreach ($attributes as $attribute) { |
218
|
|
|
$response['tableAttributes'][$attribute[0]] = $user->getTableAttributeHtml($attribute[0]); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
Event::off( |
222
|
|
|
User::class, |
223
|
|
|
User::EVENT_REGISTER_DEFAULT_TABLE_ATTRIBUTES, |
224
|
|
|
[ |
225
|
|
|
RegisterOrganizationUserElementDefaultTableAttributes::class, |
226
|
|
|
'handle' |
227
|
|
|
] |
228
|
|
|
); |
229
|
|
|
|
230
|
|
|
// Add attributes the user index |
231
|
|
|
Event::off( |
232
|
|
|
User::class, |
233
|
|
|
User::EVENT_REGISTER_TABLE_ATTRIBUTES, |
234
|
|
|
[ |
235
|
|
|
RegisterOrganizationUserElementTableAttributes::class, |
236
|
|
|
'handle' |
237
|
|
|
] |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
// Add 'organizations' on the user html element |
241
|
|
|
Event::off( |
242
|
|
|
User::class, |
243
|
|
|
User::EVENT_SET_TABLE_ATTRIBUTE_HTML, |
244
|
|
|
[ |
245
|
|
|
SetOrganizationUserElementTableAttributeHtml::class, |
246
|
|
|
'handle' |
247
|
|
|
] |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $this->asJson($response); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param null $user |
256
|
|
|
* @param null $organization |
257
|
|
|
* @return \yii\web\Response |
258
|
|
|
* @throws \Twig\Error\LoaderError |
259
|
|
|
* @throws \Twig\Error\RuntimeError |
260
|
|
|
* @throws \Twig\Error\SyntaxError |
261
|
|
|
*/ |
262
|
|
|
public function actionAssociationEditorHtml($user = null, $organization = null) |
263
|
|
|
{ |
264
|
|
|
if (null === $organization) { |
265
|
|
|
$organization = Craft::$app->getRequest()->getBodyParam('organization'); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
if (null === $user) { |
269
|
|
|
$user = Craft::$app->getRequest()->getBodyParam( |
270
|
|
|
'user', |
271
|
|
|
Craft::$app->getRequest()->getBodyParam('elementId') |
272
|
|
|
); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$userAssociation = UserAssociation::findOne([ |
276
|
|
|
'user' => $user, |
277
|
|
|
'organization' => $organization |
278
|
|
|
]); |
279
|
|
|
|
280
|
|
|
$view = Craft::$app->getView(); |
281
|
|
|
return $this->asJson([ |
282
|
|
|
'html' => Craft::$app->getView()->renderTemplate( |
283
|
|
|
"organizations/_cp/_components/userAssociationEditorHtml", |
284
|
|
|
[ |
285
|
|
|
'association' => $userAssociation |
286
|
|
|
] |
287
|
|
|
), |
288
|
|
|
'headHtml' => $view->getHeadHtml(), |
289
|
|
|
'footHtml' => $view->getBodyHtml() |
290
|
|
|
]); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|