Completed
Push — master ( ccfeeb...7d9725 )
by Alexey
02:50 queued 46s
created

ProfileController::actionIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace modules\users\controllers;
4
5
use Yii;
6
use yii\helpers\VarDumper;
7
use yii\web\Controller;
8
use modules\users\models\Profile;
9
use yii\filters\AccessControl;
10
use yii\filters\VerbFilter;
11
use yii\web\NotFoundHttpException;
12
use yii\bootstrap\ActiveForm;
13
use yii\web\Response;
14
use modules\users\Module;
15
16
/**
17
 * Class ProfileController
18
 * @package modules\users\controllers
19
 */
20
class ProfileController extends Controller
21
{
22
    /** @var  string|bool $jsFile */
23
    protected $jsFile;
24
25
    /**
26
     * @inheritdoc
27
     * @return array
28
     */
29
    public function behaviors()
30
    {
31
        return [
32
            'verbs' => [
33
                'class' => VerbFilter::class,
34
                'actions' => [
35
                    'delete' => ['post'],
36
                ],
37
            ],
38
            'access' => [
39
                'class' => AccessControl::class,
40
                'rules' => [
41
                    [
42
                        'allow' => true,
43
                        'roles' => ['@']
44
                    ],
45
                ],
46
            ],
47
        ];
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function init()
54
    {
55
        parent::init();
56
        $this->processRegisterJs();
57
    }
58
59
    /**
60
     * Publish and register the required JS file
61
     */
62
    protected function processRegisterJs()
63
    {
64
        $this->jsFile = '@modules/users/views/ajax/ajax.js';
65
        $assetManager = Yii::$app->assetManager;
66
        $assetManager->publish($this->jsFile);
67
        $url = $assetManager->getPublishedUrl($this->jsFile);
68
        $this->view->registerJsFile($url,
69
            ['depends' => 'yii\web\JqueryAsset',] // depends
70
        );
71
    }
72
73
    /**
74
     * @return string
75
     * @throws NotFoundHttpException
76
     */
77
    public function actionIndex()
78
    {
79
        $model = $this->findModel();
80
        return $this->render('index', [
81
            'model' => $model
82
        ]);
83
    }
84
85
    /**
86
     * @return string
87
     * @throws NotFoundHttpException
88
     */
89
    public function actionUpdate()
90
    {
91
        $model = $this->findModel();
92
        return $this->render('update', [
93
            'model' => $model,
94
        ]);
95
    }
96
97
    /**
98
     * @return Response
99
     * @throws NotFoundHttpException
100
     */
101
    public function actionUpdateProfile()
102
    {
103
        $model = $this->findModel();
104
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
105
            Yii::$app->session->setFlash('success', Module::t('module', 'Profile successfully changed.'));
106
        } else {
107
            Yii::$app->session->setFlash('error', Module::t('module', 'Error! Profile not changed.'));
108
        }
109
        return $this->redirect(['update', 'tab' => 'profile']);
110
    }
111
112
    /**
113
     * @return array|Response
114
     * @throws NotFoundHttpException
115
     */
116
    public function actionUpdatePassword()
117
    {
118
        $model = $this->findModel();
119
        $model->scenario = $model::SCENARIO_PASSWORD_UPDATE;
120
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
121
            Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.'));
122
        } else {
123
            Yii::$app->session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.'));
124
        }
125
        return $this->redirect(['update', 'tab' => 'password']);
126
    }
127
128
    /**
129
     * @return array|Response
130
     * @throws NotFoundHttpException
131
     */
132
    public function actionAjaxValidatePasswordForm()
133
    {
134
        $model = $this->findModel();
135
        $model->scenario = $model::SCENARIO_PASSWORD_UPDATE;
136
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
137
            Yii::$app->response->format = Response::FORMAT_JSON;
138
            return ActiveForm::validate($model);
139
        }
140
        return $this->redirect(['index']);
141
    }
142
143
    /**
144
     * Action Generate new auth key
145
     * @throws NotFoundHttpException
146
     */
147
    public function actionGenerateAuthKey()
148
    {
149
        $model = $this->processGenerateAuthKey();
150
        if (Yii::$app->request->isAjax) {
151
            Yii::$app->response->format = Response::FORMAT_JSON;
152
            return [
153
                'body' => $this->renderAjax('_auth_key', ['model' => $model]),
154
                'success' => true,
155
            ];
156
        }
157
        return $this->redirect(['index']);
158
    }
159
160
    /**
161
     * Generate new auth key
162
     * @return Profile|null
163
     * @throws NotFoundHttpException
164
     */
165
    private function processGenerateAuthKey()
166
    {
167
        $model = $this->findModel();
168
        $model->generateAuthKey();
169
        $model->save();
170
        return $model;
171
    }
172
173
    /**
174
     * Finds the User model based on its primary key value.
175
     * If the model is not found, a 404 HTTP exception will be thrown.
176
     * @return null|Profile the loaded model
177
     * @throws NotFoundHttpException if the model cannot be found
178
     */
179
    private function findModel()
180
    {
181
        if (!Yii::$app->user->isGuest) {
182
            /** @var object $identity */
183
            $identity = Yii::$app->user->identity;
184
            if (($model = Profile::findOne($identity->id)) !== null) {
185
                return $model;
186
            }
187
        }
188
        throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
189
    }
190
}
191