Completed
Push — master ( 53489f...89bc16 )
by Igor
02:21
created

UserController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

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 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use app\models\UserModelSearch;
6
use Yii;
7
use app\models\UserModel;
8
use yii\web\Controller;
9
use yii\web\NotFoundHttpException;
10
use yii\filters\VerbFilter;
11
use yii2mod\editable\EditableAction;
12
13
/**
14
 * UserController implements the CRUD actions for UserModel model.
15
 */
16
class UserController extends Controller
17
{
18
    /**
19
     * Behaviors
20
     * @return array
21
     */
22
    public function behaviors()
23
    {
24
        return [
25
            'verbs' => [
26
                'class' => VerbFilter::className(),
27
                'actions' => [
28
                    'delete' => ['post'],
29
                ],
30
            ],
31
        ];
32
    }
33
34
    /**
35
     * Declares external actions for the controller.
36
     * This method is meant to be overwritten to declare external actions for the controller.
37
     * @return array
38
     */
39
    public function actions()
40
    {
41
        return [
42
            'edit-user' => [
43
                'class' => EditableAction::className(),
44
                'modelClass' => UserModel::className(),
45
                'forceCreate' => false
46
            ]
47
        ];
48
    }
49
50
    /**
51
     * Lists all users.
52
     * @return mixed
53
     */
54
    public function actionIndex()
55
    {
56
        $searchModel = new UserModelSearch();
57
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
58
59
        return $this->render('index', [
60
            'dataProvider' => $dataProvider,
61
            'searchModel' => $searchModel,
62
        ]);
63
    }
64
65
    /**
66
     * Creates a new UserModel model.
67
     * If creation is successful, the browser will be redirected to the 'view' page.
68
     * @return mixed
69
     */
70
    public function actionCreate()
71
    {
72
        $model = new UserModel(['scenario' => 'createUser']);
73
        if ($model->load(Yii::$app->request->post())) {
74
            if ($model->createUser()) {
75
                Yii::$app->session->setFlash('success', 'User has been created.');
76
                return $this->redirect(['index']);
77
            }
78
        }
79
80
        return $this->render('create', [
81
            'model' => $model,
82
        ]);
83
84
    }
85
86
    /**
87
     * Updates an existing UserModel model.
88
     * If update is successful, the browser will be redirected to the 'view' page.
89
     * @param integer $id
90
     * @return mixed
91
     */
92
    public function actionUpdate($id)
93
    {
94
        $model = $this->findModel($id);
95
96
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
0 ignored issues
show
Bug introduced by
The method load cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method validate cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
97
            if (!empty($model->newPassword)) {
98
                $model->setPassword($model->newPassword);
0 ignored issues
show
Bug introduced by
The method setPassword cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
99
            }
100
            $model->save(false);
0 ignored issues
show
Bug introduced by
The method save cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
101
            Yii::$app->session->setFlash('success', 'User has been saved.');
102
            return $this->redirect(['index']);
103
        }
104
105
        return $this->render('update', [
106
            'model' => $model,
107
        ]);
108
109
    }
110
111
    /**
112
     * Deletes an existing UserModel model.
113
     * If deletion is successful, the browser will be redirected to the 'index' page.
114
     * @param integer $id
115
     * @return mixed
116
     */
117
    public function actionDelete($id)
118
    {
119
        $this->findModel($id)->delete();
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $this->findModel($id) (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
120
        Yii::$app->session->setFlash('success', 'User has been deleted.');
121
        return $this->redirect(['index']);
122
    }
123
124
    /**
125
     * Finds the UserModel model based on its primary key value.
126
     * If the model is not found, a 404 HTTP exception will be thrown.
127
     * @param integer $id
128
     * @return UserModel the loaded model
129
     * @throws NotFoundHttpException if the model cannot be found
130
     */
131
    protected function findModel($id)
132
    {
133
        if (($model = UserModel::findOne($id)) !== null) {
134
            return $model;
135
        } else {
136
            throw new NotFoundHttpException('The requested page does not exist.');
137
        }
138
    }
139
}
140