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\Post; |
11
|
|
|
use common\models\PostComment; |
12
|
|
|
use common\models\PostType; |
13
|
|
|
use common\models\search\PostComment as PostCommentSearch; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\filters\AccessControl; |
16
|
|
|
use yii\filters\VerbFilter; |
17
|
|
|
use yii\web\Controller; |
18
|
|
|
use yii\web\NotFoundHttpException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* PostCommentController, controlling the actions for PostComment model. |
22
|
|
|
* |
23
|
|
|
* @author Agiel K. Saputra <[email protected]> |
24
|
|
|
* @since 0.1.0 |
25
|
|
|
*/ |
26
|
|
|
class PostCommentController 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' => ['index', 'update', 'delete', 'bulk-action', 'reply'], |
39
|
|
|
'allow' => true, |
40
|
|
|
'roles' => ['editor'], |
41
|
|
|
], |
42
|
|
|
], |
43
|
|
|
], |
44
|
|
|
'verbs' => [ |
45
|
|
|
'class' => VerbFilter::className(), |
46
|
|
|
'actions' => [ |
47
|
|
|
'delete' => ['post'], |
48
|
|
|
'bulk-action' => ['post'], |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Lists all PostComment models on specific post type. |
57
|
|
|
* If there is post_id the action will generate list of all PostComment models based on post_id. |
58
|
|
|
* |
59
|
|
|
* @param integer $posttype Post type ID |
60
|
|
|
* @param null|integer $post Post ID |
61
|
|
|
* @throws \yii\web\NotFoundHttpException |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function actionIndex($posttype, $post = null) |
65
|
|
|
{ |
66
|
|
|
$postId = null; |
67
|
|
|
$postType = $this->findPostType($posttype); |
68
|
|
|
|
69
|
|
|
if ($post) { |
|
|
|
|
70
|
|
|
$post = $this->findPost($post); |
71
|
|
|
$postId = $post->id; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$searchModel = new PostCommentSearch(); |
75
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $posttype, $postId); |
76
|
|
|
|
77
|
|
|
return $this->render('index', [ |
78
|
|
|
'post' => $post, |
79
|
|
|
'postType' => $postType, |
80
|
|
|
'searchModel' => $searchModel, |
81
|
|
|
'dataProvider' => $dataProvider, |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Updates an existing PostComment model. |
88
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
89
|
|
|
* |
90
|
|
|
* @param integer $id |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function actionUpdate($id) |
94
|
|
|
{ |
95
|
|
|
$model = $this->findModel($id); |
96
|
|
|
|
97
|
|
|
if ($model->load(Yii::$app->request->post())) { |
|
|
|
|
98
|
|
|
$model->date = date('Y-m-d H:i:s', strtotime($model->date)); |
99
|
|
|
if ($model->save()) { |
|
|
|
|
100
|
|
|
return $this->redirect(['update', 'id' => $model->id]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->render('update', [ |
105
|
|
|
'model' => $model, |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Deletes an existing PostComment model. |
111
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
112
|
|
|
* |
113
|
|
|
* @param integer $id |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function actionDelete($id) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$model = $this->findModel($id); |
119
|
|
|
$post = $model->commentPost; |
120
|
|
|
|
121
|
|
|
if ($model->delete()) { |
|
|
|
|
122
|
|
|
if (!$model->parent) { |
123
|
|
|
$post->updateAttributes(['comment_count', --$post->comment_count]); |
124
|
|
|
} |
125
|
|
|
PostComment::deleteAll(['parent' => $model->id]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->redirect(['index', 'posttype' => $post->type]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Bulk action for PostComment triggered when button 'Apply' clicked. |
133
|
|
|
* The action depends on the value of the dropdown next to the button. |
134
|
|
|
* Only accept POST HTTP method. |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function actionBulkAction() |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
if (Yii::$app->request->post('action') === PostComment::STATUS_APPROVED) { |
139
|
|
|
foreach (Yii::$app->request->post('ids', []) as $id) { |
140
|
|
|
$this->findModel($id)->updateAttributes(['status' => PostComment::STATUS_APPROVED]); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
} elseif (Yii::$app->request->post('action') === PostComment::STATUS_NOT_APPROVED) { |
143
|
|
|
foreach (Yii::$app->request->post('ids', []) as $id) { |
144
|
|
|
$this->findModel($id)->updateAttributes(['status' => PostComment::STATUS_NOT_APPROVED]); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} elseif (Yii::$app->request->post('action') === PostComment::STATUS_TRASHED) { |
147
|
|
|
foreach (Yii::$app->request->post('ids', []) as $id) { |
148
|
|
|
$this->findModel($id)->updateAttributes(['status' => PostComment::STATUS_TRASHED]); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
} elseif (Yii::$app->request->post('action') === 'delete') { |
151
|
|
|
foreach (Yii::$app->request->post('ids', []) as $id) { |
152
|
|
|
$model = $this->findModel($id); |
153
|
|
|
$post = $model->commentPost; |
154
|
|
|
if ($model->delete()) { |
|
|
|
|
155
|
|
|
if (!$model->parent) { |
156
|
|
|
$post->updateAttributes(['comment_count', --$post->comment_count]); |
157
|
|
|
} |
158
|
|
|
PostComment::deleteAll(['parent' => $model->id]); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Reply an existing PostComment model. |
166
|
|
|
* If reply is successful, the browser will be redirected to 'update' page. |
167
|
|
|
* |
168
|
|
|
* @param int $id Find PostComment model based on id as parent. |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function actionReply($id) |
172
|
|
|
{ |
173
|
|
|
$commentParent = $this->findModel($id); |
174
|
|
|
$model = new PostComment(['scenario' => 'reply']); |
175
|
|
|
|
176
|
|
|
if ($model->load(Yii::$app->request->post())) { |
177
|
|
|
$model->post_id = $commentParent->post_id; |
178
|
|
|
$model->parent = $commentParent->id; |
179
|
|
|
if ($model->save()) { |
180
|
|
|
$this->redirect(['post-comment/update', 'id' => $model->id]); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this->render('reply', [ |
185
|
|
|
'commentParent' => $commentParent, |
186
|
|
|
'model' => $model, |
187
|
|
|
]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Finds the PostComment model based on its primary key value. |
192
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
193
|
|
|
* |
194
|
|
|
* @param integer $id |
195
|
|
|
* @return PostComment the loaded model |
196
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
197
|
|
|
*/ |
198
|
|
|
protected function findModel($id) |
199
|
|
|
{ |
200
|
|
|
if (($model = PostComment::findOne($id)) !== null) { |
201
|
|
|
return $model; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Finds the PostType model based on its primary key value. |
209
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
210
|
|
|
* |
211
|
|
|
* @param integer $id |
212
|
|
|
* @return PostType the loaded model |
213
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
214
|
|
|
*/ |
215
|
|
|
protected function findPostType($id) |
216
|
|
|
{ |
217
|
|
|
if (($model = PostType::findOne($id)) !== null) { |
218
|
|
|
return $model; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Finds the Post model based on its primary key value. |
227
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
228
|
|
|
* |
229
|
|
|
* @param integer $id |
230
|
|
|
* @return Post the loaded model |
231
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
232
|
|
|
*/ |
233
|
|
|
protected function findPost($id) |
234
|
|
|
{ |
235
|
|
|
if (($model = Post::findOne($id)) !== null) { |
236
|
|
|
return $model; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
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.