public function actionCreate($parent_id = 0) { $parent_id = intval($parent_id); $model = new Review(); $model->loadDefaultValues(); if (0 === $parent_id) { $model->parent_id = $parent_id; $model->submission_id = 0; $model->object_model_id = 0; $model->object_id = 0; } elseif (null !== ($parent = Review::findOne(['id' => $parent_id]))) { /** @var Review $parent */ $model->parent_id = $parent_id; $model->object_id = $parent->object_id; $model->object_model_id = $parent->object_model_id; $model->root_id = $parent->root_id; $model->submission_id = $parent->submission_id; } if (true === Yii::$app->request->isPost) { if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(Url::toRoute(['view', 'id' => $model->id])); } else { // @todo add alert and may be something else here } } return $this->render('edit', ['review' => $model]); }
/** * @param $id * @param $objectModelId * @param $objectId * @param string $returnUrl * @return \yii\web\Response * @throws HttpException */ public function actionProcess($id, $objectModelId, $objectId, $returnUrl = '/') { if (false === Yii::$app->request->isPost) { throw new HttpException(403); } /** @var $review \app\modules\review\models\Review|SpamCheckerBehavior */ $post = Yii::$app->request->post(); $review = new Review(['scenario' => 'check']); $review->load($post); if (!Yii::$app->user->isGuest) { $review->author_email = Yii::$app->user->identity->email; } $review->object_id = $objectId; $review->object_model_id = $objectModelId; if ($review->validate()) { $submission_id = Yii::$app->runAction('review/process/submission', ['id' => $id]); if ($submission_id == "0") { Yii::$app->session->setFlash('info', Yii::t('app', 'Error occurred while saving review. Sorry. Try again later')); return $this->redirect($returnUrl); } $review->submission_id = $submission_id; $review->status = Review::STATUS_NEW; if ($this->module->enableSpamChecking) { $activeSpamChecker = SpamChecker::getActive(); if (!is_null($activeSpamChecker) && !empty($activeSpamChecker->api_key)) { $review->attachBehavior('spamChecker', ['class' => SpamCheckerBehavior::className(), 'data' => [$activeSpamChecker->name => ['class' => $activeSpamChecker->behavior, 'value' => ['key' => $activeSpamChecker->api_key, SpamChecker::FIELD_TYPE_CONTENT => $review->review_text]]]]); if ($review->isSpam()) { $review->status = Review::STATUS_NOT_APPROVED; Submission::updateAll(['spam' => 1], ['id' => $submission_id]); } } } if ($review->save()) { $ratingData = isset($post['ObjectRating']) ? $post['ObjectRating'] : null; if (null !== $ratingData) { $group = isset($ratingData['group']) ? trim($ratingData['group']) : null; $group = RatingItem::getGroupByName($group); $items = []; if (!empty($ratingData['values']) && !empty($group)) { $user_id = \Yii::$app->getUser()->isGuest ? 0 : \Yii::$app->user->identity->getId(); $ratingId = md5(Json::encode(array_merge($ratingData['values'], [microtime(), $user_id]))); $date = date('Y-m-d H:m:s'); if (0 == $group['require_review'] || 0 != $group['require_review']) { $items = RatingItem::getItemsByAttributes(['rating_group' => $group['rating_group']], true, true); } if (!empty($items)) { foreach ($items as $key => $item) { $model = new RatingValues(); $model->loadDefaultValues(); $model->object_id = $objectId; $model->object_model_id = $objectModelId; $model->rating_item_id = $item['id']; $model->value = isset($ratingData['values'][$item['id']]) ? intval($ratingData['values'][$item['id']]) : 0; $model->rating_id = $ratingId; $model->date = $date; $model->save(); } if (isset($review)) { $review->rating_id = $ratingId; $review->save(true, ['rating_id']); } } } } Yii::$app->session->setFlash('info', Yii::t('app', 'Your review will appear on the website immediately after moderation')); return $this->redirect($returnUrl); } } return $this->redirect($returnUrl); }