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