Phosphorum\Controllers\RepliesController::voteDownAction PHP Method

voteDownAction() public method

Votes a post down
public voteDownAction ( integer $id ) : Phalcon\Http\Response
$id integer
return Phalcon\Http\Response
    public function voteDownAction($id = 0)
    {
        $response = new Response();
        /**
         * Find the post using get
         */
        $postReply = PostsReplies::findFirstById($id);
        if (!$postReply) {
            $contentNotExist = ['status' => 'error', 'message' => 'Post reply does not exist'];
            return $response->setJsonContent($contentNotExist);
        }
        if (!$this->checkTokenGetJson('post-' . $postReply->post->id)) {
            $csrfTokenError = ['status' => 'error', 'message' => 'This post is outdated. Please try to vote for the reply again.'];
            return $response->setJsonContent($csrfTokenError);
        }
        $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);
        }
        $post = $postReply->post;
        if (!$post) {
            $contentPostNotExist = ['status' => 'error', 'message' => 'Post associated to the reply does not exist'];
            return $response->setJsonContent($contentPostNotExist);
        }
        if ($post->deleted) {
            $contentDeleted = ['status' => 'error', 'message' => 'Post associated to the reply is deleted'];
            return $response->setJsonContent($contentDeleted);
        }
        $parametersVoted = ['posts_replies_id = ?0 AND users_id = ?1', 'bind' => [$postReply->id, $user->id]];
        $voted = PostsRepliesVotes::count($parametersVoted);
        if ($voted) {
            $contentAlreadyVoted = ['status' => 'error', 'message' => 'You have already voted this reply'];
            return $response->setJsonContent($contentAlreadyVoted);
        }
        $postReplyVote = new PostsRepliesVotes();
        $postReplyVote->posts_replies_id = $postReply->id;
        $postReplyVote->users_id = $user->id;
        $postReplyVote->vote = PostsRepliesVotes::VOTE_DOWN;
        if (!$postReplyVote->save()) {
            foreach ($postReplyVote->getMessages() as $message) {
                $contentError = ['status' => 'error', 'message' => $message->getMessage()];
                return $response->setJsonContent($contentError);
            }
        }
        $postReply->votes_down++;
        if ($postReply->users_id != $user->id) {
            if ($postReply->post->users_id == $user->id) {
                $karmaCount = intval(abs($user->karma - $postReply->user->karma) / 1000);
                $points = Karma::VOTE_DOWN_ON_MY_REPLY_ON_MY_POST + $karmaCount;
            } else {
                $points = Karma::VOTE_DOWN_ON_MY_REPLY + intval(abs($user->karma - $postReply->user->karma) / 1000);
            }
            $postReply->user->decreaseKarma($points);
        }
        if ($postReply->save()) {
            if ($postReply->users_id != $user->id) {
                $user->decreaseKarma(Karma::VOTE_DOWN_ON_SOMEONE_ELSE_REPLY);
            }
            $user->votes--;
            if (!$user->save()) {
                foreach ($user->getMessages() as $message) {
                    $contentError = ['status' => 'error', 'message' => $message->getMessage()];
                    return $response->setJsonContent($contentError);
                }
            }
        }
        return $response->setJsonContent(['status' => 'OK']);
    }