public function voteUpAction($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_UP;
if (!$postReplyVote->save()) {
foreach ($postReplyVote->getMessages() as $message) {
$contentError = ['status' => 'error', 'message' => $message->getMessage()];
return $response->setJsonContent($contentError);
}
}
$postReply->votes_up++;
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_UP_ON_MY_REPLY_ON_MY_POST + $karmaCount;
} else {
$points = Karma::VOTE_UP_ON_MY_REPLY + intval(abs($user->karma - $postReply->user->karma) / 1000);
}
$postReply->user->increaseKarma($points);
}
if ($postReply->save()) {
if ($postReply->users_id != $user->id) {
$user->increaseKarma(Karma::VOTE_UP_ON_SOMEONE_ELSE_REPLY);
}
$user->votes--;
if (!$user->save()) {
foreach ($user->getMessages() as $message) {
$contentError = ['status' => 'error', 'message' => $message->getMessage()];
return $response->setJsonContent($contentError);
}
}
}
if ($user->id != $postReply->users_id) {
$activity = new ActivityNotifications();
$activity->users_id = $postReply->users_id;
$activity->posts_id = $post->id;
$activity->posts_replies_id = $postReply->id;
$activity->users_origin_id = $user->id;
$activity->type = 'R';
$activity->save();
}
return $response->setJsonContent(['status' => 'OK']);
}