/** * @param Topic $topic * * @return bool */ public function deleteTopic(Topic $topic) { if ($topic->deleted_at == null) { $topic->forum->decrement('num_topics'); $topic->forum->decrement('num_posts', $topic->num_posts); if ($topic->user_id > 0) { $topic->author->decrement('num_topics'); } $success = $topic->delete(); if ($success) { if ($topic->last_post_id == $topic->forum->last_post_id) { $this->forumRepository->updateLastPost($topic->forum); } } return $success; } else { // First we need to remove old foreign keys - otherwise we can't delete posts $topic->update(['first_post_id' => null, 'last_post_id' => null]); // Now delete the posts for this topic $this->postRepository->deletePostsForTopic($topic); // Don't forget the polls if ($topic->has_poll) { $this->pollRepository->remove($topic->poll); } // And finally delete the topic $topic->forceDelete(); } return true; }
/** * @param int $forumId * @param CreateRequest $createRequest * * @return $this|bool|\Illuminate\Http\RedirectResponse */ public function postCreate($forumId, CreateRequest $createRequest) { // Forum permissions are checked in "CreateRequest" if (!$this->guard->check()) { $captcha = $this->checkCaptcha(); if ($captcha !== true) { return $captcha; } } $poll = null; if ($createRequest->input('add-poll')) { $pollCreateRequest = app()->make('MyBB\\Core\\Http\\Requests\\Poll\\CreateRequest'); $poll = ['question' => $pollCreateRequest->input('question'), 'num_options' => count($pollCreateRequest->options()), 'options' => $pollCreateRequest->options(), 'is_closed' => false, 'is_multiple' => (bool) $pollCreateRequest->input('is_multiple'), 'is_public' => (bool) $pollCreateRequest->input('is_public'), 'end_at' => null, 'max_options' => (int) $pollCreateRequest->input('maxoptions')]; if ($pollCreateRequest->input('endAt')) { $poll['end_at'] = new \DateTime($pollCreateRequest->input('endAt')); } } $topic = $this->topicRepository->create(['title' => $createRequest->input('title'), 'forum_id' => $createRequest->input('forum_id'), 'first_post_id' => 0, 'last_post_id' => 0, 'views' => 0, 'num_posts' => 0, 'content' => $createRequest->input('content'), 'username' => $createRequest->input('username')]); if ($topic) { if ($poll) { $poll['topic_id'] = $topic->id; $this->pollRepository->create($poll); $this->topicRepository->setHasPoll($topic, true); } return redirect()->route('topics.show', ['slug' => $topic->slug, 'id' => $topic->id]); } return redirect()->route('topic.create', ['forumId' => $forumId])->withInput()->withErrors(['content' => trans('errors.error_creating_topic')]); }
/** * @param string $topicSlug * @param int $topicId * * @return \Illuminate\Http\RedirectResponse */ public function remove($topicSlug, $topicId) { $topic = $this->topicRepository->find($topicId); if (!$topic) { throw new TopicNotFoundException(); } if (!$topic->has_poll) { throw new PollNotFoundException(); } $poll = $topic->poll; $this->pollRepository->remove($poll); $topic->has_poll = false; $topic->save(); return redirect()->route('topics.show', [$topicSlug, $topicId]); }