Esempio n. 1
0
 /**
  * This shows the create post form and also store the related post
  */
 public function createAction()
 {
     if (!($usersId = $this->session->get('identity'))) {
         $this->flashSession->error('You must be logged first');
         return $this->response->redirect();
     }
     $this->tag->setTitle('Start a Discussion');
     $this->gravatar->setSize(48);
     if ($this->request->isPost()) {
         if (!$this->checkTokenPost()) {
             return $this->response->redirect();
         }
         $title = $this->request->getPost('title', 'trim');
         /** @var Users $user */
         $user = Users::findFirstById($usersId);
         $user->increaseKarma(Karma::ADD_NEW_POST);
         $user->save();
         $post = new Posts();
         $post->users_id = $usersId;
         $post->categories_id = $this->request->getPost('categoryId');
         $post->title = $title;
         $post->slug = $this->slug->generate($title);
         $post->content = $this->request->getPost('content');
         if ($post->save()) {
             return $this->response->redirect('discussion/' . $post->id . '/' . $post->slug);
         }
         foreach ($post->getMessages() as $message) {
             $this->flash->error($message);
         }
         $this->view->setVar('firstTime', false);
     } else {
         $this->view->setVar('firstTime', Posts::countByUsersId($usersId) == 0);
     }
     $parameters = ['order' => 'name'];
     $this->view->setVar('categories', Categories::find($parameters));
 }
Esempio n. 2
0
 /**
  * This shows the create post form and also store the related post
  */
 public function createAction()
 {
     if (!($usersId = $this->session->get('identity'))) {
         $this->flashSession->error('You must be logged first');
         $this->response->redirect();
         return;
     }
     $this->tag->setTitle('Start a Discussion');
     $this->gravatar->setSize(48);
     if ($this->request->isPost()) {
         if (!$this->checkTokenPost()) {
             $this->response->redirect();
             return;
         }
         $title = $this->request->getPost('title', 'trim');
         $post = new Posts();
         $post->users_id = $usersId;
         $post->categories_id = $this->request->getPost('categoryId');
         $post->title = $title;
         $post->slug = $this->slug->generate($title);
         $post->content = $this->request->getPost('content');
         if ($post->save()) {
             $user = Users::findFirstById($usersId);
             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();
                 }
             }
             $user->increaseKarma(Karma::ADD_NEW_POST);
             $user->save();
             $this->response->redirect("discussion/{$post->id}/{$post->slug}");
             return;
         }
         $this->flashSession->error(join('<br>', $post->getMessages()));
         $this->view->setVar('firstTime', false);
     } else {
         $this->view->setVar('firstTime', Posts::countByUsersId($usersId) == 0);
     }
     $this->view->setVar('categories', Categories::find(['order' => 'name']));
 }