Esempio n. 1
0
 public function testGetIsAllowChangeVote()
 {
     $firstIsAllow = Rating::getIsAllowChangeVote($this->firstModelId);
     $this->assertEquals($firstIsAllow, true);
     $secondIsAllow = Rating::getIsAllowChangeVote($this->secondModelId);
     $this->assertEquals($secondIsAllow, true);
     $thirdIsAllow = Rating::getIsAllowChangeVote($this->thirdModelId);
     $this->assertEquals($thirdIsAllow, false);
 }
Esempio n. 2
0
 public function run()
 {
     if (Yii::$app->request->getIsAjax()) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         if (null === ($modelId = (int) Yii::$app->request->post('modelId'))) {
             return ['content' => Yii::t('vote', 'modelId has not been sent')];
         }
         if (null === ($targetId = (int) Yii::$app->request->post('targetId'))) {
             return ['content' => Yii::t('vote', 'The purpose is not defined')];
         }
         $act = Yii::$app->request->post('act');
         if (!in_array($act, ['like', 'dislike'], true)) {
             return ['content' => Yii::t('vote', 'Wrong action')];
         }
         $value = $act === 'like' ? Rating::VOTE_LIKE : Rating::VOTE_DISLIKE;
         $userId = Yii::$app->user->getId();
         if ($userId === null && !Rating::getIsAllowGuests($modelId)) {
             return ['content' => Yii::t('vote', 'Guests are not allowed to vote')];
         }
         if (!($userIp = Rating::compressIp(Yii::$app->request->getUserIP()))) {
             return ['content' => Yii::t('vote', 'The user is not recognized')];
         }
         if (!is_int($modelId)) {
             return ['content' => Yii::t('vote', 'The model is not registered')];
         }
         if (Rating::getIsAllowGuests($modelId)) {
             $isVoted = Rating::findOne(['model_id' => $modelId, 'target_id' => $targetId, 'user_ip' => $userIp]);
         } else {
             $isVoted = Rating::findOne(['model_id' => $modelId, 'target_id' => $targetId, 'user_id' => $userId]);
         }
         if (is_null($isVoted)) {
             $newVote = new Rating();
             $newVote->model_id = $modelId;
             $newVote->target_id = $targetId;
             $newVote->user_id = $userId;
             $newVote->user_ip = $userIp;
             $newVote->value = $value;
             if ($newVote->save()) {
                 if ($value === Rating::VOTE_LIKE) {
                     return ['content' => Yii::t('vote', 'Your vote is accepted. Thanks!'), 'success' => true];
                 } else {
                     return ['content' => Yii::t('vote', 'Thanks for your opinion'), 'success' => true];
                 }
             } else {
                 return ['content' => Yii::t('vote', 'Validation error')];
             }
         } else {
             if ($isVoted->value !== $value && Rating::getIsAllowChangeVote($modelId)) {
                 $isVoted->value = $value;
                 if ($isVoted->save()) {
                     return ['content' => Yii::t('vote', 'Your vote has been changed. Thanks!'), 'success' => true, 'changed' => true];
                 } else {
                     return ['content' => Yii::t('vote', 'Validation error')];
                 }
             }
             return ['content' => Yii::t('vote', 'You have already voted!')];
         }
     } else {
         throw new MethodNotAllowedHttpException(Yii::t('vote', 'Forbidden method'), 405);
     }
 }