Exemplo n.º 1
0
 /**
  * Check whether the user can have the badge
  *
  * @param Users $user
  * @return boolean
  */
 public function canHave(Users $user)
 {
     $canHave = PostsRepliesVotes::count(['users_id = ?0 AND vote = 1', 'bind' => [$user->id]]) > 0;
     $canHave = $canHave || PostsVotes::count(['users_id = ?0 AND vote = 1', 'bind' => [$user->id]]) > 0;
     return $canHave;
 }
Exemplo n.º 2
0
 /**
  * Votes a post down
  */
 public function voteDownAction($id = 0)
 {
     $response = new Response();
     if (!$this->checkTokenGetJson()) {
         $csrfTokenError = ['status' => 'error', 'message' => 'Token error. This might be CSRF attack.'];
         return $response->setJsonContent($csrfTokenError);
     }
     /**
      * Find the post using get
      */
     $post = Posts::findFirstById($id);
     if (!$post) {
         $contentNotExist = ['status' => 'error', 'message' => 'Post does not exist'];
         return $response->setJsonContent($contentNotExist);
     }
     $user = Users::findFirstById($this->session->get('identity'));
     if (!$user) {
         $contentlogIn = ['status' => 'error', 'message' => 'You must log in first to vote'];
         return $response->setJsonContent($contentlogIn);
     }
     if ($user->votes <= 0) {
         $contentDontHave = ['status' => 'error', 'message' => "You don't have enough votes available"];
         return $response->setJsonContent($contentDontHave);
     }
     if (PostsVotes::count(['posts_id = ?0 AND users_id = ?1', 'bind' => [$post->id, $user->id]])) {
         $contentAlreadyVote = ['status' => 'error', 'message' => 'You have already voted this post'];
         return $response->setJsonContent($contentAlreadyVote);
     }
     $postVote = new PostsVotes();
     $postVote->posts_id = $post->id;
     $postVote->users_id = $user->id;
     $postVote->vote = PostsVotes::VOTE_DOWN;
     $postVote->save();
     $post->votes_down++;
     if ($post->users_id != $user->id) {
         $post->user->decreaseKarma(Karma::SOMEONE_DID_VOTE_MY_POST);
         $user->increaseKarma(Karma::VOTE_ON_SOMEONE_ELSE_POST);
     }
     if ($post->save()) {
         $user->votes--;
         if (!$user->save()) {
             foreach ($user->getMessages() as $message) {
                 $contentErrorSave = ['status' => 'error', 'message' => $message->getMessage()];
                 return $response->setJsonContent($contentErrorSave);
             }
         }
     }
     $contentOk = ['status' => 'OK'];
     return $response->setJsonContent($contentOk);
 }
Exemplo n.º 3
0
 /**
  * Check whether the user can have the badge
  *
  * @param Users $user
  * @return boolean
  */
 public function canHave(Users $user)
 {
     $canHave = PostsRepliesVotes::count(array('users_id = ?0 AND vote = -1', 'bind' => array($user->id))) > 0;
     $canHave = $canHave || PostsVotes::count(array('users_id = ?0 AND vote = -1', 'bind' => array($user->id))) > 0;
     return $canHave;
 }