1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\review\controllers; |
4
|
|
|
|
5
|
|
|
use app\models\Object; |
6
|
|
|
use app\modules\page\models\Page; |
7
|
|
|
use app\modules\review\models\Review; |
8
|
|
|
use app\modules\shop\models\Category; |
9
|
|
|
use app\modules\shop\models\Product; |
10
|
|
|
use yii\db\ActiveRecord; |
11
|
|
|
use yii\filters\AccessControl; |
12
|
|
|
use yii\helpers\Url; |
13
|
|
|
use yii\web\BadRequestHttpException; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\web\NotFoundHttpException; |
16
|
|
|
use app\components\SearchModel; |
17
|
|
|
use app\models\Submission; |
18
|
|
|
use yii\web\Response; |
19
|
|
|
|
20
|
|
|
class BackendReviewController extends \app\backend\components\BackendController |
21
|
|
|
{ |
22
|
|
|
public function behaviors() |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
|
|
'access' => [ |
26
|
|
|
'class' => AccessControl::className(), |
27
|
|
|
'rules' => [ |
28
|
|
|
[ |
29
|
|
|
'allow' => true, |
30
|
|
|
'roles' => ['review manage'], |
31
|
|
|
], |
32
|
|
|
], |
33
|
|
|
], |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function actionIndex() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$searchModelConfig = [ |
40
|
|
|
'defaultOrder' => ['id' => SORT_DESC], |
41
|
|
|
'model' => Review::className(), |
42
|
|
|
'relations' => [ |
43
|
|
|
'submission.form' => ['name'], |
44
|
|
|
], |
45
|
|
|
'additionalConditions' => [ |
46
|
|
|
['parent_id' => 0], |
47
|
|
|
] |
48
|
|
|
]; |
49
|
|
|
$searchModel = new SearchModel($searchModelConfig); |
50
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
51
|
|
|
return $this->render( |
52
|
|
|
'index', |
53
|
|
|
[ |
54
|
|
|
'dataProvider' => $dataProvider, |
55
|
|
|
'searchModel' => $searchModel, |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function actionView($id) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$model = Review::find() |
63
|
|
|
->with(['submission']) |
64
|
|
|
->where(['id' => $id]) |
65
|
|
|
->one(); |
66
|
|
|
if (null === $model) { |
67
|
|
|
throw new NotFoundHttpException; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
if (true === Yii::$app->request->isPost) { |
71
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
72
|
|
|
return $this->redirect(Url::toRoute(['view', 'id' => $model->id])); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->render('edit', [ |
77
|
|
|
'review' => $model |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function actionUpdateStatus($id = null) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
if (null === $id) { |
84
|
|
|
$id = \Yii::$app->request->post('editableKey'); |
85
|
|
|
$index = \Yii::$app->request->post('editableIndex'); |
86
|
|
|
if (null === $id || null === $index) { |
87
|
|
|
throw new BadRequestHttpException; |
88
|
|
|
} else { |
89
|
|
|
$review = $this->loadModel($id); |
90
|
|
|
$reviews = \Yii::$app->request->post('Review', []); |
91
|
|
|
$review->status = $reviews[$index]['status']; |
92
|
|
|
return $review->update(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
$reviews = $reviews = \Yii::$app->request->post('Review'); |
96
|
|
|
$status = $reviews['status']; |
97
|
|
|
$review = $this->loadModel($id); |
98
|
|
|
$review->status = $status; |
99
|
|
|
if ($review->update()) { |
|
|
|
|
100
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Review successfully updated')); |
101
|
|
|
} else { |
102
|
|
|
Yii::$app->session->setFlash('error', Yii::t('app', 'Error occurred while updating review')); |
103
|
|
|
} |
104
|
|
|
return $this->redirect( |
105
|
|
|
Url::toRoute( |
106
|
|
|
[ |
107
|
|
|
'view', |
108
|
|
|
'id' => $review->id |
109
|
|
|
] |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function actionMarkSpam($id, $spam = 1) |
116
|
|
|
{ |
117
|
|
|
if ($spam === 1) { |
118
|
|
|
$message = Yii::t('app', 'Entry successfully marked as spam'); |
119
|
|
|
} else { |
120
|
|
|
$message = Yii::t('app', 'Entry successfully marked as not spam'); |
121
|
|
|
} |
122
|
|
|
/** @var Submission $submission */ |
123
|
|
|
$submission = Submission::findOne($id); |
124
|
|
|
if (is_null($submission)) { |
125
|
|
|
throw new NotFoundHttpException; |
126
|
|
|
} |
127
|
|
|
$submission->spam = $spam; |
128
|
|
|
if ($spam == 1) { |
129
|
|
|
/** @var Review $review */ |
130
|
|
|
$review = Review::findOne(['submission_id' => $id]); |
131
|
|
|
if (!is_null($review)) { |
132
|
|
|
$review->status = Review::STATUS_NOT_APPROVED; |
133
|
|
|
$review->save(true, ['status']); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
if ($submission->save(true, ['spam'])) { |
137
|
|
|
Yii::$app->session->setFlash('success', $message); |
138
|
|
|
} |
139
|
|
|
return $this->redirect( |
140
|
|
|
Url::toRoute( |
141
|
|
|
[ |
142
|
|
|
'view', |
143
|
|
|
'id' => $id |
144
|
|
|
] |
145
|
|
|
) |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $id |
151
|
|
|
* @param null $returnUrl |
152
|
|
|
* @return Response |
153
|
|
|
* @throws NotFoundHttpException |
154
|
|
|
* @throws \Exception |
155
|
|
|
*/ |
156
|
|
|
public function actionDelete($id, $returnUrl = null) |
157
|
|
|
{ |
158
|
|
|
$model = $this->loadModel($id); |
159
|
|
|
$parent_id = $model->parent_id; |
160
|
|
|
if ($model->delete()) { |
|
|
|
|
161
|
|
|
Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed')); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$returnUrl = !empty($returnUrl) |
165
|
|
|
? $returnUrl |
166
|
|
|
: (0 === intval($parent_id) ? Url::toRoute(['index']) : Url::toRoute(['view', 'id' => $parent_id])); |
167
|
|
|
return $this->redirect($returnUrl); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param array $returnUrl |
172
|
|
|
* @return Response |
173
|
|
|
* @throws \Exception |
174
|
|
|
*/ |
175
|
|
|
public function actionRemoveAll($returnUrl = ['index']) |
176
|
|
|
{ |
177
|
|
|
$items = Yii::$app->request->post('items', []); |
178
|
|
|
if (!empty($items)) { |
179
|
|
|
$items = Review::findAll(['id' => $items]); |
180
|
|
|
foreach ($items as $item) { |
181
|
|
|
$item->delete(); |
182
|
|
|
} |
183
|
|
|
Yii::$app->session->setFlash('info', Yii::t('app', 'Objects removed')); |
184
|
|
|
} |
185
|
|
|
return $this->redirect($returnUrl); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Load review model by id |
190
|
|
|
* @param $id |
191
|
|
|
* @return Review |
|
|
|
|
192
|
|
|
* @throws NotFoundHttpException |
193
|
|
|
*/ |
194
|
|
|
protected function loadModel($id) |
195
|
|
|
{ |
196
|
|
|
$model = Review::findOne($id); |
197
|
|
|
if (is_null($model)) { |
198
|
|
|
throw new NotFoundHttpException; |
199
|
|
|
} |
200
|
|
|
return $model; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function actionCreate($parent_id = 0) |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
$parent_id = intval($parent_id); |
206
|
|
|
$model = new Review(); |
207
|
|
|
$model->loadDefaultValues(); |
208
|
|
|
|
209
|
|
|
if (0 === $parent_id) { |
210
|
|
|
$model->parent_id = $parent_id; |
211
|
|
|
$model->submission_id = 0; |
212
|
|
|
$model->object_model_id = 0; |
213
|
|
|
$model->object_id = 0; |
214
|
|
|
} elseif (null !== $parent = Review::findOne(['id' => $parent_id])) { |
215
|
|
|
/** @var Review $parent */ |
216
|
|
|
$model->parent_id = $parent_id; |
217
|
|
|
$model->object_id = $parent->object_id; |
218
|
|
|
$model->object_model_id = $parent->object_model_id; |
219
|
|
|
$model->root_id = $parent->root_id; |
220
|
|
|
$model->submission_id = $parent->submission_id; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
View Code Duplication |
if (true === Yii::$app->request->isPost) { |
224
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
225
|
|
|
return $this->redirect(Url::toRoute(['view', 'id' => $model->id])); |
|
|
|
|
226
|
|
|
} else { |
|
|
|
|
227
|
|
|
// @todo add alert and may be something else here |
|
|
|
|
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $this->render('edit', [ |
232
|
|
|
'review' => $model |
233
|
|
|
]); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return array |
|
|
|
|
238
|
|
|
*/ |
239
|
|
|
public function actionAjaxSearch() |
240
|
|
|
{ |
241
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
242
|
|
|
$search = \Yii::$app->request->get('search', []); |
243
|
|
|
$object = !empty($search['object']) ? intval($search['object']) : 0; |
244
|
|
|
$term = !empty($search['term']) ? $search['term'] : ''; |
245
|
|
|
|
246
|
|
|
$result = [ |
247
|
|
|
'more' => false, |
248
|
|
|
'results' => [] |
249
|
|
|
]; |
250
|
|
|
|
251
|
|
|
if (null === $object = Object::findById($object)) { |
252
|
|
|
return $result; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** @var ActiveRecord $class */ |
256
|
|
|
$class = $object->object_class; |
257
|
|
|
$list = [ |
258
|
|
|
Product::className(), |
259
|
|
|
Category::className(), |
260
|
|
|
Page::className(), |
261
|
|
|
]; |
262
|
|
|
if (!in_array($class, $list)) { |
263
|
|
|
return $result; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$query = $class::find() |
267
|
|
|
->select('id, name, "#" `url`') |
268
|
|
|
->andWhere(['like', 'name', $term]) |
269
|
|
|
->asArray(true); |
270
|
|
|
$result['results'] = array_values($query->all()); |
271
|
|
|
array_walk($result['results'], |
272
|
|
|
function (&$val) use ($class) |
273
|
|
|
{ |
274
|
|
|
if (null !== $model = $class::findOne(['id' => $val['id']])) { |
275
|
|
|
if (Product::className() === $model->className()) { |
|
|
|
|
276
|
|
|
$val['url'] = Url::toRoute([ |
277
|
|
|
'@product', |
278
|
|
|
'model' => $model, |
279
|
|
|
'category_group_id' => $model->category->category_group_id, |
280
|
|
|
]); |
281
|
|
|
} elseif (Category::className() === $model->className()) { |
|
|
|
|
282
|
|
|
$val['url'] = Url::toRoute([ |
283
|
|
|
'@category', |
284
|
|
|
'last_category_id' => $model->id, |
285
|
|
|
'category_group_id' => $model->category_group_id, |
286
|
|
|
]); |
287
|
|
|
} else if (Page::className() === $model->className()) { |
|
|
|
|
288
|
|
|
$val['url'] = Url::toRoute([ |
289
|
|
|
'@article', |
290
|
|
|
'id' => $model->id, |
291
|
|
|
]); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
}); |
295
|
|
|
|
296
|
|
|
return $result; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return array |
301
|
|
|
*/ |
302
|
|
|
public function actionAjaxGetTree($root_id = null, $current_id = 0) |
303
|
|
|
{ |
304
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
305
|
|
|
|
306
|
|
|
$q = Review::find() |
307
|
|
|
->select('*') |
308
|
|
|
->where(['root_id' => $root_id]) |
309
|
|
|
->orderBy(['parent_id' => SORT_ASC]) |
310
|
|
|
->asArray(true); |
311
|
|
|
|
312
|
|
|
$result = array_reduce($q->all(), |
313
|
|
|
function ($res, $item) use ($current_id) |
314
|
|
|
{ |
315
|
|
|
$res[] = [ |
316
|
|
|
'id' => $item['id'], |
317
|
|
|
'parent' => 0 === intval($item['parent_id']) ? '#' : $item['parent_id'], |
318
|
|
|
'text' => $item['id'], |
319
|
|
|
'type' => intval($item['id']) === intval($current_id) ? 'current' : 'leaf', |
320
|
|
|
'a_attr' => [ |
321
|
|
|
'data-id' => $item['id'], |
322
|
|
|
] |
323
|
|
|
]; |
324
|
|
|
return $res; |
325
|
|
|
}, []); |
326
|
|
|
|
327
|
|
|
return $result; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.