|
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\forms\SetUpForm; |
|
16
|
|
|
use rhosocial\organization\Organization; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
use yii\filters\AccessControl; |
|
19
|
|
|
use yii\filters\VerbFilter; |
|
20
|
|
|
use yii\web\BadRequestHttpException; |
|
21
|
|
|
use yii\web\Controller; |
|
22
|
|
|
use yii\web\UnauthorizedHttpException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Organization Controller, designed for user module. |
|
26
|
|
|
* |
|
27
|
|
|
* @version 1.0 |
|
28
|
|
|
* @author vistart <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class OrganizationController extends Controller |
|
31
|
|
|
{ |
|
32
|
|
|
public $layout = '@rhosocial/organization/web/user/views/layouts/organization'; |
|
33
|
|
|
const RESULT_SUCCESS = 'success'; |
|
34
|
|
|
const RESULT_FAILED = 'failed'; |
|
35
|
|
|
const SESSION_KEY_MESSAGE = 'session_key_message'; |
|
36
|
|
|
const SESSION_KEY_RESULT = 'session_key_result'; |
|
37
|
|
|
public $organizationSetUpSuccessMessage; |
|
38
|
|
|
public $organizationSetUpFailedMessage; |
|
39
|
|
|
public $departmentSetUpSuccessMessage; |
|
40
|
|
|
public $departmentSetUpFailedMessage; |
|
41
|
|
|
public $organizationRevokeSuccessMessage; |
|
42
|
|
|
public $organizationRevokeFailedMessage; |
|
43
|
|
|
|
|
44
|
|
|
public $viewBasePath = '@rhosocial/organization/web/user/views/organization/'; |
|
45
|
|
|
|
|
46
|
|
|
protected function initMessages() |
|
47
|
|
|
{ |
|
48
|
|
|
if (!is_string($this->organizationSetUpSuccessMessage)) { |
|
49
|
|
|
$this->organizationSetUpSuccessMessage = Yii::t('organization' ,'Organization Set Up.'); |
|
50
|
|
|
} |
|
51
|
|
|
if (!is_string($this->organizationSetUpFailedMessage)) { |
|
52
|
|
|
$this->organizationSetUpFailedMessage = Yii::t('organization', 'Organization Set Up Failed.'); |
|
53
|
|
|
} |
|
54
|
|
|
if (!is_string($this->departmentSetUpSuccessMessage)) { |
|
55
|
|
|
$this->departmentSetUpSuccessMessage = Yii::t('organization' ,'Department Set Up.'); |
|
56
|
|
|
} |
|
57
|
|
|
if (!is_string($this->departmentSetUpFailedMessage)) { |
|
58
|
|
|
$this->departmentSetUpFailedMessage = Yii::t('organization', 'Department Set Up Failed.'); |
|
59
|
|
|
} |
|
60
|
|
|
if (!is_string($this->organizationRevokeSuccessMessage)) { |
|
61
|
|
|
$this->organizationRevokeSuccessMessage = Yii::t('organization', 'Successfully revoked.'); |
|
62
|
|
|
} |
|
63
|
|
|
if (!is_string($this->organizationRevokeFailedMessage)) { |
|
64
|
|
|
$this->organizationRevokeFailedMessage = Yii::t('organization', 'Failed to revoke.'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function init() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->initMessages(); |
|
71
|
|
|
parent::init(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get organization by specific parameter. |
|
76
|
|
|
* @param Organization|string|integer $organization |
|
77
|
|
|
* @return Organization |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getOrganization($organization) |
|
80
|
|
|
{ |
|
81
|
|
|
if (!$organization) { |
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
$class = Yii::$app->user->identity->organizationClass; |
|
85
|
|
|
if ($organization instanceof $class) { |
|
86
|
|
|
$organization = $organization->getID(); |
|
87
|
|
|
} |
|
88
|
|
|
if (is_numeric($organization) || is_int($organization)) { |
|
89
|
|
|
return $class::find()->id($organization)->one(); |
|
90
|
|
|
} |
|
91
|
|
|
if (is_string($organization) && strlen($organization) == 16) { |
|
92
|
|
|
return $class::find()->guid($organization)->one(); |
|
93
|
|
|
} |
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @inheritdoc |
|
99
|
|
|
*/ |
|
100
|
|
|
public function actions() |
|
101
|
|
|
{ |
|
102
|
|
|
return [ |
|
103
|
|
|
'list' => [ |
|
104
|
|
|
'class' => 'rhosocial\organization\web\user\controllers\organization\ListAction', |
|
105
|
|
|
], |
|
106
|
|
|
'revoke' => [ |
|
107
|
|
|
'class' => 'rhosocial\organization\web\user\controllers\organization\RevokeAction', |
|
108
|
|
|
], |
|
109
|
|
|
'view-members' => [ |
|
110
|
|
|
'class' => 'rhosocial\organization\web\user\controllers\organization\ViewMembersAction', |
|
111
|
|
|
], |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @inheritdoc |
|
117
|
|
|
*/ |
|
118
|
|
|
public function behaviors() |
|
119
|
|
|
{ |
|
120
|
|
|
return [ |
|
121
|
|
|
'access' => [ |
|
122
|
|
|
'class' => AccessControl::class, |
|
123
|
|
|
'rules' => [ |
|
124
|
|
|
[ // Disallow all unauthorized users to access this controller. |
|
125
|
|
|
'allow' => false, |
|
126
|
|
|
'roles' => ['?'], |
|
127
|
|
|
], |
|
128
|
|
|
[ // Disallow user who does not have `setUpOrganization` permission to access this `set-up-organization` action. |
|
129
|
|
|
'actions' => ['set-up-organization'], |
|
130
|
|
|
'allow' => false, |
|
131
|
|
|
'matchCallback' => function ($rule, $action) { |
|
|
|
|
|
|
132
|
|
|
return !Yii::$app->user->can('setUpOrganization'); |
|
133
|
|
|
}, |
|
134
|
|
|
'denyCallback' => function ($rule, $action) { |
|
|
|
|
|
|
135
|
|
|
throw new UnauthorizedHttpException(Yii::t('organization', 'You do not have access to set up new organization.')); |
|
136
|
|
|
}, |
|
137
|
|
|
], |
|
138
|
|
|
[ |
|
139
|
|
|
'allow' => true, |
|
140
|
|
|
'roles' => ['@'], |
|
141
|
|
|
] |
|
142
|
|
|
], |
|
143
|
|
|
], |
|
144
|
|
|
'verbs' => [ |
|
145
|
|
|
'class' => VerbFilter::class, |
|
146
|
|
|
'actions' => [ |
|
147
|
|
|
'deregister' => ['post'], |
|
148
|
|
|
'revoke' => ['post'], |
|
149
|
|
|
], |
|
150
|
|
|
], |
|
151
|
|
|
]; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function actionIndex() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->render($this->viewBasePath . 'index'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return string the rendering result. |
|
161
|
|
|
*/ |
|
162
|
|
|
public function actionSetUpOrganization() |
|
163
|
|
|
{ |
|
164
|
|
|
$model = new SetUpForm(['user' => Yii::$app->user->identity]); |
|
165
|
|
|
if ($model->load(Yii::$app->request->post())) { |
|
166
|
|
|
try { |
|
167
|
|
|
if (($result = $model->setUpOrganization()) === true) { |
|
168
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS); |
|
169
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, '(' . $model->getUser()->lastSetUpOrganization->getID() . ') ' . $this->organizationSetUpSuccessMessage); |
|
170
|
|
|
return $this->redirect(['list']); |
|
171
|
|
|
} |
|
172
|
|
|
if ($result instanceof \Exception) { |
|
173
|
|
|
throw $result; |
|
174
|
|
|
} |
|
175
|
|
|
} catch (\Exception $ex) { |
|
176
|
|
|
Yii::error($ex->getMessage(), __METHOD__); |
|
177
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_FAILED); |
|
178
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->organizationSetUpFailedMessage); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
return $this->render($this->viewBasePath . 'set-up-organization', ['model' => $model]); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Set up department. |
|
186
|
|
|
* @param string $parent Parent organization or department ID. |
|
187
|
|
|
* @return string the rendering result. |
|
188
|
|
|
*/ |
|
189
|
|
|
public function actionSetUpDepartment($parent) |
|
190
|
|
|
{ |
|
191
|
|
|
$model = new SetUpForm(['user' => Yii::$app->user->identity, 'parent' => $parent]); |
|
192
|
|
|
if (!$model->getParent()) { |
|
193
|
|
|
throw new BadRequestHttpException(Yii::t('organization', 'Parent Organization/Department Not Exist.')); |
|
194
|
|
|
} |
|
195
|
|
|
if ($model->load(Yii::$app->request->post())) { |
|
196
|
|
|
try { |
|
197
|
|
|
if (($result = $model->setUpDepartment()) === true) { |
|
198
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_SUCCESS); |
|
199
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, '(' . $model->getUser()->lastSetUpOrganization->getID() . ') ' . $this->departmentSetUpSuccessMessage); |
|
200
|
|
|
return $this->redirect(['list']); |
|
201
|
|
|
} |
|
202
|
|
|
if ($result instanceof \Exception) { |
|
203
|
|
|
throw $result; |
|
204
|
|
|
} |
|
205
|
|
|
} catch (\Exception $ex) { |
|
206
|
|
|
Yii::error($ex->getMessage(), __METHOD__); |
|
207
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_RESULT, self::RESULT_FAILED); |
|
208
|
|
|
Yii::$app->session->setFlash(self::SESSION_KEY_MESSAGE, $this->departmentSetUpFailedMessage); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
return $this->render($this->viewBasePath . 'set-up-organization', ['model' => $model]); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function actionView($id) |
|
215
|
|
|
{ |
|
216
|
|
|
$user = Yii::$app->user->identity; |
|
217
|
|
|
$organization = $user->getAtOrganizations()->id($id)->one(); |
|
218
|
|
|
$profile = $organization->profile; |
|
|
|
|
|
|
219
|
|
|
return $this->render($this->viewBasePath . 'view'); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function actionUpdate($id) |
|
|
|
|
|
|
223
|
|
|
{ |
|
224
|
|
|
return $this->render($this->viewBasePath . 'update'); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.