MenuController::actionIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace toir427\admin\controllers;
4
5
use Yii;
6
use toir427\admin\models\Menu;
7
use toir427\admin\models\searchs\Menu as MenuSearch;
8
use yii\web\Controller;
9
use yii\web\NotFoundHttpException;
10
use yii\filters\VerbFilter;
11
use toir427\admin\components\Helper;
12
13
/**
14
 * MenuController implements the CRUD actions for Menu model.
15
 *
16
 * @author Misbahul D Munir <[email protected]>
17
 * @since 1.0
18
 */
19
class MenuController extends Controller
20
{
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function behaviors()
26
    {
27
        return [
28
            'verbs' => [
29
                'class' => VerbFilter::className(),
30
                'actions' => [
31
                    'delete' => ['post'],
32
                ],
33
            ],
34
        ];
35
    }
36
37
    /**
38
     * Lists all Menu models.
39
     * @return mixed
40
     */
41
    public function actionIndex()
42
    {
43
        $searchModel = new MenuSearch;
44
        $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
45
46
        return $this->render('index', [
47
                'dataProvider' => $dataProvider,
48
                'searchModel' => $searchModel,
49
        ]);
50
    }
51
52
    /**
53
     * Displays a single Menu model.
54
     * @param  integer $id
55
     * @return mixed
56
     */
57
    public function actionView($id)
58
    {
59
        return $this->render('view', [
60
                'model' => $this->findModel($id),
61
        ]);
62
    }
63
64
    /**
65
     * Creates a new Menu model.
66
     * If creation is successful, the browser will be redirected to the 'view' page.
67
     * @return mixed
68
     */
69
    public function actionCreate()
70
    {
71
        $model = new Menu;
72
73
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
74
            Helper::invalidate();
75
            return $this->redirect(['view', 'id' => $model->id]);
76
        } else {
77
            return $this->render('create', [
78
                    'model' => $model,
79
            ]);
80
        }
81
    }
82
83
    /**
84
     * Updates an existing Menu model.
85
     * If update is successful, the browser will be redirected to the 'view' page.
86
     * @param  integer $id
87
     * @return mixed
88
     */
89
    public function actionUpdate($id)
90
    {
91
        $model = $this->findModel($id);
92
        if ($model->menuParent) {
93
            $model->parent_name = $model->menuParent->name;
94
        }
95
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
96
            Helper::invalidate();
97
            return $this->redirect(['view', 'id' => $model->id]);
98
        } else {
99
            return $this->render('update', [
100
                    'model' => $model,
101
            ]);
102
        }
103
    }
104
105
    /**
106
     * Deletes an existing Menu model.
107
     * If deletion is successful, the browser will be redirected to the 'index' page.
108
     * @param  integer $id
109
     * @return mixed
110
     */
111
    public function actionDelete($id)
112
    {
113
        $this->findModel($id)->delete();
114
        Helper::invalidate();
115
116
        return $this->redirect(['index']);
117
    }
118
119
    /**
120
     * Finds the Menu model based on its primary key value.
121
     * If the model is not found, a 404 HTTP exception will be thrown.
122
     * @param  integer $id
123
     * @return Menu the loaded model
124
     * @throws NotFoundHttpException if the model cannot be found
125
     */
126
    protected function findModel($id)
127
    {
128
        if (($model = Menu::findOne($id)) !== null) {
129
            return $model;
130
        } else {
131
            throw new NotFoundHttpException('The requested page does not exist.');
132
        }
133
    }
134
}
135