Passed
Push — master ( b30e74...f26942 )
by Alexey
02:34
created

DefaultController::processChangeStatus()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace modules\users\controllers\backend;
4
5
use Yii;
6
use modules\users\models\User;
7
use modules\users\models\search\UserSearch;
8
use yii\web\NotFoundHttpException;
9
use modules\rbac\models\Assignment;
10
use modules\users\Module;
11
12
/**
13
 * Class DefaultController
14
 * @package modules\users\controllers\backend
15
 */
16
class DefaultController extends BaseController
17
{
18
    /**
19
     * Lists all User models.
20
     * @return mixed
21
     */
22
    public function actionIndex()
23
    {
24
        $searchModel = new UserSearch();
25
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
26
        $assignModel = new Assignment();
27
        return $this->render('index', [
28
            'searchModel' => $searchModel,
29
            'dataProvider' => $dataProvider,
30
            'assignModel' => $assignModel,
31
        ]);
32
    }
33
34
    /**
35
     * Displays a single User model.
36
     * @param int|string $id
37
     * @return string|\yii\web\Response
38
     * @throws NotFoundHttpException
39
     */
40
    public function actionView($id)
41
    {
42
        if ($model = $this->findModel($id)) {
43
            $assignModel = new Assignment([
44
                'user' => $model
45
            ]);
46
            return $this->render('view', [
47
                'model' => $model,
48
                'assignModel' => $assignModel,
49
            ]);
50
        }
51
        return $this->redirect(['index']);
52
    }
53
54
    /**
55
     * Creates a new User model.
56
     * If creation is successful, the browser will be redirected to the 'view' page.
57
     * @return string|\yii\web\Response
58
     */
59
    public function actionCreate()
60
    {
61
        $model = new User();
62
        $model->status = $model::STATUS_WAIT;
63
        /** @var \modules\users\models\User $identity */
64
        $identity = Yii::$app->user->identity;
65
        $model->registration_type = $identity->id;
66
67
        if ($model->load(Yii::$app->request->post())) {
68
            if ($model->save()) {
69
                return $this->redirect(['view', 'id' => $model->id]);
70
            }
71
        }
72
73
        $model->scenario = $model::SCENARIO_ADMIN_CREATE;
74
        return $this->render('create', [
75
            'model' => $model,
76
        ]);
77
    }
78
79
    /**
80
     * Updates an existing User model.
81
     * @param int|string $id
82
     * @return string|\yii\web\Response
83
     * @throws NotFoundHttpException
84
     */
85
    public function actionUpdate($id)
86
    {
87
        if ($model = $this->findModel($id)) {
88
            return $this->render('update', [
89
                'model' => $model,
90
            ]);
91
        }
92
        return $this->redirect(['index']);
93
    }
94
95
    /**
96
     * @param int|string $id
97
     * @return \yii\web\Response
98
     * @throws NotFoundHttpException
99
     */
100
    public function actionUpdateProfile($id)
101
    {
102
        if ($model = $this->findModel($id)) {
103
            $model->scenario = $model::SCENARIO_ADMIN_UPDATE;
104
105
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
106
                Yii::$app->session->setFlash('success', Module::t('module', 'Profile successfully changed.'));
107
            } else {
108
                Yii::$app->session->setFlash('error', Module::t('module', 'Error! Profile not changed.'));
109
            }
110
        }
111
        return $this->redirect(['update', 'id' => $model->id, 'tab' => 'profile']);
112
    }
113
114
    /**
115
     * @param int|string $id
116
     * @return \yii\web\Response
117
     * @throws NotFoundHttpException
118
     */
119
    public function actionUpdatePassword($id)
120
    {
121
        if ($model = $this->findModel($id)) {
122
            $model->scenario = $model::SCENARIO_ADMIN_PASSWORD_UPDATE;
123
124
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
125
                Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.'));
126
            } else {
127
                Yii::$app->session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.'));
128
            }
129
        }
130
        return $this->redirect(['update', 'id' => $model->id, 'tab' => 'password']);
131
    }
132
133
    /**
134
     * Change Status
135
     * @param int|string $id
136
     * @return array|\yii\web\Response
137
     * @throws NotFoundHttpException
138
     */
139
    public function actionStatus($id)
140
    {
141
        if ($model = $this->processChangeStatus($id)) {
142
            if (Yii::$app->request->isAjax) {
143
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
144
                return [
145
                    'body' => $model->getStatusLabelName(),
146
                    'success' => true,
147
                ];
148
            }
149
        }
150
        return $this->redirect(['index']);
151
    }
152
153
    /**
154
     * @param int|string $id
155
     * @return bool|User|null
156
     * @throws NotFoundHttpException
157
     */
158
    private function processChangeStatus($id)
159
    {
160
        $model = $this->findModel($id);
161
        /** @var object $identity */
162
        $identity = Yii::$app->user->identity;
163
        if ($model->id !== $identity->id && !$model->isSuperAdmin($model->id)) {
164
            $model->setStatus();
165
            $model->save();
166
            return $model;
167
        }
168
        return false;
169
    }
170
171
    /**
172
     * Deletes an existing User model.
173
     * If deletion is successful, the browser will be redirected to the 'index' page.
174
     * @param int|string $id
175
     * @return \yii\web\Response
176
     * @throws NotFoundHttpException
177
     * @throws \Exception
178
     * @throws \Throwable
179
     * @throws \yii\db\StaleObjectException
180
     */
181
    public function actionDelete($id)
182
    {
183
        $model = $this->findModel($id);
184
        if (!$model->isSuperAdmin()) {
185
            if ($model->isDeleted()) {
186
                $model->delete();
187
                Yii::$app->session->setFlash('success', Module::t('module', 'The user "{:name}" have been successfully deleted.', [':name' => $model->username]));
188
            } else {
189
                /** @var $model \yii2tech\ar\softdelete\SoftDeleteBehavior */
190
                $model->softDelete();
191
                /** @var $model User */
192
                Yii::$app->session->setFlash('success', Module::t('module', 'The user "{:name}" are marked as deleted.', [':name' => $model->username]));
193
            }
194
        }
195
        return $this->redirect(['index']);
196
    }
197
}
198