/
SiteController.php
263 lines (172 loc) · 6.75 KB
/
SiteController.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
<?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;
// CMG Imports
use cmsgears\core\common\config\CoreGlobal;
use cmsgears\core\frontend\config\SiteProperties;
use cmsgears\core\frontend\config\CoreGlobalWeb;
use cmsgears\core\common\models\forms\Register;
use cmsgears\core\common\models\resources\UserMeta;
use cmsgears\core\common\models\mappers\SiteMember;
/**
* SiteController provides application actions. Most of these actions are specific to
* system pages.
*
* @since 1.0.0
*/
class SiteController extends \cmsgears\core\common\controllers\SiteController {
// Variables ---------------------------------------------------
// Globals ----------------
// Public -----------------
// Protected --------------
protected $siteMemberService;
// Private ----------------
private $siteProperties;
// Constructor and Initialisation ------------------------------
public function init() {
parent::init();
// Config
$this->layout = CoreGlobalWeb::LAYOUT_PUBLIC;
$this->admin = false;
// Services
$this->siteMemberService = Yii::$app->factory->get( 'siteMemberService' );
}
// Instance methods --------------------------------------------
// Yii interfaces ------------------------
// Yii parent classes --------------------
// yii\base\Component -----
public function behaviors() {
$behaviours = parent::behaviors();
$behaviours[ 'verbs' ][ 'actions' ][ 'index' ] = [ 'get' ];
$behaviours[ 'verbs' ][ 'actions' ][ 'maintenance' ] = [ 'get' ];
$behaviours[ 'verbs' ][ 'actions' ][ 'register' ] = [ 'get', 'post' ];
$behaviours[ 'verbs' ][ 'actions' ][ 'confirm-account' ] = [ 'get', 'post' ];
$behaviours[ 'verbs' ][ 'actions' ][ 'join-site' ] = [ 'get', 'post' ];
$behaviours[ 'verbs' ][ 'actions' ][ 'feedback' ] = [ 'get' ];
$behaviours[ 'verbs' ][ 'actions' ][ 'testimonial' ] = [ 'get' ];
return $behaviours;
}
// yii\base\Controller ----
public function actions() {
if( !Yii::$app->user->isGuest ) {
$this->layout = CoreGlobalWeb::LAYOUT_PRIVATE;
}
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
'error' => [
'class' => 'yii\web\ErrorAction'
]
];
}
// CMG interfaces ------------------------
// CMG parent classes --------------------
// SiteController ------------------------
public function getSiteProperties() {
if( !isset( $this->siteProperties ) ) {
$this->siteProperties = SiteProperties::getInstance();
}
return $this->siteProperties;
}
public function actionIndex() {
$this->layout = CoreGlobalWeb::LAYOUT_LANDING;
return $this->render( CoreGlobalWeb::PAGE_INDEX );
}
public function actionMaintenance() {
return $this->render( CoreGlobalWeb::PAGE_MAINTENANCE );
}
public function actionRegister() {
// Send user to home if already logged in
$this->checkHome();
// Create Form Model
$coreProperties = $this->getCoreProperties();
$model = new Register();
// Load and Validate Form Model
if( $coreProperties->isRegistration() && $model->load( Yii::$app->request->post() ) && $model->validate() ) {
// Register User
$user = $this->userService->register( $model );
if( isset( $user ) ) {
// Add User to current Site
$this->siteMemberService->createByParams( [ 'userId' => $user->id ] );
// Default Settings
$metaService = Yii::$app->factory->get( 'userMetaService' );
$metaService->initByNameType( $user->id, CoreGlobal::META_RECEIVE_EMAIL, CoreGlobal::SETTINGS_NOTIFICATION, UserMeta::VALUE_TYPE_FLAG );
$metaService->initByNameType( $user->id, CoreGlobal::META_RECEIVE_EMAIL, CoreGlobal::SETTINGS_REMINDER, UserMeta::VALUE_TYPE_FLAG );
// Send Register Mail
Yii::$app->coreMailer->sendRegisterMail( $user );
// Trigger New User Notification
$this->userService->notifyAdmin( $user, [
'template' => CoreGlobal::TPL_NOTIFY_USER_NEW,
'adminLink' => "core/user/update?id={$user->id}"
]);
// Set Flash Message
Yii::$app->session->setFlash( CoreGlobal::FLASH_GENERIC, Yii::$app->coreMessage->getMessage( CoreGlobal::MESSAGE_REGISTER ) );
// Refresh the Page
return $this->refresh();
}
}
return $this->render( CoreGlobalWeb::PAGE_REGISTER, [ CoreGlobal::MODEL_GENERIC => $model ] );
}
public function actionConfirmAccount( $token, $email ) {
// Send user to home if already logged in
$this->checkHome();
// Unset Flash Message
Yii::$app->session->setFlash( CoreGlobal::FLASH_GENERIC, null );
if( isset( $token ) && isset( $email ) ) {
$coreProperties = $this->getCoreProperties();
$user = $this->userService->getByEmail( $email );
if( isset( $user ) ) {
if( $this->userService->verify( $user, $token ) ) {
// Send Verify Mail
Yii::$app->coreMailer->sendVerifyUserMail( $user );
// Set Success Message
Yii::$app->session->setFlash( CoreGlobal::FLASH_GENERIC, Yii::$app->coreMessage->getMessage( CoreGlobal::MESSAGE_ACCOUNT_CONFIRM ) );
// Autologin
if( $coreProperties->isAutoLogin() ) {
Yii::$app->user->login( $user, 3600 * 24 * 30 );
}
return $this->render( CoreGlobalWeb::PAGE_ACCOUNT_CONFIRM, [ 'confirmed' => true ] );
}
// Set Failure Message
Yii::$app->session->setFlash( CoreGlobal::FLASH_GENERIC, Yii::$app->coreMessage->getMessage( CoreGlobal::ERROR_ACCOUNT_CONFIRM ) );
return $this->render( CoreGlobalWeb::PAGE_ACCOUNT_CONFIRM, [ 'confirmed' => false ] );
}
}
// Set Failure Message
Yii::$app->session->setFlash( CoreGlobal::FLASH_GENERIC, Yii::$app->coreMessage->getMessage( CoreGlobal::ERROR_ACCOUNT_CONFIRM ) );
return $this->render( CoreGlobalWeb::PAGE_ACCOUNT_CONFIRM );
}
public function actionJoinSite() {
$model = new SiteMember();
$user = Yii::$app->core->getUser();
if( $model->load( Yii::$app->request->post(), $model->getClassName() ) && $model->validate() ) {
$siteId = Yii::$app->core->getSiteId();
$siteMember = $this->siteMemberService->findBySiteIdUserId( $siteId, $user->id );
if( !isset( $siteMember ) ) {
$siteMember = $this->siteMemberService->create( $user );
if( $siteMember ) {
$this->checkHome();
}
}
}
return $this->render( CoreGlobal::PAGE_SITE_MEMBER, [ 'user' => $user, 'model' => $model ] );
}
public function actionFeedback() {
$this->layout = CoreGlobalWeb::LAYOUT_PUBLIC;
return $this->render( CoreGlobalWeb::PAGE_FEEDBACK );
}
public function actionTestimonial() {
$this->layout = CoreGlobalWeb::LAYOUT_PUBLIC;
return $this->render( CoreGlobalWeb::PAGE_TESTIMONIAL );
}
}