|
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\organization\controllers; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use flipbox\organization\elements\Organization; |
|
13
|
|
|
use flipbox\organization\helpers\User as UserHelper; |
|
14
|
|
|
use flipbox\organization\Organization as OrganizationPlugin; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Flipbox Factory <[email protected]> |
|
18
|
|
|
* @since 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class UserController extends AbstractController |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return \yii\web\Response |
|
25
|
|
|
*/ |
|
26
|
|
|
public function actionDissociate() |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
// POST, DELETE |
|
30
|
|
|
$this->requirePostDeleteRequest(); |
|
31
|
|
|
|
|
32
|
|
|
// Get body params |
|
33
|
|
|
$userIdentifier = Craft::$app->getRequest()->getBodyParam('user'); |
|
34
|
|
|
$organizationIdentifier = Craft::$app->getRequest()->getBodyParam('organization'); |
|
35
|
|
|
|
|
36
|
|
|
// Get user element |
|
37
|
|
|
$userElement = UserHelper::get($userIdentifier); |
|
38
|
|
|
|
|
39
|
|
|
// Get organization element |
|
40
|
|
|
/** @var Organization $organizationElement */ |
|
41
|
|
|
$organizationElement = OrganizationPlugin::getInstance()->getOrganization()->get($organizationIdentifier); |
|
42
|
|
|
|
|
43
|
|
|
// Remove |
|
44
|
|
|
$success = OrganizationPlugin::getInstance()->getUser()->dissociate( |
|
45
|
|
|
$userElement, |
|
46
|
|
|
$organizationElement |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
// Action successful |
|
50
|
|
|
if ($success) { |
|
51
|
|
|
// Success message |
|
52
|
|
|
$message = Craft::t('organization', 'Successfully removed user from organization.'); |
|
53
|
|
|
|
|
54
|
|
|
// Ajax request |
|
55
|
|
|
if (Craft::$app->getRequest()->isAjax) { |
|
56
|
|
|
return $this->asJson([ |
|
57
|
|
|
'success' => true, |
|
58
|
|
|
'message' => $message |
|
59
|
|
|
]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Flash success message |
|
63
|
|
|
Craft::$app->getSession()->setNotice($message); |
|
64
|
|
|
|
|
65
|
|
|
// Redirect |
|
66
|
|
|
return $this->redirectToPostedUrl(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Fail message |
|
70
|
|
|
$message = Craft::t('organization', 'Failed to remove user from organization.'); |
|
71
|
|
|
|
|
72
|
|
|
// Ajax request |
|
73
|
|
|
if (Craft::$app->getRequest()->isAjax) { |
|
74
|
|
|
return $this->asErrorJson($message); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Flash fail message |
|
78
|
|
|
Craft::$app->getSession()->setError($message); |
|
79
|
|
|
|
|
80
|
|
|
return null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return \yii\web\Response |
|
85
|
|
|
*/ |
|
86
|
|
|
public function actionAssociate() |
|
87
|
|
|
{ |
|
88
|
|
|
|
|
89
|
|
|
// POST, PUT, PATCH |
|
90
|
|
|
$this->requirePostPutPatchRequest(); |
|
91
|
|
|
|
|
92
|
|
|
// Get body params |
|
93
|
|
|
$userIdentifier = Craft::$app->getRequest()->getBodyParam('user'); |
|
94
|
|
|
$organizationIdentifier = Craft::$app->getRequest()->getBodyParam('organization'); |
|
95
|
|
|
$siteId = Craft::$app->getRequest()->getBodyParam('siteId'); |
|
96
|
|
|
$sortOrder = Craft::$app->getRequest()->getBodyParam('sortOrder'); |
|
97
|
|
|
|
|
98
|
|
|
// Get user element |
|
99
|
|
|
$userElement = UserHelper::get($userIdentifier); |
|
100
|
|
|
|
|
101
|
|
|
// Get organization element |
|
102
|
|
|
/** @var Organization $organizationElement */ |
|
103
|
|
|
$organizationElement = OrganizationPlugin::getInstance()->getOrganization()->get($organizationIdentifier); |
|
104
|
|
|
|
|
105
|
|
|
// Action successful |
|
106
|
|
|
if (OrganizationPlugin::getInstance()->getUser()->associate( |
|
107
|
|
|
$userElement, |
|
108
|
|
|
$organizationElement, |
|
109
|
|
|
$siteId, |
|
110
|
|
|
$sortOrder |
|
111
|
|
|
)) { |
|
112
|
|
|
// Success message |
|
113
|
|
|
$message = Craft::t('organization', 'Successfully associated user to organization.'); |
|
114
|
|
|
|
|
115
|
|
|
// Ajax request |
|
116
|
|
|
if (Craft::$app->getRequest()->isAjax) { |
|
117
|
|
|
return $this->asJson([ |
|
118
|
|
|
'success' => true, |
|
119
|
|
|
'message' => $message |
|
120
|
|
|
]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// Flash success message |
|
124
|
|
|
Craft::$app->getSession()->setNotice($message); |
|
125
|
|
|
|
|
126
|
|
|
// Redirect |
|
127
|
|
|
return $this->redirectToPostedUrl(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Fail message |
|
131
|
|
|
$message = Craft::t('organization', 'Failed to associate user to organization.'); |
|
132
|
|
|
|
|
133
|
|
|
// Ajax request |
|
134
|
|
|
if (Craft::$app->getRequest()->isAjax) { |
|
135
|
|
|
return $this->asErrorJson($message); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// Flash fail message |
|
139
|
|
|
Craft::$app->getSession()->setError($message); |
|
140
|
|
|
|
|
141
|
|
|
return null; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|