Exemplo n.º 1
0
 /**
  * Method for voting a task
  *
  * @return mixed
  */
 public function voteAction()
 {
     $this->view->disable();
     if (!$this->request->isPost()) {
         return $this->response->redirect($this->router->getControllerName());
     }
     $way = 'positive';
     if ($this->request->getPost('way') == 'negative') {
         $way = 'negative';
     }
     $objectId = $this->request->getPost('objectId');
     $object = $this->request->getPost('object');
     $vote = Vote::vote($objectId, $object, $way);
     $user = Users::findFirstById($this->auth->getAuth()['id']);
     $this->setJsonResponse();
     if (!$user) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'You need to login first'];
         return $this->jsonMessages;
     }
     if ($object == Vote::OBJECT_POSTS) {
         if (!($post = Posts::findFirstById($objectId))) {
             $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Post does not exist'];
             return $this->jsonMessages;
         }
         $this->setPointPost($way, $user, $post);
         //Adding notification when you have receive vote on the post, and not for now for post replies
         if ($user->getId() != $post->getUsersId()) {
             $this->setActivityNotifications($user, $post);
         }
     }
     if ($object == Vote::OBJECT_POSTS_REPLY) {
         if (!($postReply = PostsReply::findFirstById($objectId))) {
             $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Post reply does not exist'];
             return $this->jsonMessages;
         }
         //Set karam Voting someone else's post (positive or negative) on posts reply
         $this->setPointReply($way, $user, $postReply);
     }
     if (!$vote) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Vote have a problem :)'];
         return $this->jsonMessages;
     }
     if ($user->getVote() <= 0) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => t('You don\'t have enough votes available :)')];
         return $this->jsonMessages;
     }
     //checking the user have already voted this post yet
     if (is_array($vote)) {
         $this->jsonMessages['messages'][] = $vote;
         return $this->jsonMessages;
     }
     if ($this->request->isAjax()) {
         $vote = (new Vote())->getVotes($objectId, $object);
         return ['data' => $vote['positive'] - $vote['negative']];
     }
     echo 0;
     return 0;
 }