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('create-post') || !$this->checkCaptcha()) {
$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);
}
$siteKey = isset($this->config->reCaptcha->siteKey) ? $this->config->reCaptcha->siteKey : '';
$this->view->setVar('siteKey', $siteKey);
$this->view->setVar('isUserTrust', $this->isUserTrust());
$this->view->setVar('categories', Categories::find(['order' => 'name']));
}