/** * 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'])); }
/** * 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)); }
} $database->commit(); $categoryIds = Categories::find(['columns' => 'id'])->toArray(); $userIds = Users::find(['columns' => 'id'])->toArray(); $database->begin(); for ($i = 0; $i <= 500; $i++) { $title = $faker->company; $post = new Posts(); $post->title = $title; $post->slug = Tag::friendlyTitle($title); $post->content = $faker->text(); $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;