1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.me/ |
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
10
|
|
|
* @license https://vistart.me/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace rhosocial\organization\web\user\controllers; |
14
|
|
|
|
15
|
|
|
use rhosocial\organization\exceptions\RevokePreventedException; |
16
|
|
|
use rhosocial\organization\forms\SetUpForm; |
17
|
|
|
use rhosocial\organization\Organization; |
18
|
|
|
use Yii; |
19
|
|
|
use yii\base\InvalidParamException; |
20
|
|
|
use yii\data\ActiveDataProvider; |
21
|
|
|
use yii\filters\AccessControl; |
22
|
|
|
use yii\filters\VerbFilter; |
23
|
|
|
use yii\web\BadRequestHttpException; |
24
|
|
|
use yii\web\Controller; |
25
|
|
|
use yii\web\ServerErrorHttpException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Organization Controller, designed for user module. |
29
|
|
|
* |
30
|
|
|
* @version 1.0 |
31
|
|
|
* @author vistart <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class OrganizationController extends Controller |
34
|
|
|
{ |
35
|
|
|
public $layout = '@rhosocial/organization/web/user/views/layouts/organization'; |
36
|
|
|
const RESULT_SUCCESS = 'success'; |
37
|
|
|
const RESULT_FAILED = 'failed'; |
38
|
|
|
const SESSION_KEY_MESSAGE = 'session_key_message'; |
39
|
|
|
const SESSION_KEY_RESULT = 'session_key_result'; |
40
|
|
|
public $organizationSetUpSuccessMessage; |
41
|
|
|
public $organizationSetUpFailedMessage; |
42
|
|
|
public $departmentSetUpSuccessMessage; |
43
|
|
|
public $departmentSetUpFailedMessage; |
44
|
|
|
public $organizationRevokeSuccessMessage; |
45
|
|
|
public $organizationRevokeFailedMessage; |
46
|
|
|
|
47
|
|
|
public $viewBasePath = '@rhosocial/organization/web/user/views/organization/'; |
48
|
|
|
|
49
|
|
|
protected function initMessages() |
50
|
|
|
{ |
51
|
|
|
if (!is_string($this->organizationSetUpSuccessMessage)) { |
52
|
|
|
$this->organizationSetUpSuccessMessage = Yii::t('organization' ,'Organization Set Up.'); |
53
|
|
|
} |
54
|
|
|
if (!is_string($this->organizationSetUpFailedMessage)) { |
55
|
|
|
$this->organizationSetUpFailedMessage = Yii::t('organization', 'Organization Set Up Failed.'); |
56
|
|
|
} |
57
|
|
|
if (!is_string($this->departmentSetUpSuccessMessage)) { |
58
|
|
|
$this->departmentSetUpSuccessMessage = Yii::t('organization' ,'Department Set Up.'); |
59
|
|
|
} |
60
|
|
|
if (!is_string($this->departmentSetUpFailedMessage)) { |
61
|
|
|
$this->departmentSetUpFailedMessage = Yii::t('organization', 'Department Set Up Failed.'); |
62
|
|
|
} |
63
|
|
|
if (!is_string($this->organizationRevokeSuccessMessage)) { |
64
|
|
|
$this->organizationRevokeSuccessMessage = Yii::t('organization', 'Successfully revoked.'); |
65
|
|
|
} |
66
|
|
|
if (!is_string($this->organizationRevokeFailedMessage)) { |
67
|
|
|
$this->organizationRevokeFailedMessage = Yii::t('organization', 'Failed to revoke.'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function init() |
72
|
|
|
{ |
73
|
|
|
$this->initMessages(); |
74
|
|
|
parent::init(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get organization by specific parameter. |
79
|
|
|
* @param Organization|string|integer $organization |
80
|
|
|
* @return Organization |
81
|
|
|
*/ |
82
|
|
|
public function getOrganization($organization) |
83
|
|
|
{ |
84
|
|
|
if (!$organization) { |
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
$class = Yii::$app->user->identity->organizationClass; |
88
|
|
|
if ($organization instanceof $class) { |
89
|
|
|
$organization = $organization->getID(); |
90
|
|
|
} |
91
|
|
|
if (is_numeric($organization) || is_int($organization)) { |
92
|
|
|
return $class::find()->id($organization)->one(); |
93
|
|
|
} |
94
|
|
|
if (is_string($organization) && strlen($organization) == 16) { |
95
|
|
|
return $class::find()->guid($organization)->one(); |
96
|
|
|
} |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritdoc |
102
|
|
|
*/ |
103
|
|
|
public function actions() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
|
|
'view-members' => [ |
107
|
|
|
'class' => 'rhosocial\organization\web\user\controllers\organization\ViewMembersAction', |
108
|
|
|
], |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
|
|
public function behaviors() |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
'access' => [ |
119
|
|
|
'class' => AccessControl::class, |
120
|
|
|
'rules' => [ |
121
|
|
|
[ |
122
|
|
|
'allow' => true, |
123
|
|
|
'roles' => ['@'], |
124
|
|
|
], |
125
|
|
|
], |
126
|
|
|
], |
127
|
|
|
'verbs' => [ |
128
|
|
|
'class' => VerbFilter::class, |
129
|
|
|
'actions' => [ |
130
|
|
|
'deregister' => ['post'], |
131
|
|
|
], |
132
|
|
|
], |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function actionIndex() |
137
|
|
|
{ |
138
|
|
|
return $this->render($this->viewBasePath . 'index'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* List all organization(s) and department(s) which current user has joined in. |
143
|
|
|
* @return string the rendering result. |
144
|
|
|
*/ |
145
|
|
|
public function actionList() |
146
|
|
|
{ |
147
|
|
|
$identity = Yii::$app->user->identity; |
148
|
|
|
if (!$identity) { |
149
|
|
|
throw new ServerErrorHttpException('User Not Found.'); |
150
|
|
|
} |
151
|
|
|
$dataProvider = new ActiveDataProvider([ |
152
|
|
|
'query' => $identity->getAtOrganizations(), |
153
|
|
|
'pagination' => [ |
154
|
|
|
'pageParam' => 'oganization-page', |
155
|
|
|
'pageSize' => 20, |
156
|
|
|
], |
157
|
|
|
'sort' => [ |
158
|
|
|
'sortParam' => 'organization-sort', |
159
|
|
|
], |
160
|
|
|
]); |
161
|
|
|
return $this->render($this->viewBasePath . 'list', ['dataProvider' => $dataProvider]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string the rendering result. |
166
|
|
|
*/ |
167
|
|
|
public function actionSetUpOrganization() |
168
|
|
|
{ |
169
|
|
|
$model = new SetUpForm(['user' => Yii::$app->user->identity]); |
170
|
|
|
if ($model->load(Yii::$app->request->post())) { |
171
|
|
|
try { |
172
|
|
|
if (($result = $model->setUpOrganization()) === true) { |
173
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS); |
174
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, '(' . $model->getUser()->lastSetUpOrganization->getID() . ') ' . $this->organizationSetUpSuccessMessage); |
175
|
|
|
return $this->redirect(['list']); |
176
|
|
|
} |
177
|
|
|
if ($result instanceof \Exception) { |
178
|
|
|
throw $result; |
179
|
|
|
} |
180
|
|
|
} catch (\Exception $ex) { |
181
|
|
|
Yii::error($ex->getMessage(), __METHOD__); |
182
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_FAILED); |
183
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->organizationSetUpFailedMessage); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
return $this->render($this->viewBasePath . 'set-up-organization', ['model' => $model]); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set up department. |
191
|
|
|
* @param string $parent Parent organization or department ID. |
192
|
|
|
* @return string the rendering result. |
193
|
|
|
*/ |
194
|
|
|
public function actionSetUpDepartment($parent) |
195
|
|
|
{ |
196
|
|
|
$model = new SetUpForm(['user' => Yii::$app->user->identity, 'parent' => $parent]); |
197
|
|
|
if (!$model->getParent()) { |
198
|
|
|
throw new BadRequestHttpException(Yii::t('organization', 'Parent Organization/Department Not Exist.')); |
199
|
|
|
} |
200
|
|
|
if ($model->load(Yii::$app->request->post())) { |
201
|
|
|
try { |
202
|
|
|
if (($result = $model->setUpDepartment()) === true) { |
203
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS); |
204
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, '(' . $model->getUser()->lastSetUpOrganization->getID() . ') ' . $this->departmentSetUpSuccessMessage); |
205
|
|
|
return $this->redirect(['list']); |
206
|
|
|
} |
207
|
|
|
if ($result instanceof \Exception) { |
208
|
|
|
throw $result; |
209
|
|
|
} |
210
|
|
|
} catch (\Exception $ex) { |
211
|
|
|
Yii::error($ex->getMessage(), __METHOD__); |
212
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_FAILED); |
213
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->departmentSetUpFailedMessage); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
return $this->render($this->viewBasePath . 'set-up-organization', ['model' => $model]); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function actionView($id) |
220
|
|
|
{ |
221
|
|
|
$user = Yii::$app->user->identity; |
222
|
|
|
$organization = $user->getAtOrganizations()->id($id)->one(); |
223
|
|
|
$profile = $organization->profile; |
|
|
|
|
224
|
|
|
return $this->render($this->viewBasePath . 'view'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function actionUpdate($id) |
|
|
|
|
228
|
|
|
{ |
229
|
|
|
return $this->render($this->viewBasePath . 'update'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Revoke organization or department. |
234
|
|
|
* @param string|integer $id |
235
|
|
|
*/ |
236
|
|
|
public function actionRevoke($id) |
237
|
|
|
{ |
238
|
|
|
try { |
239
|
|
|
Yii::$app->user->identity->revokeOrganization($id, true); |
240
|
|
|
} catch (InvalidParamException $ex) { |
241
|
|
|
throw new BadRequestHttpException(Yii::t('organization', $ex->getMessage())); |
242
|
|
|
} catch (RevokePreventedException $ex) { |
243
|
|
|
throw new BadRequestHttpException(Yii::t('organization', $ex->getMessage())); |
244
|
|
|
} catch (\Exception $ex) { |
245
|
|
|
throw new ServerErrorHttpException($ex->getMessage()); |
246
|
|
|
} |
247
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS); |
248
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, "($id) " . $this->organizationRevokeSuccessMessage); |
249
|
|
|
return $this->redirect(['list']); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.