Exemple #1
0
 /**
  * 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);
 }
 /**
  * This shows the create post form and also store the related post
  *
  * @param int $id Post ID
  */
 public function editAction($id)
 {
     if (!($usersId = $this->session->get('identity'))) {
         $this->flashSession->error('You must be logged first');
         $this->response->redirect();
         return;
     }
     $parameters = ["id = ?0 AND (users_id = ?1 OR 'Y' = ?2)", 'bind' => [$id, $usersId, $this->session->get('identity-moderator')]];
     if (!($post = Posts::findFirst($parameters))) {
         $this->flashSession->error('The discussion does not exist');
         $this->response->redirect();
         return;
     }
     if ($this->request->isPost()) {
         if (!$this->checkTokenPost()) {
             $this->response->redirect();
             return;
         }
         $title = $this->request->getPost('title', 'trim');
         $content = $this->request->getPost('content');
         /** @var \Phalcon\Db\Adapter\Pdo\Mysql $connection */
         $connection = $this->getDI()->getShared('db');
         $connection->begin();
         $post->categories_id = $this->request->getPost('categoryId');
         $post->title = $title;
         $post->slug = $this->slug->generate($title);
         $post->content = $content;
         $post->edited_at = time();
         if (!$post->hasPoll() || !$post->isStartVoting()) {
             foreach ($post->getPollOptions() as $option) {
                 $option->delete();
             }
             if ($pollOptions = $this->request->getPost('pollOptions', ['trim'], [])) {
                 foreach ($pollOptions as $opt) {
                     $option = new PostsPollOptions();
                     $option->posts_id = $post->id;
                     $option->title = htmlspecialchars($opt, ENT_QUOTES);
                     $option->save();
                 }
             }
         }
         if ($post->isStartVoting()) {
             $connection->rollback();
             $this->flashSession->error("The voting for the poll was started. You can't change the Poll.");
         } else {
             if ($post->save()) {
                 if ($post->users_id != $usersId && ($user = Users::findFirstById($usersId))) {
                     $user->increaseKarma(Karma::MODERATE_POST);
                     $user->save();
                 }
                 $connection->commit();
                 $this->response->redirect("discussion/{$post->id}/{$post->slug}");
                 return;
             } else {
                 $connection->rollback();
                 $this->flashSession->error(join('<br>', $post->getMessages()));
             }
         }
     } else {
         $this->tag->displayTo('id', $post->id);
         $this->tag->displayTo('title', $post->title);
         $this->tag->displayTo('content', $post->content);
         $this->tag->displayTo('categoryId', $post->categories_id);
     }
     $this->tag->setTitle('Edit Discussion: ' . $this->escaper->escapeHtml($post->title));
     $this->gravatar->setSize(48);
     $this->view->setVars(['categories' => Categories::find(['order' => 'name']), 'post' => $post, 'optionsCount' => $post->pollOptions->count()]);
 }
Exemple #3
0
    $userRandId = array_rand($userIds);
    $post->users_id = $userIds[$userRandId]['id'];
    $categoryRandId = array_rand($categoryIds);
    $post->categories_id = $categoryIds[$categoryRandId]['id'];
    if (!$post->save()) {
        $database->rollback();
        die(join(PHP_EOL, $post->getMessages()));
    }
    if (!mt_rand(0, 10)) {
        $size = mt_rand(2, 10);
        $options = [];
        for ($j = 0; $j < $size; $j++) {
            $options[$j] = $faker->company;
        }
        foreach ($options as $opt) {
            $option = new PostsPollOptions();
            $option->posts_id = $post->id;
            $option->title = htmlspecialchars($opt, ENT_QUOTES);
            if (!$option->save()) {
                echo join(PHP_EOL, $option->getMessages()), PHP_EOL;
                $database->rollback();
                die;
            }
            $log->info('Option: ' . $option->title);
        }
    }
    $log->info('Post: ' . $post->title);
}
$database->commit();
$postIds = Posts::find(['columns' => 'id'])->toArray();
$database->begin();