/** * @inheritdoc * @param int $id * @return int|mixed * @throws NotFoundHttpException */ public function run($id) { /** @var Form|HasProperties $form */ if (null === ($form = Form::findById($id))) { throw new NotFoundHttpException(); } $post = Yii::$app->request->post(); $form->abstractModel->setAttrubutesValues($post); /** @var AbstractModel|SpamCheckerBehavior $model */ $model = $form->getAbstractModel(); if (Yii::$app->request->isAjax && isset($post['ajax'])) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($model); } /** @var \app\models\Object $object */ $object = Object::getForClass(Form::className()); $propGroups = ObjectPropertyGroup::find()->where(['and', 'object_id = :object', 'object_model_id = :id'], [':object' => $object->id, ':id' => $id])->asArray()->all(); $propIds = ArrayHelper::getColumn($propGroups, 'property_group_id'); // Spam checking $activeSpamChecker = SpamChecker::getActive(); $data = []; $haveSpam = false; if ($activeSpamChecker !== null && !empty($activeSpamChecker->api_key)) { $data[$activeSpamChecker->name]['class'] = $activeSpamChecker->behavior; $data[$activeSpamChecker->name]['value']['key'] = $activeSpamChecker->api_key; $properties = Property::getForGroupId($propIds[0]); foreach ($properties as $prop) { if (!isset($activeSpamChecker->{$prop->interpret_as}) || empty($activeSpamChecker->{$prop->interpret_as})) { continue; } $data[$activeSpamChecker->name]['value'][$activeSpamChecker->{$prop->interpret_as}] = is_array($post[$form->abstractModel->formName()][$prop->key]) ? implode(' ', $post[$form->abstractModel->formName()][$prop->key]) : $post[$form->abstractModel->formName()][$prop->key]; } $model->attachBehavior('spamChecker', ['class' => SpamCheckerBehavior::className(), 'data' => $data]); $haveSpam = $model->isSpam(); } $date = new \DateTime(); /** @var Submission|HasProperties $submission */ $submission = new Submission(['form_id' => $form->id, 'date_received' => $date->format('Y-m-d H:i:s'), 'ip' => Yii::$app->request->userIP, 'user_agent' => Yii::$app->request->userAgent, 'spam' => (int) $haveSpam]); if (false === Yii::$app->user->isGuest) { $submission->processed_by_user_id = Yii::$app->user->identity->getId(); } if (!($form->abstractModel->validate() && $submission->save())) { return "0"; } if (isset($post[$form->abstractModel->formName()])) { $data = ['AddPropetryGroup' => [$submission->formName() => array_keys($form->getPropertyGroups())], $submission->abstractModel->formName() => $post[$form->abstractModel->formName()]]; if (isset($_FILES[$form->abstractModel->formName()])) { $_FILES[$submission->abstractModel->formName()] = $_FILES[$form->abstractModel->formName()]; } $submission->saveProperties($data); } return $submission->id; }
/** * @param int $id * @return int|mixed * @throws NotFoundHttpException */ public function run($id) { /** @var Form|HasProperties $form */ if (null === ($form = Form::findById($id))) { throw new NotFoundHttpException(); } $post = \Yii::$app->request->post(); $form->abstractModel->setAttrubutesValues($post); /** @var AbstractModel|SpamCheckerBehavior $model */ $model = $form->getAbstractModel(); if (\Yii::$app->request->isAjax && isset($post['ajax'])) { \Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($model); } /** @var \app\models\Object $object */ $object = Object::getForClass(Form::className()); $propGroups = ObjectPropertyGroup::find()->where(['and', 'object_id = :object', 'object_model_id = :id'], [':object' => $object->id, ':id' => $id])->asArray()->all(); $propIds = ArrayHelper::getColumn($propGroups, 'property_group_id'); // Spam checking $activeSpamChecker = SpamChecker::getActive(); $data = []; $spamResult = []; if ($activeSpamChecker !== null && !empty($activeSpamChecker->api_key)) { $data[$activeSpamChecker->name]['class'] = $activeSpamChecker->behavior; $data[$activeSpamChecker->name]['value']['key'] = $activeSpamChecker->api_key; $properties = Property::getForGroupId($propIds[0]); foreach ($properties as $prop) { if (!isset($activeSpamChecker->{$prop->interpret_as}) || empty($activeSpamChecker->{$prop->interpret_as})) { continue; } $data[$activeSpamChecker->name]['value'][$activeSpamChecker->{$prop->interpret_as}] = is_array($post[$form->abstractModel->formName()][$prop->key]) ? implode(' ', $post[$form->abstractModel->formName()][$prop->key]) : $post[$form->abstractModel->formName()][$prop->key]; } $model->attachBehavior('spamChecker', ['class' => SpamCheckerBehavior::className(), 'data' => $data]); $spamResult = $model->check(); } $haveSpam = false; if (is_array($spamResult) === true) { foreach ($spamResult as $result) { if (ArrayHelper::getValue($result, 'ok', false) === true) { $haveSpam = $haveSpam || ArrayHelper::getValue($result, 'is_spam', false); } } } $date = new \DateTime(); /** @var Submission|HasProperties $submission */ $submission = new Submission(['form_id' => $form->id, 'date_received' => $date->format('Y-m-d H:i:s'), 'ip' => Yii::$app->request->userIP, 'user_agent' => Yii::$app->request->userAgent, 'spam' => (int) $haveSpam]); if (false === Yii::$app->user->isGuest) { $submission->processed_by_user_id = Yii::$app->user->identity->getId(); } if (!($form->abstractModel->validate() && $submission->save())) { return "0"; } if (isset($post[$form->abstractModel->formName()])) { $data = ['AddPropetryGroup' => [$submission->formName() => array_keys($form->getPropertyGroups())], $submission->abstractModel->formName() => $post[$form->abstractModel->formName()]]; if (isset($_FILES[$form->abstractModel->formName()])) { $_FILES[$submission->abstractModel->formName()] = $_FILES[$form->abstractModel->formName()]; } $submission->saveProperties($data); } if ($haveSpam === false) { if (!empty($form->email_notification_addresses)) { try { $emailView = !empty($form->email_notification_view) ? $form->email_notification_view : '@app/widgets/form/views/email-template.php'; Yii::$app->mail->compose($emailView, ['form' => $form, 'submission' => $submission])->setTo(explode(',', $form->email_notification_addresses))->setFrom(Yii::$app->mail->getMailFrom())->setSubject($form->name . ' #' . $submission->id)->send(); } catch (\Exception $e) { // do nothing } } } return $submission->id; }
/** * @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); }