/** * 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); }
/** * Allow to change your user settings */ public function settingsAction() { $usersId = $this->session->get('identity'); if (!$usersId) { $this->flashSession->error('You must be logged first'); $this->response->redirect(); return; } $user = Users::findFirstById($usersId); if (!$user) { $this->flashSession->error('The user does not exist'); $this->response->redirect(); return; } if ($this->request->isPost()) { if (!$this->checkTokenPost()) { $this->response->redirect(); return; } $user->timezone = $this->request->getPost('timezone'); $user->notifications = $this->request->getPost('notifications'); $user->theme = $this->request->getPost('theme'); $user->digest = $this->request->getPost('digest'); if ($user->save()) { $this->session->set('identity-theme', $user->theme); $this->session->get('identity-timezone', $user->timezone); $this->flashSession->success('Settings were successfully updated'); $this->response->redirect(); return; } } else { $this->tag->displayTo('timezone', $user->timezone); $this->tag->displayTo('notifications', $user->notifications); $this->tag->displayTo('theme', $user->theme); $this->tag->displayTo('digest', $user->digest); } $this->tag->setTitle('My Settings'); $this->tag->setAutoEscape(false); $this->gravatar->setSize(64); $this->view->setVars(['user' => $user, 'timezones' => $this->di->getShared('timezones'), 'numberPosts' => Posts::count(['users_id = ?0 AND deleted = 0', 'bind' => [$user->id]]), 'numberReplies' => PostsReplies::count(['users_id = ?0', 'bind' => [$user->id]])]); }
/** * Shows the latest notifications for the current user */ public function notificationsAction($offset = 0) { $usersId = $this->session->get('identity'); if (!$usersId) { $this->flashSession->error('You must be logged first'); return $this->response->redirect(); } $user = Users::findFirstById($usersId); if (!$user) { $this->flashSession->error('The user does not exist'); return $this->response->redirect(); } $this->view->user = $user; $this->view->notifications = ActivityNotifications::find(['users_id = ?0', 'bind' => [$usersId], 'limit' => 128, 'order' => 'created_at DESC']); $this->tag->setTitle('Notifications'); }
/** * Accepts a reply as correct answer */ public function acceptAction($id = 0) { $response = new Response(); /** * Find the post using get */ $postReply = PostsReplies::findFirstById($id); if (!$postReply) { $contentNotExist = array('status' => 'error', 'message' => 'Post reply does not exist'); return $response->setJsonContent($contentNotExist); } $user = Users::findFirstById($this->session->get('identity')); if (!$user) { $contentLogIn = array('status' => 'error', 'message' => 'You must log in first to vote'); return $response->setJsonContent($contentLogIn); } if ($postReply->accepted == 'Y') { $contentAlready = array('status' => 'error', 'message' => 'This reply is already accepted as answer'); return $response->setJsonContent($contentAlready); } if ($postReply->post->deleted) { $contentDeleted = array('status' => 'error', 'message' => 'Post associated to the reply is deleted'); return $response->setJsonContent($contentDeleted); } if ($postReply->post->accepted_answer == 'Y') { $contentAlreadyAnswer = array('status' => 'error', 'message' => 'This post already has an accepted answer'); return $response->setJsonContent($contentAlreadyAnswer); } if ($postReply->post->users_id != $user->id && $user->moderator != 'Y') { $contentCorrect = array('status' => 'error', 'message' => 'You can\'t accept this answer as correct'); return $response->setJsonContent($contentCorrect); } if ($postReply->post->users_id != $postReply->users_id) { $postReply->post->user->karma += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY; $postReply->post->user->votes_points += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY; $points = 30 + intval(abs($user->karma - $postReply->user->karma) / 1000); $parametersBounty = array('users_id = ?0 AND posts_replies_id = ?1', 'bind' => array($postReply->users_id, $postReply->id)); $postBounty = PostsBounties::findFirst($parametersBounty); if ($postBounty) { $points += $postBounty->points; } $postReply->user->karma += $points; $postReply->user->votes_points += $points; if ($postReply->users_id != $user->id && $postReply->post->users_id != $user->id) { $user->karma += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY; $user->votes_points += Karma::SOMEONE_ELSE_ACCEPT_YOUR_REPLY; } } $postReply->accepted = 'Y'; $postReply->post->accepted_answer = 'Y'; if ($postReply->save()) { if (!$user->save()) { foreach ($user->getMessages() as $message) { $contentError = array('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 = $postReply->post->id; $activity->posts_replies_id = $postReply->id; $activity->users_origin_id = $user->id; $activity->type = 'A'; $activity->save(); } $contentOk = array('status' => 'OK'); return $response->setJsonContent($contentOk); }