|
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\web\Controller; |
|
16
|
|
|
use yii\web\NotFoundHttpException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* TermRelationshipController implements the CRUD actions for TermRelationship model. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Agiel K. Saputra <[email protected]> |
|
22
|
|
|
* @since 0.1.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class TermRelationshipController extends Controller |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
'access' => [ |
|
33
|
|
|
'class' => AccessControl::className(), |
|
34
|
|
|
'rules' => [ |
|
35
|
|
|
[ |
|
36
|
|
|
'actions' => [ |
|
37
|
|
|
'ajax-change-hierarchical', |
|
38
|
|
|
'ajax-create-non-hierarchical', |
|
39
|
|
|
'ajax-delete-non-hierarchical', |
|
40
|
|
|
], |
|
41
|
|
|
'allow' => true, |
|
42
|
|
|
'roles' => ['subscriber'], |
|
43
|
|
|
], |
|
44
|
|
|
], |
|
45
|
|
|
], |
|
46
|
|
|
'verbs' => [ |
|
47
|
|
|
'class' => VerbFilter::className(), |
|
48
|
|
|
'actions' => [ |
|
49
|
|
|
'ajax-change-hierarchical' => ['post'], |
|
50
|
|
|
'ajax-create-non-hierarchical' => ['post'], |
|
51
|
|
|
'ajax-delete-non-hierarchical' => ['post'], |
|
52
|
|
|
], |
|
53
|
|
|
], |
|
54
|
|
|
|
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* If user change the value of checkbox when updating a post, this action will be triggered. |
|
60
|
|
|
* If the checkbox checked it will trigger addItem (create TermRelationship) while if unchecked will trigger |
|
61
|
|
|
* remItem (remove TermRelationship). |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \Exception |
|
64
|
|
|
* @throws \yii\web\NotFoundHttpException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function actionAjaxChangeHierarchical() |
|
67
|
|
|
{ |
|
68
|
|
|
if (Yii::$app->request->post('action') === 'addItem') { |
|
69
|
|
|
$model = new TermRelationship(); |
|
70
|
|
|
$model->load(Yii::$app->request->post()); |
|
71
|
|
View Code Duplication |
if ($model->save()) { |
|
|
|
|
|
|
72
|
|
|
if ($term = $this->findTerm($model->term_id)) { |
|
73
|
|
|
$term->updateAttributes(['count' => ++$term->count]); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} elseif (Yii::$app->request->post('action') === 'remItem' |
|
77
|
|
|
&& $termRelationship = Yii::$app->request->post('TermRelationship') |
|
78
|
|
|
) { |
|
79
|
|
|
$model = $this->findModel($termRelationship['post_id'], $termRelationship['term_id']); |
|
80
|
|
View Code Duplication |
if ($model->delete()) { |
|
|
|
|
|
|
81
|
|
|
if ($term = $this->findTerm($model->term_id)) { |
|
82
|
|
|
$term->updateAttributes(['count' => --$term->count]); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Create new TermRelationship model through AJAX via Seletize box on 'create' and 'update post' page. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function actionAjaxCreateNonHierarchical() |
|
92
|
|
|
{ |
|
93
|
|
|
$model = new TermRelationship(); |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
|
|
|
|
|
|
96
|
|
|
if ($term = $this->findTerm($model->term_id)) { |
|
97
|
|
|
$term->updateAttributes(['count' => ++$term->count]); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Delete TermRelationship for non-hierarchical Taxonomy |
|
104
|
|
|
* which is triggered when the user remove item in Selectize box. |
|
105
|
|
|
* |
|
106
|
|
|
* @throws \Exception |
|
107
|
|
|
* @throws \yii\web\NotFoundHttpException |
|
108
|
|
|
*/ |
|
109
|
|
|
public function actionAjaxDeleteNonHierarchical() |
|
110
|
|
|
{ |
|
111
|
|
|
if ($termRelationship = Yii::$app->request->post('TermRelationship')) { |
|
112
|
|
|
$model = $this->findModel($termRelationship['post_id'], $termRelationship['term_id']); |
|
113
|
|
View Code Duplication |
if ($model->delete()) { |
|
|
|
|
|
|
114
|
|
|
if ($term = $this->findTerm($model->term_id)) { |
|
115
|
|
|
$term->updateAttributes(['count' => --$term->count]); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Finds the TermRelationship model based on its primary key value. |
|
123
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
|
124
|
|
|
* |
|
125
|
|
|
* @param integer $post_id |
|
126
|
|
|
* @param integer $term_id |
|
127
|
|
|
* @return TermRelationship the loaded model |
|
128
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function findModel($post_id, $term_id) |
|
131
|
|
|
{ |
|
132
|
|
|
if (($model = TermRelationship::findOne(['post_id' => $post_id, 'term_id' => $term_id])) !== null) { |
|
133
|
|
|
return $model; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Finds the Term model based on its primary key value. |
|
141
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
|
142
|
|
|
* |
|
143
|
|
|
* @param integer $id |
|
144
|
|
|
* @return Term|false the loaded model |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function findTerm($id) |
|
147
|
|
|
{ |
|
148
|
|
|
if ($model = Term::findOne($id)) { |
|
149
|
|
|
return $model; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
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.