|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\review\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use app\actions\SubmitFormAction; |
|
6
|
|
|
use app\behaviors\spamchecker\SpamCheckerBehavior; |
|
7
|
|
|
use app\components\Controller; |
|
8
|
|
|
use app\models\SpamChecker; |
|
9
|
|
|
use app\models\Submission; |
|
10
|
|
|
use app\modules\review\models\RatingItem; |
|
11
|
|
|
use app\modules\review\models\RatingValues; |
|
12
|
|
|
use app\modules\review\models\Review; |
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\helpers\Json; |
|
15
|
|
|
use yii\web\HttpException; |
|
16
|
|
|
|
|
17
|
|
|
class ProcessController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
public function actions() |
|
20
|
|
|
{ |
|
21
|
|
|
return [ |
|
22
|
|
|
'submission' => [ |
|
23
|
|
|
'class' => SubmitFormAction::className(), |
|
|
|
|
|
|
24
|
|
|
], |
|
25
|
|
|
]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $id |
|
30
|
|
|
* @param $objectModelId |
|
31
|
|
|
* @param $objectId |
|
32
|
|
|
* @param string $returnUrl |
|
33
|
|
|
* @return \yii\web\Response |
|
34
|
|
|
* @throws HttpException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function actionProcess($id, $objectModelId, $objectId, $returnUrl = '/') |
|
37
|
|
|
{ |
|
38
|
|
|
if (false === Yii::$app->request->isPost) { |
|
39
|
|
|
throw new HttpException(403); |
|
40
|
|
|
} |
|
41
|
|
|
/** @var $review \app\modules\review\models\Review|SpamCheckerBehavior */ |
|
42
|
|
|
$post = Yii::$app->request->post(); |
|
43
|
|
|
$review = new Review(['scenario' => 'check']); |
|
44
|
|
|
$review->load($post); |
|
45
|
|
|
if (!Yii::$app->user->isGuest) { |
|
46
|
|
|
$review->author_email = Yii::$app->user->identity->email; |
|
47
|
|
|
} |
|
48
|
|
|
$review->object_id = $objectId; |
|
49
|
|
|
$review->object_model_id = $objectModelId; |
|
50
|
|
|
if ($review->validate()) { |
|
51
|
|
|
$submission_id = Yii::$app->runAction('review/process/submission', ['id' => $id]); |
|
52
|
|
|
if ($submission_id == "0") { |
|
53
|
|
|
Yii::$app->session->setFlash( |
|
54
|
|
|
'info', |
|
55
|
|
|
Yii::t('app', 'Error occurred while saving review. Sorry. Try again later') |
|
56
|
|
|
); |
|
57
|
|
|
return $this->redirect($returnUrl); |
|
58
|
|
|
} |
|
59
|
|
|
$review->submission_id = $submission_id; |
|
60
|
|
|
$review->status = Review::STATUS_NEW; |
|
61
|
|
|
if ($this->module->enableSpamChecking) { |
|
62
|
|
|
$activeSpamChecker = SpamChecker::getActive(); |
|
63
|
|
|
if (!is_null($activeSpamChecker) && !empty($activeSpamChecker->api_key)) { |
|
64
|
|
|
$review->attachBehavior( |
|
65
|
|
|
'spamChecker', |
|
66
|
|
|
[ |
|
67
|
|
|
'class' => SpamCheckerBehavior::className(), |
|
|
|
|
|
|
68
|
|
|
'data' => [ |
|
69
|
|
|
$activeSpamChecker->name => [ |
|
70
|
|
|
'class' => $activeSpamChecker->behavior, |
|
71
|
|
|
'value' => [ |
|
72
|
|
|
'key' => $activeSpamChecker->api_key, |
|
73
|
|
|
SpamChecker::FIELD_TYPE_CONTENT => $review->review_text |
|
74
|
|
|
], |
|
75
|
|
|
], |
|
76
|
|
|
], |
|
77
|
|
|
] |
|
78
|
|
|
); |
|
79
|
|
|
if ($review->isSpam()) { |
|
|
|
|
|
|
80
|
|
|
$review->status = Review::STATUS_NOT_APPROVED; |
|
81
|
|
|
Submission::updateAll(['spam' => 1], ['id' => $submission_id]); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
if ($review->save()) { |
|
86
|
|
|
$ratingData = isset($post['ObjectRating']) ? $post['ObjectRating'] : null; |
|
87
|
|
|
if (null !== $ratingData) { |
|
88
|
|
|
$group = isset($ratingData['group']) ? trim($ratingData['group']) : null; |
|
89
|
|
|
$group = RatingItem::getGroupByName($group); |
|
|
|
|
|
|
90
|
|
|
$items = []; |
|
91
|
|
|
if (!empty($ratingData['values']) && !empty($group)) { |
|
92
|
|
|
$user_id = \Yii::$app->getUser()->isGuest ? 0 : \Yii::$app->user->identity->getId(); |
|
|
|
|
|
|
93
|
|
|
$ratingId = md5(Json::encode(array_merge($ratingData['values'], [microtime(), $user_id]))); |
|
94
|
|
|
$date = date('Y-m-d H:m:s'); |
|
95
|
|
|
if ((0 == $group['require_review']) || ((0 != $group['require_review']))) { |
|
96
|
|
|
$items = RatingItem::getItemsByAttributes( |
|
97
|
|
|
['rating_group' => $group['rating_group']], |
|
98
|
|
|
true, |
|
99
|
|
|
true |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
if (!empty($items)) { |
|
103
|
|
|
foreach ($items as $key => $item) { |
|
104
|
|
|
$model = new RatingValues(); |
|
105
|
|
|
$model->loadDefaultValues(); |
|
106
|
|
|
$model->object_id = $objectId; |
|
107
|
|
|
$model->object_model_id = $objectModelId; |
|
108
|
|
|
$model->rating_item_id = $item['id']; |
|
109
|
|
|
$model->value = isset($ratingData['values'][$item['id']]) |
|
110
|
|
|
? intval($ratingData['values'][$item['id']]) |
|
111
|
|
|
: 0; |
|
112
|
|
|
$model->rating_id = $ratingId; |
|
113
|
|
|
$model->date = $date; |
|
114
|
|
|
$model->save(); |
|
115
|
|
|
} |
|
116
|
|
|
if (isset($review)) { |
|
117
|
|
|
$review->rating_id = $ratingId; |
|
118
|
|
|
$review->save(true, ['rating_id']); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
Yii::$app->session->setFlash( |
|
124
|
|
|
'info', |
|
125
|
|
|
Yii::t('app', 'Your review will appear on the website immediately after moderation') |
|
126
|
|
|
); |
|
127
|
|
|
return $this->redirect($returnUrl); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
return $this->redirect($returnUrl); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.