/** * @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 Post $post * @param Topic $topic */ public function movePostToTopic(Post $post, Topic $topic) { $post->topic->decrement('num_posts'); $post->topic->forum->decrement('num_posts'); $post->topic_id = $topic->id; $post->save(); $topic->increment('num_posts'); $topic->forum->increment('num_posts'); $topic->update(['last_post_id' => $post->id]); $topic->forum->update(['last_post_id' => $post->id, 'last_post_user_id' => $post->author->id]); }
/** * Add a post to a topic. * * @param Topic $topic The topic to add a post to. * @param array $postDetails The details of the post to add. * * @return mixed */ public function addPostToTopic(Topic $topic, array $postDetails) { $postDetails = array_merge(['user_id' => $this->guard->user()->id, 'username' => null, 'content' => '', 'content_parsed' => ''], $postDetails); $postDetails['content_parsed'] = $this->formatter->parse($postDetails['content'], [MessageFormatter::ME_USERNAME => $this->guard->user()->name]); // TODO: Parser options... if ($postDetails['user_id'] > 0) { $postDetails['username'] = User::find($postDetails['user_id'])->name; } else { $postDetails['user_id'] = null; if ($postDetails['username'] == trans('general.guest')) { $postDetails['username'] = null; } } $post = $topic->posts()->create($postDetails); if ($post !== false) { $topic->increment('num_posts'); $topic->update(['last_post_id' => $post['id']]); $topic->forum->increment('num_posts'); $topic->forum->update(['last_post_id' => $post->id, 'last_post_user_id' => $postDetails['user_id']]); } if ($post->user_id > 0) { $post->author->increment('num_posts'); } return $post; }