|
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\Term; |
|
11
|
|
|
use common\models\TermRelationship; |
|
12
|
|
|
use Yii; |
|
13
|
|
|
use yii\filters\AccessControl; |
|
14
|
|
|
use yii\filters\VerbFilter; |
|
15
|
|
|
use yii\helpers\Html; |
|
16
|
|
|
use yii\web\Controller; |
|
17
|
|
|
use yii\web\NotFoundHttpException; |
|
18
|
|
|
use yii\web\Response; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* TermController implements the CRUD actions for Term model. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Agiel K. Saputra <[email protected]> |
|
24
|
|
|
* @since 0.1.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class TermController extends Controller |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritdoc |
|
30
|
|
|
*/ |
|
31
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
return [ |
|
34
|
|
|
'access' => [ |
|
35
|
|
|
'class' => AccessControl::className(), |
|
36
|
|
|
'rules' => [ |
|
37
|
|
|
[ |
|
38
|
|
|
'actions' => ['ajax-create-hierarchical', 'ajax-create-non-hierarchical', 'bulk-action'], |
|
39
|
|
|
'allow' => true, |
|
40
|
|
|
'roles' => ['editor'], |
|
41
|
|
|
], |
|
42
|
|
|
[ |
|
43
|
|
|
'actions' => ['ajax-search'], |
|
44
|
|
|
'allow' => true, |
|
45
|
|
|
'roles' => ['subscriber'], |
|
46
|
|
|
], |
|
47
|
|
|
], |
|
48
|
|
|
], |
|
49
|
|
|
'verbs' => [ |
|
50
|
|
|
'class' => VerbFilter::className(), |
|
51
|
|
|
'actions' => [ |
|
52
|
|
|
'ajax-create-hierarchical' => ['post'], |
|
53
|
|
|
'ajax-create-non-hierarchical' => ['post'], |
|
54
|
|
|
'bulk-action' => ['post'], |
|
55
|
|
|
'ajax-search' => ['post'], |
|
56
|
|
|
], |
|
57
|
|
|
], |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create a new Term model for hierarchical Taxonomy through AJAX request. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function actionAjaxCreateHierarchical() |
|
65
|
|
|
{ |
|
66
|
|
|
$item = ''; |
|
67
|
|
|
$term = new Term(); |
|
68
|
|
|
$termRelationship = new TermRelationship(); |
|
69
|
|
|
|
|
70
|
|
|
if ($term->load(Yii::$app->request->post())) { |
|
71
|
|
|
if ($termRelationship->load(Yii::$app->request->post()) && $termRelationship->post_id) { |
|
72
|
|
|
$term->count = 1; |
|
73
|
|
|
if ($term->save()) { |
|
74
|
|
|
$termRelationship->term_id = $term->id; |
|
75
|
|
View Code Duplication |
if ($termRelationship->save()) { |
|
|
|
|
|
|
76
|
|
|
$item = $item |
|
77
|
|
|
. '<br />' |
|
78
|
|
|
. Html::label(Html::checkbox('termIds[]', true, ['value' => $term->id]) . $term->name); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
View Code Duplication |
} elseif ($term->save()) { |
|
|
|
|
|
|
82
|
|
|
$item = $item |
|
83
|
|
|
. '<br />' |
|
84
|
|
|
. Html::label(Html::checkbox('termIds[]', true, ['value' => $term->id]) . $term->name); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $item; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Create a new Term for non-hierarchical Taxonomy through Selectize box. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function actionAjaxCreateNonHierarchical() |
|
97
|
|
|
{ |
|
98
|
|
|
$model = new Term(); |
|
99
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
|
100
|
|
|
|
|
101
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
|
102
|
|
|
return ['id' => $model->id, 'name' => $model->name]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return []; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Search Term through Ajax with JSON as response. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function actionAjaxSearch() |
|
114
|
|
|
{ |
|
115
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
|
116
|
|
|
|
|
117
|
|
|
if ($term = Yii::$app->request->post('Term')) { |
|
118
|
|
|
$model = Term::find() |
|
119
|
|
|
->select(['id', 'name']) |
|
120
|
|
|
->where(['like', 'name', $term['name']]) |
|
121
|
|
|
->andWhere(['taxonomy_id' => $term['taxonomy_id']]) |
|
122
|
|
|
->limit('10') |
|
123
|
|
|
->all(); |
|
124
|
|
|
if ($model) { |
|
|
|
|
|
|
125
|
|
|
return ($model); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return []; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Bulk action for Term triggered when button 'Apply' clicked. |
|
134
|
|
|
* The action depends on the value of the dropdown next to the button. |
|
135
|
|
|
* Only accept POST HTTP method. |
|
136
|
|
|
*/ |
|
137
|
|
View Code Duplication |
public function actionBulkAction() |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
if (Yii::$app->request->post('action') == 'deleted') { |
|
140
|
|
|
foreach (Yii::$app->request->post('ids', []) as $id) { |
|
141
|
|
|
$this->findModel($id)->delete(); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Finds the Term model based on its primary key value. |
|
148
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
|
149
|
|
|
* |
|
150
|
|
|
* @param integer $id |
|
151
|
|
|
* @return Term the loaded model |
|
152
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function findModel($id) |
|
155
|
|
|
{ |
|
156
|
|
|
if (($model = Term::findOne($id)) !== null) { |
|
157
|
|
|
return $model; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
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.