Example #1
0
 /**
  * @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;
 }
Example #2
0
 /**
  * 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;
 }
Example #3
0
 /**
  * @param QuotePostRequest $quoteRequest
  *
  * @return \Illuminate\View\View
  */
 public function viewQuotes(QuotePostRequest $quoteRequest)
 {
     $contents = $quoteRequest->input('posts');
     $data = $posts = $conversations = [];
     //TODO: conversations
     foreach ($contents as $content) {
         if (is_array($content)) {
             $data[] = [(string) $content['id'], $content['data']];
             // It isn't XSS, we parsed it with JS.
             $content = $content['id'];
         } else {
             $content = (string) $content;
             $data[] = [$content, ''];
         }
         $content = explode('_', $content);
         switch ($content[0]) {
             case 'post':
                 $posts[] = (int) $content[1];
                 break;
             case 'conversation':
                 $conversations[] = (int) $content[1];
                 break;
         }
     }
     $myPosts = $this->postsRepository->getPostsByIds($posts);
     $posts = [];
     $content = [];
     foreach ($myPosts as $post) {
         $posts[$post->id] = $post;
     }
     $i = 0;
     foreach ($data as $value) {
         list($type, $id) = explode('_', $value[0]);
         $value = $value[1];
         switch ($type) {
             case 'post':
                 $post = $posts[$id];
                 if ($value) {
                     $oldContent = $post->content;
                     $oldContentParsed = $post->content_parsed;
                     $post->content = $value;
                     $post->content_parsed = e($value);
                 }
                 $author = $post->author;
                 if ($post->author) {
                     $author = app()->make('MyBB\\Core\\Presenters\\User', [$post->author]);
                 }
                 $content[] = ['id' => $i++, 'quote' => $this->quoteRenderer->renderFromPost($post), 'content_parsed' => $post->content_parsed, 'post' => app()->make('MyBB\\Core\\Presenters\\Post', [$post]), 'author' => $author];
                 if ($value) {
                     $post->content = $oldContent;
                     $post->content_parsed = $oldContentParsed;
                 }
                 break;
             case 'conversation':
                 // TODO
                 break;
         }
     }
     return view('post.quotes', ['contents' => $content]);
 }
Example #4
0
 /**
  * @param string $slug
  * @param int    $id
  * @param int    $postId
  *
  * @return \Exception|\Illuminate\Http\RedirectResponse
  */
 public function restore($slug = '', $id = 0, $postId = 0)
 {
     // Forum permissions are checked in "find"
     $topic = $this->topicRepository->find($id);
     $post = $this->postRepository->find($postId);
     if (!$post || !$topic || $post['topic_id'] != $topic['id'] || !$post['deleted_at'] && !$topic['deleted_at']) {
         throw new PostNotFoundException();
     }
     if ($post['id'] == $topic['first_post_id']) {
         $this->topicRepository->restoreTopic($topic);
     } else {
         $this->postRepository->restorePost($post);
     }
     if ($topic) {
         return redirect()->route('topics.showPost', ['slug' => $topic->slug, 'id' => $topic->id, 'postId' => $post->id]);
     }
     return redirect()->route('topics.showPost', ['slug' => $slug, 'id' => $id, 'postId' => $postId])->withErrors([trans('errors.error_deleting_topic')]);
 }
Example #5
0
 /**
  * @param array $posts
  */
 public function merge(array $posts)
 {
     $this->postRepository->mergePosts($posts);
 }
Example #6
0
 /**
  * @param Post $post
  */
 public function deletePost(Post $post)
 {
     $this->postRepository->deletePost($post);
 }