/
UserController.php
325 lines (240 loc) · 8.34 KB
/
UserController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* This file is part of CMSGears Framework. Please view License file distributed
* with the source code for license details.
*
* @link https://www.cmsgears.org/
* @copyright Copyright (c) 2015 VulpineCode Technologies Pvt. Ltd.
*/
namespace cmsgears\core\frontend\controllers;
// Yii Imports
use Yii;
use yii\filters\VerbFilter;
use yii\base\InvalidArgumentException;
// CMG Imports
use cmsgears\core\common\config\CoreGlobal;
use cmsgears\core\frontend\config\CoreGlobalWeb;
use cmsgears\core\common\models\forms\ResetPassword;
use cmsgears\core\common\models\resources\Address;
use cmsgears\core\common\models\resources\File;
/**
* UserController process the actions specific to User model.
*
* @since 1.0.0
*/
class UserController extends \cmsgears\core\frontend\controllers\base\Controller {
// Variables ---------------------------------------------------
// Globals ----------------
// Public -----------------
// Protected --------------
protected $optionService;
protected $countryService;
protected $provinceService;
protected $regionService;
protected $addressService;
protected $modelAddressService;
// Private ----------------
// Constructor and Initialisation ------------------------------
public function init() {
parent::init();
// Permission
$this->crudPermission = CoreGlobal::PERM_USER;
// Config
$this->baseUrl = '/user';
$this->apixBase = 'core/user';
// Services
$this->modelService = Yii::$app->factory->get( 'userService' );
$this->optionService = Yii::$app->factory->get( 'optionService' );
$this->countryService = Yii::$app->factory->get( 'countryService' );
$this->provinceService = Yii::$app->factory->get( 'provinceService' );
$this->regionService = Yii::$app->factory->get( 'regionService' );
$this->addressService = Yii::$app->factory->get( 'addressService' );
$this->modelAddressService = Yii::$app->factory->get( 'modelAddressService' );
}
// Instance methods --------------------------------------------
// Yii interfaces ------------------------
// Yii parent classes --------------------
// yii\base\Component -----
// yii\base\Controller ----
public function behaviors() {
return [
'rbac' => [
'class' => Yii::$app->core->getRbacFilterClass(),
'actions' => [
// User dashboard
'index' => [ 'permission' => $this->crudPermission ],
'home' => [ 'permission' => $this->crudPermission ],
// User details
'profile' => [ 'permission' => $this->crudPermission ],
'account' => [ 'permission' => $this->crudPermission ],
'address' => [ 'permission' => $this->crudPermission ],
'settings' => [ 'permission' => $this->crudPermission ]
]
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
// User dashboard
'index' => [ 'get' ],
'home' => [ 'get' ],
// User details
'profile' => [ 'get', 'post' ],
'account' => [ 'get', 'post' ],
'address' => [ 'get', 'post' ],
'settings' => [ 'get' ]
]
]
];
}
// CMG interfaces ------------------------
// CMG parent classes --------------------
// UserController ------------------------
// Redirect user to appropriate home page
public function actionIndex() {
$this->checkHome();
}
// Default home page for user
public function actionHome() {
return $this->render( CoreGlobalWeb::PAGE_INDEX );
}
/**
* The action profile allows users to update their profile.
*
* @return string
*/
public function actionProfile() {
// Find Model
$user = Yii::$app->core->getUser();
// Avatar
$avatar = File::loadFile( $user->avatar, 'Avatar' );
// Scenario
$user->setScenario( 'profile' );
if( $user->load( Yii::$app->request->post(), $user->getClassName() ) && $user->validate() ) {
// Update User
$this->modelService->update( $user, [ 'avatar' => $avatar ] );
// Refresh Page
return $this->refresh();
}
$genderMap = $this->optionService->getIdNameMapByCategorySlug( CoreGlobal::CATEGORY_GENDER, [
'prepend' => [ [ 'id' => '0', 'name' => 'Gender' ] ]
]);
$maritalMap = $this->optionService->getIdNameMapByCategorySlug( CoreGlobal::CATEGORY_MARITAL, [
'prepend' => [ [ 'id' => '0', 'name' => 'Marital Status' ] ]
]);
return $this->render( CoreGlobalWeb::PAGE_PROFILE, [
'user' => $user,
'avatar' => $avatar,
'genderMap' => $genderMap,
'maritalMap' => $maritalMap
]);
}
/**
* The account action allows user to change password.
*
* @return string
*/
public function actionAccount() {
// Find Model
$user = Yii::$app->core->getUser();
$model = new ResetPassword();
// Configure Model
$model->email = $user->email;
// Old password required if it was already set
if( !empty( $user->passwordHash ) ) {
$model->setScenario( 'oldPassword' );
}
if( $model->load( Yii::$app->request->post(), $model->getClassName() ) && $model->validate() ) {
// Update Password
if( $this->modelService->resetPassword( $user, $model, false ) ) {
// Send Password Change Mail
Yii::$app->coreMailer->sendPasswordChangeMail( $user );
}
return $this->refresh();
}
return $this->render( CoreGlobalWeb::PAGE_ACCOUNT, [
'user' => $user,
'model' => $model
]);
}
/**
* The address action allows user to update primary address using form submit. Use the
* corresponding Ajax Action to handle multiple user address.
*
* In case we need multiple address using form submit, this action can be overridden by
* child classes to load multiple address.
*
* @return string
*/
public function actionAddress( $ctype ) {
$user = Yii::$app->core->getUser();
$address = null;
// Accept only selected type for a user
if( !in_array( $ctype, Address::$minTypeList ) ) {
throw new InvalidArgumentException( 'Address type not allowed.' );
}
switch( $ctype ) {
case Address::TYPE_PRIMARY: {
$address = $user->primaryAddress;
break;
}
case Address::TYPE_BILLING: {
$address = $user->billingAddress;
break;
}
case Address::TYPE_MAILING: {
$address = $user->mailingAddress;
break;
}
case Address::TYPE_SHIPPING: {
$address = $user->shippingAddress;
break;
}
}
if( empty( $address ) ) {
$address = $this->addressService->getModelObject();
}
if( $address->load( Yii::$app->request->post(), $address->getClassName() ) && $address->validate() ) {
// Create/Update Address
$address = $this->addressService->createOrUpdate( $address );
if( $address ) {
// Create Mapping
$this->modelAddressService->activateByModelId( $user->id, CoreGlobal::TYPE_USER, $address->id, $ctype );
return $this->refresh();
}
}
$countryMap = Yii::$app->factory->get( 'countryService' )->getIdNameMap();
$countryId = isset( $address->country ) ? $address->country->id : array_keys( $countryMap )[ 0 ];
$provinceMap = Yii::$app->factory->get( 'provinceService' )->getMapByCountryId( $countryId, [ 'default' => true, 'defaultValue' => Yii::$app->core->provinceLabel ] );
$provinceId = isset( $address->province ) ? $address->province->id : array_keys( $provinceMap )[ 0 ];
$regionMap = Yii::$app->factory->get( 'regionService' )->getMapByProvinceId( $provinceId, [ 'default' => true, 'defaultValue' => Yii::$app->core->regionLabel ] );
return $this->render( 'address', [
'user' => $user,
'address' => $address,
'countryMap' => $countryMap,
'provinceMap' => $provinceMap,
'regionMap' => $regionMap
]);
}
/**
* The settings action pre-load the privacy, notification and reminder settings and
* send them to view. In case more settings are required, we can either override this
* action or use the model service to access additional settings.
*
* @return string
*/
public function actionSettings() {
$user = Yii::$app->core->getUser();
// Load key settings
$privacy = $this->modelService->getMetaNameMetaMapByType( $user, CoreGlobal::SETTINGS_PRIVACY );
$notification = $this->modelService->getMetaNameMetaMapByType( $user, CoreGlobal::SETTINGS_NOTIFICATION );
$reminder = $this->modelService->getMetaNameMetaMapByType( $user, CoreGlobal::SETTINGS_REMINDER );
// NOTE: Rest of the settings can be loaded in view if required.
// TODO: Check for options to cache all the user attributes.
return $this->render( CoreGlobalWeb::PAGE_SETTINGS, [
'user' => $user,
'privacy' => $privacy,
'notification' => $notification,
'reminder' => $reminder
]);
}
}