1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.writesdown.com/ |
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
5
|
|
|
* @license http://www.writesdown.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace backend\controllers; |
9
|
|
|
|
10
|
|
|
use common\models\search\Taxonomy as TaxonomySearch; |
11
|
|
|
use common\models\search\Term as TermSearch; |
12
|
|
|
use common\models\Taxonomy; |
13
|
|
|
use common\models\Term; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\filters\AccessControl; |
16
|
|
|
use yii\filters\VerbFilter; |
17
|
|
|
use yii\helpers\Html; |
18
|
|
|
use yii\web\Controller; |
19
|
|
|
use yii\web\NotFoundHttpException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* TaxonomyController implements the CRUD actions for Taxonomy model. |
23
|
|
|
* |
24
|
|
|
* @author Agiel K. Saputra <[email protected]> |
25
|
|
|
* @since 0.1.0 |
26
|
|
|
*/ |
27
|
|
|
class TaxonomyController extends Controller |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
public function behaviors() |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
|
36
|
|
|
'access' => [ |
37
|
|
|
'class' => AccessControl::className(), |
38
|
|
|
'rules' => [ |
39
|
|
|
[ |
40
|
|
|
'actions' => ['index', 'create', 'update', 'delete', 'bulk-action', 'ajax-create'], |
41
|
|
|
'allow' => true, |
42
|
|
|
'roles' => ['administrator'], |
43
|
|
|
], |
44
|
|
|
[ |
45
|
|
|
'actions' => ['view', 'update-term', 'delete-term'], |
46
|
|
|
'allow' => true, |
47
|
|
|
'roles' => ['editor'], |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
'verbs' => [ |
52
|
|
|
'class' => VerbFilter::className(), |
53
|
|
|
'actions' => [ |
54
|
|
|
'delete' => ['post'], |
55
|
|
|
'bulk-action' => ['post'], |
56
|
|
|
'ajax-create' => ['post'], |
57
|
|
|
'delete-term' => ['post'], |
58
|
|
|
], |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Lists all Taxonomy models. |
65
|
|
|
* |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function actionIndex() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$searchModel = new TaxonomySearch(); |
71
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
72
|
|
|
|
73
|
|
|
return $this->render('index', [ |
74
|
|
|
'searchModel' => $searchModel, |
75
|
|
|
'dataProvider' => $dataProvider, |
76
|
|
|
]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* View single taxonomy, list all Term from it and create a new Term model. |
81
|
|
|
* If create Term successful, the browser will be redirected to 'view taxonomy' page. |
82
|
|
|
* |
83
|
|
|
* @param integer $id |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function actionView($id) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$term = new Term(); |
89
|
|
|
$searchModel = new TermSearch(); |
90
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $id); |
91
|
|
|
|
92
|
|
|
if ($term->load(Yii::$app->request->post())) { |
93
|
|
|
$term->taxonomy_id = $id; |
94
|
|
|
if ($term->save()) { |
95
|
|
|
return $this->redirect(['/taxonomy/view', 'id' => $id]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->render('view', [ |
100
|
|
|
'model' => $this->findModel($id), |
101
|
|
|
'term' => $term, |
102
|
|
|
'searchModel' => $searchModel, |
103
|
|
|
'dataProvider' => $dataProvider, |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Creates a new Taxonomy model. |
109
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
110
|
|
|
* |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function actionCreate() |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$model = new Taxonomy(); |
116
|
|
|
|
117
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
118
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->render('create', [ |
122
|
|
|
'model' => $model, |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Updates an existing Taxonomy model. |
128
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
129
|
|
|
* |
130
|
|
|
* @param integer $id |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function actionUpdate($id) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$model = $this->findModel($id); |
136
|
|
|
|
137
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
|
|
|
|
138
|
|
|
return $this->redirect(['view', 'id' => $model->id]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->render('update', [ |
142
|
|
|
'model' => $model, |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Deletes an existing Taxonomy model. |
148
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
149
|
|
|
* |
150
|
|
|
* @param integer $id |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
public function actionDelete($id) |
154
|
|
|
{ |
155
|
|
|
$this->findModel($id)->delete(); |
|
|
|
|
156
|
|
|
|
157
|
|
|
return $this->redirect(['index']); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Bulk action for Taxonomy triggered when button 'Apply' clicked. |
162
|
|
|
* The action depends on the value of the dropdown next to the button. |
163
|
|
|
* Only accept POST HTTP method. |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public function actionBulkAction() |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
if (Yii::$app->request->post('action') == 'deleted') { |
168
|
|
|
foreach (Yii::$app->request->post('ids', []) as $id) { |
169
|
|
|
$this->findModel($id)->delete(); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Updates an existing Term on 'view taxonomy' page. |
176
|
|
|
|
177
|
|
|
* |
178
|
|
|
*@param $id |
179
|
|
|
* @param $term |
180
|
|
|
* @throws NotFoundHttpException |
181
|
|
|
* @return string|\yii\web\Response |
182
|
|
|
* @see actionView |
183
|
|
|
*/ |
184
|
|
View Code Duplication |
public function actionUpdateTerm($id, $term) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$term = $this->findTerm($term); |
187
|
|
|
$searchModel = new TermSearch(); |
188
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $id); |
189
|
|
|
|
190
|
|
|
if ($term->load(Yii::$app->request->post())) { |
|
|
|
|
191
|
|
|
$term->taxonomy_id = $id; |
192
|
|
|
if ($term->save()) { |
|
|
|
|
193
|
|
|
return $this->redirect(['/taxonomy/view', 'id' => $id]); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $this->render('view', [ |
198
|
|
|
'model' => $this->findModel($id), |
199
|
|
|
'term' => $term, |
200
|
|
|
'searchModel' => $searchModel, |
201
|
|
|
'dataProvider' => $dataProvider, |
202
|
|
|
]); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Delete an existing Term of a taxonomy on 'view taxonomy' page. |
207
|
|
|
* If deletion is successful, the browser will be redirected to the 'view taxonomy' page. |
208
|
|
|
|
209
|
|
|
* |
210
|
|
|
*@param integer $id |
211
|
|
|
* @param integer $term |
212
|
|
|
* @throws \Exception |
213
|
|
|
* @throws \yii\web\NotFoundHttpException |
214
|
|
|
* @return mixed |
215
|
|
|
*/ |
216
|
|
|
public function actionDeleteTerm($id, $term) |
217
|
|
|
{ |
218
|
|
|
$this->findTerm($term)->delete(); |
|
|
|
|
219
|
|
|
|
220
|
|
|
return $this->redirect(['view', 'id' => $id]); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Create taxonomy via AJAX request on 'create' and 'update post type' page. |
225
|
|
|
*/ |
226
|
|
|
public function actionAjaxCreate() |
227
|
|
|
{ |
228
|
|
|
$model = new Taxonomy(); |
229
|
|
|
|
230
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
231
|
|
|
return '<br />' |
232
|
|
|
. Html::label(Html::checkbox('taxonomy_ids[]', true, ['value' => $model->id]) . ' ' . $model->name); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return ''; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Finds the Taxonomy model based on its primary key value. |
240
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
241
|
|
|
* |
242
|
|
|
* @param integer $id |
243
|
|
|
* @return Taxonomy the loaded model |
244
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
245
|
|
|
*/ |
246
|
|
|
protected function findModel($id) |
247
|
|
|
{ |
248
|
|
|
if (($model = Taxonomy::findOne($id)) !== null) { |
249
|
|
|
return $model; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Finds the Term model based on its primary key value. |
257
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
258
|
|
|
* |
259
|
|
|
* @param integer $id |
260
|
|
|
* @return Term the loaded model |
261
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
262
|
|
|
*/ |
263
|
|
|
protected function findTerm($id) |
264
|
|
|
{ |
265
|
|
|
if (($model = Term::findOne($id)) !== null) { |
266
|
|
|
return $model; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
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.