/** * Create a new topic * * @param array $details Details about the topic. * * @return mixed */ public function create(array $details = []) { $details = array_merge(['title' => '', 'forum_id' => 0, 'user_id' => $this->guard->user()->id, 'username' => null, 'first_post_id' => 0, 'last_post_id' => 0, 'views' => 0, 'num_posts' => 0, 'content' => ''], $details); $details['slug'] = $this->createSlugForTitle($details['title']); if ($details['user_id'] > 0) { $details['username'] = User::find($details['user_id'])->name; // TODO: Use User Repository! } else { $details['user_id'] = null; if ($details['username'] == trans('general.guest')) { $details['username'] = null; } } $topic = null; $this->dbManager->transaction(function () use($details, &$topic) { $topic = $this->topicModel->create(['title' => $details['title'], 'slug' => $details['slug'], 'forum_id' => $details['forum_id'], 'user_id' => $details['user_id'], 'username' => $details['username']]); $firstPost = $this->postRepository->addPostToTopic($topic, ['content' => $details['content'], 'username' => $details['username']]); $topic->update(['first_post_id' => $firstPost->id, 'last_post_id' => $firstPost->id, 'num_posts' => 1]); }); $topic->forum->increment('num_topics'); if ($topic->user_id > 0) { $topic->author->increment('num_topics'); } return $topic; }
/** * @param string $slug * @param int $id * @param ReplyRequest $replyRequest * * @return $this|bool|\Illuminate\Http\RedirectResponse */ public function postReply($slug, $id, ReplyRequest $replyRequest) { $this->failedValidationRedirect = route('topics.reply', ['slug' => $slug, 'id' => $id]); // Forum permissions are checked in "find" /** @var Topic $topic */ $topic = $this->topicRepository->find($id); if (!$topic) { throw new TopicNotFoundException(); } if (!$this->guard->check()) { $captcha = $this->checkCaptcha(); if ($captcha !== true) { return $captcha; } } $post = $this->postRepository->addPostToTopic($topic, ['content' => $replyRequest->input('content'), 'username' => $replyRequest->input('username')]); if ($post) { return redirect()->route('topics.last', ['slug' => $topic->slug, 'id' => $topic->id]); } return redirect()->route('topic.reply', ['slug' => $topic->slug, 'id' => $topic->id])->withInput()->withErrors(['content' => trans('errors.error_creating_post')]); }