1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\cms\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii2mod\cms\models\CmsModel; |
7
|
|
|
use yii\web\Controller; |
8
|
|
|
use yii\web\NotFoundHttpException; |
9
|
|
|
use yii\filters\VerbFilter; |
10
|
|
|
use yii2mod\cms\models\search\CmsModelSearch; |
11
|
|
|
use yii2mod\editable\EditableAction; |
12
|
|
|
use yii2mod\toggle\actions\ToggleAction; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class CmsController |
16
|
|
|
* @package yii2mod\cms\controllers |
17
|
|
|
*/ |
18
|
|
|
class CmsController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string view path |
22
|
|
|
*/ |
23
|
|
|
public $viewPath = '@vendor/yii2mod/yii2-cms/views/cms/'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
|
|
public function behaviors() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
'verbs' => [ |
32
|
|
|
'class' => VerbFilter::className(), |
33
|
|
|
'actions' => [ |
34
|
|
|
'index' => ['get'], |
35
|
|
|
'create' => ['get', 'post'], |
36
|
|
|
'update' => ['get', 'post'], |
37
|
|
|
'delete' => ['post'] |
38
|
|
|
], |
39
|
|
|
] |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
public function actions() |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
'edit-page' => [ |
50
|
|
|
'class' => EditableAction::className(), |
51
|
|
|
'modelClass' => CmsModel::className(), |
52
|
|
|
'forceCreate' => false |
53
|
|
|
], |
54
|
|
|
'toggle' => [ |
55
|
|
|
'class' => ToggleAction::className(), |
56
|
|
|
'modelClass' => CmsModel::className(), |
57
|
|
|
] |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Lists all CmsModel models. |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function actionIndex() |
66
|
|
|
{ |
67
|
|
|
$searchModel = new CmsModelSearch(); |
68
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
69
|
|
|
|
70
|
|
|
return $this->render($this->viewPath . 'index', [ |
71
|
|
|
'dataProvider' => $dataProvider, |
72
|
|
|
'searchModel' => $searchModel |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Creates a new CmsModel model. |
78
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function actionCreate() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$model = new CmsModel(); |
84
|
|
|
|
85
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
86
|
|
|
Yii::$app->session->setFlash('success', Yii::t('yii2mod.cms', 'Page has been created.')); |
87
|
|
|
return $this->redirect(['index']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->render($this->viewPath . 'create', [ |
91
|
|
|
'model' => $model, |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Updates an existing CmsModel model. |
97
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
98
|
|
|
* |
99
|
|
|
* @param integer $id |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function actionUpdate($id) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$model = $this->findModel($id); |
106
|
|
|
|
107
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
108
|
|
|
Yii::$app->session->setFlash('success', Yii::t('yii2mod.cms', 'Page has been updated.')); |
109
|
|
|
return $this->redirect(['index']); |
110
|
|
|
} |
111
|
|
|
return $this->render($this->viewPath . 'update', [ |
112
|
|
|
'model' => $model, |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Deletes an existing CmsModel model. |
118
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
119
|
|
|
* |
120
|
|
|
* @param integer $id |
121
|
|
|
* |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
|
|
public function actionDelete($id) |
125
|
|
|
{ |
126
|
|
|
$this->findModel($id)->delete(); |
127
|
|
|
Yii::$app->session->setFlash('success', Yii::t('yii2mod.cms', 'Page has been deleted.')); |
128
|
|
|
return $this->redirect(['index']); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Finds the CmsModel model based on its primary key value. |
133
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
134
|
|
|
* |
135
|
|
|
* @param integer $id |
136
|
|
|
* |
137
|
|
|
* @return CmsModel the loaded model |
138
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
protected function findModel($id) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
if (($model = CmsModel::findOne($id)) !== null) { |
143
|
|
|
return $model; |
144
|
|
|
} else { |
145
|
|
|
throw new NotFoundHttpException(Yii::t('yii2mod.cms', 'The requested page does not exist.')); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.