/** * Votes for a poll option * * @param int $id Post ID * @param int $option Option ID * @return Response */ public function voteAction($id = 0, $option = 0) { $response = new Response(); if (!$this->checkTokenGetJson('post-' . $id)) { $csrfTokenError = ['status' => 'error', 'message' => 'This post is outdated. Please try to vote again.']; return $response->setJsonContent($csrfTokenError); } if (!($post = Posts::findFirstById($id))) { $contentNotExist = ['status' => 'error', 'message' => 'Poll does not exist']; return $response->setJsonContent($contentNotExist); } if (!($user = Users::findFirstById($this->session->get('identity')))) { $contentlogIn = ['status' => 'error', 'message' => 'You must log in first to vote']; return $response->setJsonContent($contentlogIn); } if (!($option = PostsPollOptions::findFirstById($option))) { $optionNotFound = ['status' => 'error', 'message' => 'Please select one option from the list below']; return $response->setJsonContent($optionNotFound); } if ($post->isParticipatedInPoll($user->id)) { $contentAlreadyVote = ['status' => 'error', 'message' => 'You have already voted this post']; return $response->setJsonContent($contentAlreadyVote); } $pollVote = new PostsPollVotes(); $pollVote->posts_id = $post->id; $pollVote->users_id = $user->id; $pollVote->options_id = $option->id; if (!$pollVote->save()) { foreach ($pollVote->getMessages() as $message) { /** @var \Phalcon\Mvc\Model\Message $message */ $contentError = ['status' => 'error', 'message' => $message->getMessage()]; return $response->setJsonContent($contentError); } } if ($post->users_id != $user->id) { $post->user->increaseKarma(Karma::SOMEONE_DID_VOTE_MY_POLL); $user->increaseKarma(Karma::VOTE_ON_SOMEONE_ELSE_POLL); } if (!$post->save()) { foreach ($post->getMessages() as $message) { /** @var \Phalcon\Mvc\Model\Message $message */ $contentErrorSave = ['status' => 'error', 'message' => $message->getMessage()]; return $response->setJsonContent($contentErrorSave); } } $viewCache = $this->getDI()->getShared('viewCache'); $viewCache->delete('post-' . $post->id); $viewCache->delete('poll-votes-' . $post->id); $viewCache->delete('poll-options-' . $post->id); $contentOk = ['status' => 'OK']; return $response->setJsonContent($contentOk); }
/** * Displays a post and its comments * * @param int $id Post ID * @param string $slug Post slug [Optional] */ public function viewAction($id, $slug = '') { $id = (int) $id; // Check read / unread topic if ($usersId = $this->session->get('identity')) { $check_topic = new TopicTracking(); $check_topic->user_id = $usersId; $check_topic->topic_id = $id; if ($check_topic->create() == false) { $check_topic->updateTracking($id, $usersId); } } $this->gravatar->setSize(48); if (!$this->request->isPost()) { // Find the post using get if (!($post = Posts::findFirstById($id))) { $this->flashSession->error('The discussion does not exist'); $this->response->redirect(); return; } if ($post->deleted) { $this->flashSession->error('The discussion is deleted'); $this->response->redirect(); return; } $ipAddress = $this->request->getClientAddress(); $parameters = ['posts_id = ?0 AND ipaddress = ?1', 'bind' => [$id, $ipAddress]]; // A view is stored by ip address if (!($viewed = PostsViews::count($parameters))) { // Increase the number of views in the post $post->number_views++; if ($post->users_id != $usersId) { $post->user->increaseKarma(Karma::VISIT_ON_MY_POST); if ($user = Users::findFirstById($usersId)) { $user->increaseKarma($user->moderator == 'Y' ? Karma::MODERATE_VISIT_POST : Karma::VISIT_POST); $user->save(); } } $postView = new PostsViews(); $postView->post = $post; $postView->ipaddress = $ipAddress; if (!$postView->save()) { $this->flash->error(join('<br>', $postView->getMessages())); } } if (!$usersId) { // Enable cache $this->view->cache(['key' => 'post-' . $id]); // Check for a cache if ($this->viewCache->exists('post-' . $id)) { return; } } // Generate canonical meta $this->view->setVars(['canonical' => "discussion/{$post->id}/{$post->slug}", 'author' => $post->user]); } else { if (!$this->checkTokenPost()) { $this->response->redirect(); return; } if (!$usersId) { $this->flashSession->error('You must be logged in first to add a comment'); $this->response->redirect(); return; } // Find the post using POST if (!($post = Posts::findFirstById($this->request->getPost('id')))) { $this->flashSession->error('The discussion does not exist'); $this->response->redirect(); return; } if ($post->deleted) { $this->flashSession->error('The discussion is deleted'); $this->response->redirect(); return; } if ($content = $this->request->getPost('content', 'trim')) { // Check if the question can have a bounty before add the reply $canHaveBounty = $post->canHaveBounty(); if (!($user = Users::findFirstById($usersId))) { $this->flashSession->error('You must be logged in first to add a comment'); $this->response->redirect(); return; } // Only update the number of replies if the user that commented isn't the same that posted if ($post->users_id != $usersId) { $post->number_replies++; $post->modified_at = time(); $post->user->increaseKarma(Karma::SOMEONE_REPLIED_TO_MY_POST); $user->increaseKarma(Karma::REPLY_ON_SOMEONE_ELSE_POST); $user->save(); } $postReply = new PostsReplies(); $postReply->post = $post; $postReply->in_reply_to_id = $this->request->getPost('reply-id', 'int'); $postReply->users_id = $usersId; $postReply->content = $content; if ($postReply->save()) { if ($post->users_id != $usersId && $canHaveBounty) { $bounty = $post->getBounty(); $postBounty = new PostsBounties(); $postBounty->posts_id = $post->id; $postBounty->users_id = $usersId; $postBounty->posts_replies_id = $postReply->id; $postBounty->points = $bounty['value']; if (!$postBounty->save()) { $this->flash->error(join('<br>', $postBounty->getMessages())); } } $this->response->redirect("discussion/{$post->id}/{$post->slug}#C{$postReply->id}"); return; } $this->flash->error(join('<br>', $postReply->getMessages())); } } $voting = []; if ($post->hasPoll()) { $totalVotes = $post->getPollVotes()->count(); $votesCount = PostsPollVotes::count(['posts_id = ?0', 'group' => 'options_id', 'bind' => [$post->id]]); foreach ($votesCount as $row) { /** @var \Phalcon\Mvc\Model\Row $row */ $voting[$row->offsetGet('options_id')] = round($row->offsetGet('rowcount') * 100 / $totalVotes, 1); } } // Set the post name as title - escaping it first $this->tag->setTitle($this->escaper->escapeHtml($post->title) . ' - Discussion'); $this->view->setVars(['post' => $post, 'voted' => $post->isParticipatedInPoll($usersId), 'voting' => $voting]); }