/** * */ private function _loadModel() { if (\Yii::$app->user->isGuest) { $scenario = 'add-guest'; $author = 'Anonymous'; } else { $scenario = 'add-user'; $author = \Yii::$app->user->identity->username; } $model = new Comment(['scenario' => $scenario]); $model->author = $author; $model->user_id = \Yii::$app->user->id; $model->record_id = $this->recordId; $this->_model = $model; if (!$this->_model->load(\Yii::$app->request->post())) { return; } $parentComment = $this->_model->parentId ? Comment::findOne($this->_model->parentId) : Comment::find()->roots()->one(); if (!$this->_model->appendTo($parentComment)) { \Yii::$app->session->setFlash("error", \Yii::t("comment", "Yer comment fell overboard :( Add it again, lad!")); return; } $this->_createCommentAddEvent(); \Yii::$app->session->setFlash("success", \Yii::t("comment", "Aye! Yer comment is mine!")); \Yii::$app->response->redirect(""); \Yii::$app->end(); }
/** * @param $id * @param $action * @return string */ public function actionRating($id, $action) { $id = intval($id); $ratingValue = $this->_getRatingValue($action); if (!\Yii::$app->request->isAjax || !$id || !$ratingValue) { return \Yii::t("comment", "Bad request"); } $userId = \Yii::$app->user->id; $userHash = Utils::userHash(\Yii::$app->request->userIP); $isGuest = \Yii::$app->user->isGuest; $comment = Comment::find()->where(['id' => $id])->one(); if (!$comment) { return \Yii::t("comment", "Can't see any comments even through my spyglass"); } if ($isGuest && $comment->user_hash == $userHash || !$isGuest && $comment->user_id == $userId) { return \Yii::t("comment", "Avast! Ya can't vote for yer own comment"); } if ($this->_checkMark($comment->id, $isGuest, $userHash, $userId)) { return \Yii::t("comment", "C'mon, lad, ya've already voted on this comment"); } return $this->_saveRating($comment, $ratingValue); }
/** * @return \yii\db\ActiveQuery */ public function getComment() { return $this->hasOne(Comment::className(), ['id' => 'comment_id']); }