/**
  * Add a new vote
  * @param integer $id
  * @param string $type
  * @param integer $vote
  * @return mixed
  */
 public function actionVote($id, $type, $vote = 1)
 {
     if (!Yii::$app->user->isGuest) {
         $voteModel = Vote::find()->where(['voteable_type' => $type, 'voteable_id' => $id, 'user_id' => Yii::$app->user->id])->one();
         if (!isset($voteModel->id)) {
             $voteModel = new Vote();
             $voteModel->voteable_type = $type;
             $voteModel->voteable_id = $id;
             $voteModel->user_id = Yii::$app->user->id;
         } elseif ($voteModel->vote == $vote) {
             $out = ['success' => false, 'error' => 'User has already voted'];
             echo Json::encode($out);
             return;
         }
         $voteModel->vote = $vote;
         if ($voteModel->save()) {
             $rating = Vote::getRating($id, $type);
             $out = ['success' => true, 'rating' => $rating];
         } else {
             $out = ['success' => false, 'error' => $voteModel->getErrors()];
         }
     } else {
         $out = ['success' => false, 'error' => 'User is guest'];
     }
     echo Json::encode($out);
 }
Esempio n. 2
0
 public function actionIndex()
 {
     \Yii::$app->response->format = Response::FORMAT_JSON;
     $userId = \Yii::$app->user->id;
     $id = \Yii::$app->request->get('id');
     $type = \Yii::$app->request->get('type', 'article');
     $action = \Yii::$app->request->get('action', 'up');
     if ($type == 'article') {
         $model = Article::find()->where(['id' => $id])->select('id,up,down')->one();
     } else {
         $model = Comment::find()->where(['id' => $id])->select('id,up,down')->one();
     }
     $vote = Vote::find()->where(['type_id' => $id, 'type' => $type, 'action' => $action, 'user_id' => $userId])->one();
     if (empty($vote)) {
         $model->{$action} += 1;
         $model->save(false);
         $vote = new Vote();
         $params = ['type' => $type, 'action' => $action, 'type_id' => $id, 'user_id' => $userId];
         $vote->attributes = $params;
         $vote->save();
     }
     return ['up' => $model->up, 'down' => $model->down];
 }
Esempio n. 3
0
 public static function addVote($entity, $id, $voteAdd)
 {
     $user = User::thisUser();
     $vote = Vote::findOne(['entity' => $entity, 'entity_id' => $id, 'user_id' => $user->id]);
     if (empty($vote)) {
         $vote = new Vote();
         $vote->entity = $entity;
         $vote->entity_id = $id;
         $vote->user_id = $user->id;
     }
     /** @var VoteModel $model */
     $model = null;
     if ($entity == self::ENTITY_ITEM) {
         $model = Item::findOne($id);
         if ($user->reputation < Item::MIN_REPUTATION_ITEM_VOTE) {
             // Если только пользователь не отменяет свои дизлайки
             if (!($vote->vote == self::VOTE_DOWN && $voteAdd == self::VOTE_DOWN)) {
                 return ['vote' => 0, 'count' => $model->getVoteCount(), 'error' => Lang::t('ajax', 'noReputationVote')];
             }
         }
     } else {
         if ($entity == self::ENTITY_EVENT) {
             $model = Event::findOne($id);
             if ($user->reputation < Event::MIN_REPUTATION_EVENT_VOTE) {
                 // Если только пользователь не отменяет свои дизлайки
                 if (!($vote->vote == self::VOTE_DOWN && $voteAdd == self::VOTE_DOWN)) {
                     return ['vote' => 0, 'count' => $model->getVoteCount(), 'error' => Lang::t('ajax', 'noReputationVote')];
                 }
             }
         } else {
             if ($entity == self::ENTITY_SCHOOL) {
                 $model = School::findOne($id);
                 if ($user->reputation < School::MIN_REPUTATION_SCHOOL_VOTE) {
                     // Если только пользователь не отменяет свои дизлайки
                     if (!($vote->vote == self::VOTE_DOWN && $voteAdd == self::VOTE_DOWN)) {
                         return ['vote' => 0, 'count' => $model->getVoteCount(), 'error' => Lang::t('ajax', 'noReputationVote')];
                     }
                 }
             } else {
                 if ($entity == self::ENTITY_COMMENT) {
                     $model = Comment::findOne($id);
                     if ($user->reputation < Comment::MIN_REPUTATION_COMMENT_VOTE) {
                         // Если только пользователь не отменяет свои дизлайки
                         if (!($vote->vote == self::VOTE_DOWN && $voteAdd == self::VOTE_DOWN)) {
                             return ['vote' => 0, 'count' => $model->getVoteCount(), 'error' => Lang::t('ajax', 'noReputationVote')];
                         }
                     }
                 }
             }
         }
     }
     if (!empty($model)) {
         if ($vote->vote == self::VOTE_UP) {
             if ($voteAdd == self::VOTE_UP) {
                 // убираем up
                 $vote->vote = self::VOTE_NONE;
                 $model->addVote(-1);
                 $model->addReputation(VoteModel::ADD_REPUTATION_CANCEL_UP);
             } else {
                 // ставим down
                 $vote->vote = self::VOTE_DOWN;
                 $model->addVote(-2);
                 $model->addReputation(VoteModel::ADD_REPUTATION_CANCEL_UP);
                 $model->addReputation(VoteModel::ADD_REPUTATION_DOWN);
             }
         } elseif ($vote->vote == self::VOTE_DOWN) {
             if ($voteAdd == self::VOTE_UP) {
                 // ставим up
                 $vote->vote = self::VOTE_UP;
                 $model->addVote(2);
                 $model->addReputation(VoteModel::ADD_REPUTATION_CANCEL_DOWN);
                 $model->addReputation(VoteModel::ADD_REPUTATION_UP);
             } else {
                 // убираем down
                 $vote->vote = self::VOTE_NONE;
                 $model->addVote(1);
                 $model->addReputation(VoteModel::ADD_REPUTATION_CANCEL_DOWN);
             }
         } else {
             if ($voteAdd == self::VOTE_UP) {
                 // ставим up
                 $vote->vote = self::VOTE_UP;
                 $model->addVote(1);
                 $model->addReputation(VoteModel::ADD_REPUTATION_UP);
             } else {
                 // ставим down
                 $vote->vote = self::VOTE_DOWN;
                 $model->addVote(-1);
                 $model->addReputation(VoteModel::ADD_REPUTATION_DOWN);
             }
         }
     }
     if ($vote->save()) {
         if (!empty($model)) {
             $model->save();
         }
     }
     return ['vote' => $vote->vote, 'count' => $model->getVoteCount()];
 }