Issues (12)

controllers/DefaultController.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace yii2mod\settings\controllers;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use yii\web\Controller;
8
use yii\web\NotFoundHttpException;
9
use yii2mod\editable\EditableAction;
10
use yii2mod\settings\models\SettingModel;
11
12
/**
13
 * Class SettingController
14
 *
15
 * @package yii2mod\settings\controllers
16
 */
17
class DefaultController extends Controller
18
{
19
    /**
20
     * @var string path to index view file, which is used in admin panel
21
     */
22
    public $indexView = '@vendor/yii2mod/yii2-settings/views/default/index';
23
24
    /**
25
     * @var string path to create view file, which is used in admin panel
26
     */
27
    public $createView = '@vendor/yii2mod/yii2-settings/views/default/create';
28
29
    /**
30
     * @var string path to update view file, which is used in admin panel
31
     */
32
    public $updateView = '@vendor/yii2mod/yii2-settings/views/default/update';
33
34
    /**
35
     * @var string search class name for searching
36
     */
37
    public $searchClass = 'yii2mod\settings\models\search\SettingSearch';
38
39
    /**
40
     * @var string model class name for CRUD operations
41
     */
42
    public $modelClass = 'yii2mod\settings\models\SettingModel';
43
44
    /**
45
     * Returns a list of behaviors that this component should behave as.
46
     *
47
     * @return array
48
     */
49
    public function behaviors(): array
50
    {
51
        return [
52
            'verbs' => [
53
                'class' => VerbFilter::class,
54
                'actions' => [
55
                    'index' => ['get'],
56
                    'create' => ['get', 'post'],
57
                    'update' => ['get', 'post'],
58
                    'delete' => ['post'],
59
                ],
60
            ],
61
        ];
62
    }
63
64
    /**
65
     * Return a list of actions
66
     *
67
     * @return array
68
     */
69
    public function actions(): array
70
    {
71
        return [
72
            'edit-setting' => [
73
                'class' => EditableAction::class,
74
                'modelClass' => SettingModel::class,
75
                'forceCreate' => false,
76
            ],
77
        ];
78
    }
79
80
    /**
81
     * Lists all Settings.
82
     *
83
     * @return mixed
84
     */
85
    public function actionIndex()
86
    {
87
        $searchModel = Yii::createObject($this->searchClass);
88
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
89
90
        return $this->render($this->indexView, [
91
            'searchModel' => $searchModel,
92
            'dataProvider' => $dataProvider,
93
        ]);
94
    }
95
96
    /**
97
     * Creates a new Setting.
98
     *
99
     * If creation is successful, the browser will be redirected to the 'view' page.
100
     *
101
     * @return mixed
102
     */
103
    public function actionCreate()
104
    {
105
        $model = Yii::createObject($this->modelClass);
106
107
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
108
            Yii::$app->session->setFlash('success', Yii::t('yii2mod.settings', 'Setting has been created.'));
0 ignored issues
show
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
            Yii::$app->session->/** @scrutinizer ignore-call */ 
109
                                setFlash('success', Yii::t('yii2mod.settings', 'Setting has been created.'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
110
            return $this->redirect(['index']);
111
        } else {
112
            return $this->render($this->createView, [
113
                'model' => $model,
114
            ]);
115
        }
116
    }
117
118
    /**
119
     * Updates an existing Setting.
120
     *
121
     * If update is successful, the browser will be redirected to the 'view' page.
122
     *
123
     * @param int $id
124
     *
125
     * @return mixed
126
     */
127
    public function actionUpdate(int $id)
128
    {
129
        $model = $this->findModel($id);
130
131
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
132
            Yii::$app->session->setFlash('success', Yii::t('yii2mod.settings', 'Setting has been updated.'));
133
134
            return $this->redirect(['index']);
135
        } else {
136
            return $this->render($this->updateView, [
137
                'model' => $model,
138
            ]);
139
        }
140
    }
141
142
    /**
143
     * Deletes an existing Setting.
144
     *
145
     * If deletion is successful, the browser will be redirected to the 'index' page.
146
     *
147
     * @param int $id
148
     *
149
     * @return mixed
150
     */
151
    public function actionDelete(int $id)
152
    {
153
        $this->findModel($id)->delete();
154
        Yii::$app->session->setFlash('success', Yii::t('yii2mod.settings', 'Setting has been deleted.'));
155
156
        return $this->redirect(['index']);
157
    }
158
159
    /**
160
     * Finds a Setting model based on its primary key value.
161
     *
162
     * If the model is not found, a 404 HTTP exception will be thrown.
163
     *
164
     * @param int $id
165
     *
166
     * @return SettingModel the loaded model
167
     *
168
     * @throws NotFoundHttpException if the model cannot be found
169
     */
170
    protected function findModel(int $id)
171
    {
172
        $settingModelClass = $this->modelClass;
173
174
        if (($model = $settingModelClass::findOne($id)) !== null) {
175
            return $model;
176
        } else {
177
            throw new NotFoundHttpException(Yii::t('yii2mod.settings', 'The requested page does not exist.'));
178
        }
179
    }
180
}
181