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 as OrganizationElement; |
13
|
|
|
use flipbox\organization\Organization as OrganizationPlugin; |
14
|
|
|
use yii\web\ForbiddenHttpException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class OrganizationController extends AbstractController |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return null|\yii\web\Response |
26
|
|
|
* @throws ForbiddenHttpException |
27
|
|
|
*/ |
28
|
|
|
public function actionSave() |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
// POST, PUT, PATCH |
32
|
|
|
$this->requirePostPutPatchRequest(); |
33
|
|
|
$this->requireAdmin(); |
34
|
|
|
|
35
|
|
|
$organizationService = OrganizationPlugin::getInstance()->getOrganization(); |
36
|
|
|
|
37
|
|
|
// Organization Id |
38
|
|
|
if ($organizationIdentifier = Craft::$app->getRequest()->getBodyParam('identifier')) { |
39
|
|
|
$organizationElement = $organizationService->get($organizationIdentifier); |
40
|
|
|
} else { |
41
|
|
|
$organizationElement = $organizationService->create(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @var OrganizationElement $organizationElement */ |
45
|
|
|
$userElement = Craft::$app->getUser()->getIdentity(); |
46
|
|
|
|
47
|
|
|
if ($organizationElement->getId()) { |
|
|
|
|
48
|
|
|
if (!OrganizationPlugin::getInstance()->getPermission()->canCreateOrganization($userElement)) { |
49
|
|
|
throw new ForbiddenHttpException("You do not have permission to create an organization."); |
50
|
|
|
} |
51
|
|
|
} else { |
52
|
|
|
if (!OrganizationPlugin::getInstance()->getPermission()->canUpdateOrganization( |
53
|
|
|
$userElement, |
54
|
|
|
$organizationElement |
55
|
|
|
)) { |
56
|
|
|
throw new ForbiddenHttpException("You do not have permission to update an organization."); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Populate element |
61
|
|
|
$organizationService->populateFromRequest($organizationElement); |
62
|
|
|
|
63
|
|
|
// Save |
64
|
|
|
if (Craft::$app->getElements()->saveElement($organizationElement)) { |
65
|
|
|
// Success message |
66
|
|
|
$message = Craft::t('organization', 'Successfully saved organization.'); |
67
|
|
|
|
68
|
|
|
// Ajax request |
69
|
|
|
if (Craft::$app->getRequest()->isAjax) { |
70
|
|
|
return $this->asJson([ |
71
|
|
|
'success' => true, |
72
|
|
|
'message' => $message |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Flash success message |
77
|
|
|
Craft::$app->getSession()->setNotice($message); |
78
|
|
|
|
79
|
|
|
// Redirect |
80
|
|
|
return $this->redirectToPostedUrl($organizationElement); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Fail message |
84
|
|
|
$message = Craft::t('organization', 'Failed to saved organization.'); |
85
|
|
|
|
86
|
|
|
// Ajax request |
87
|
|
|
if (Craft::$app->getRequest()->isAjax) { |
88
|
|
|
return $this->asErrorJson( |
89
|
|
|
$organizationElement->getErrors() |
|
|
|
|
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Flash fail message |
94
|
|
|
Craft::$app->getSession()->setError($message); |
95
|
|
|
|
96
|
|
|
// Send the element back to the template |
97
|
|
|
Craft::$app->getUrlManager()->setRouteParams([ |
98
|
|
|
'organization' => $organizationElement |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
return null; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return \yii\web\Response |
106
|
|
|
*/ |
107
|
|
|
public function actionDelete() |
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
// POST, DELETE |
111
|
|
|
$this->requirePostDeleteRequest(); |
112
|
|
|
|
113
|
|
|
// Optional attributes |
114
|
|
|
$organizationId = Craft::$app->getRequest()->getRequiredBodyParam('identifier'); |
115
|
|
|
|
116
|
|
|
/** @var OrganizationElement $organizationElement */ |
117
|
|
|
$organizationElement = OrganizationPlugin::getInstance()->getOrganization()->getById($organizationId); |
118
|
|
|
|
119
|
|
|
// Delete |
120
|
|
|
if (Craft::$app->getElements()->deleteElement($organizationElement)) { |
121
|
|
|
// Success message |
122
|
|
|
$message = Craft::t('organization', 'Successfully deleted organization.'); |
123
|
|
|
|
124
|
|
|
// Ajax request |
125
|
|
|
if (Craft::$app->getRequest()->isAjax) { |
126
|
|
|
return $this->asJson([ |
127
|
|
|
'success' => true, |
128
|
|
|
'message' => $message |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Flash success message |
133
|
|
|
Craft::$app->getSession()->setNotice($message); |
134
|
|
|
|
135
|
|
|
// Redirect |
136
|
|
|
return $this->redirectToPostedUrl($organizationElement); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Fail message |
140
|
|
|
$message = Craft::t('organization', 'Failed to delete organization.'); |
141
|
|
|
|
142
|
|
|
// Ajax request |
143
|
|
|
if (Craft::$app->getRequest()->isAjax) { |
144
|
|
|
return $this->asErrorJson( |
145
|
|
|
$organizationElement->getErrors() |
|
|
|
|
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Flash fail message |
150
|
|
|
Craft::$app->getSession()->setError($message); |
151
|
|
|
|
152
|
|
|
return null; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: