/** * Shows topic * * @param string $slug * @param int|null $quotationPostId * * @return Response|RedirectResponse * * @throws Exception */ public function showTopicAction($slug, $quotationPostId = null) { $topic = $this->getTopicManager()->findOneBy(array('slug' => $slug)); if (!$topic) { throw $this->createNotFoundException(sprintf('Topic with slug %s does not exists', $slug)); } $editingPost = $this->request->attributes->get('editPost', null); if ($editingPost) { $this->generateBreadcrumb($editingPost, $this->translator->trans('edition.post')); $post = $this->getPostManager()->find($editingPost); } else { $post = new Post(); } if ($quotationPostId) { $quotationPost = $this->getPostManager()->find($quotationPostId); if ($quotationPost) { $this->generateBreadcrumb($quotationPost, $this->translator->trans('addition/quotation.post')); $post->setDescription('[quote]' . $quotationPost->getDescription() . '[/quote]'); } } if (!isset($quotationPost) && !isset($editingPost)) { $this->generateBreadcrumb($topic, $this->translator->trans('addition.post')); } if ($this->isLogged()) { $postForm = $this->createForm('post_type', $post); $postForm->handleRequest($this->request); if (!$editingPost) { //if the post is edited, form is not checked. if ($postForm->isValid()) { try { $post->setTopic($topic); $this->getPostManager()->update($post); $this->addFlash('success', $this->translator->trans('post.has.been.created')); } catch (Exception $ex) { $this->addFlash('danger', $this->translator->trans('post.has.not.been.created')); } $templateParameters = array('slug' => $topic->getSlug()); //we check page and if page > 1 then add page parameter to route $page = ceil($this->getPostManager()->countPostsInTopic($topic->getId()) / 10); if ($page > 1) { $templateParameters['page'] = $page; } return $this->redirect($this->generateUrl('topic_show', $templateParameters)); } } } $pagination = $this->paginator->paginate($this->getPostManager()->findPostsByTopic($topic->getId()), $this->request->get('page', 1), 10, array('wrap-queries' => true)); $postsIds = array_map(function ($post) { return $post->getId(); }, $pagination->getItems()); return $this->render('ValantirForumBundle:Topic:show.html.twig', array('posts' => $pagination, 'postForm' => $this->isLogged() ? $postForm->createView() : false, 'topic' => $topic, 'page' => $this->request->get('page', 1), 'editingPost' => $editingPost, 'scroll' => $editingPost || $quotationPostId, 'votes' => $this->getPostVoteManager()->getVotesOfPosts($postsIds))); }
/** * Adds items to breadcrumb * * @param Forum|Topic|Post $object * @param string $lastText * * @return null - only if object is null */ public function generateBreadcrumb($object, $lastText = null) { if (!$object) { return null; } switch (true) { case $object instanceof Forum: $this->generateBreadcrumb($object->getParent()); $this->addItem('forum_index', $object); $this->addLastItem($lastText); break; case $object instanceof Topic: $this->generateBreadcrumb($object->getForum()); $this->addItem('topic_show', $object); $this->addLastItem($lastText); break; case $object instanceof Post: $this->generateBreadcrumb($object->getTopic()); $this->addLastItem($lastText); break; default: break; } }
/** * Adds post to collection of posts * * @param Post $post * * @return User */ public function addPost(Post $post) { $post->setAuthor($this); $this->posts->add($post); return $this; }
/** * Checks logged user is owner of voted post * * @param Post $post * * @return JsonResponse|RedirectResponse|null */ private function checkOwner(Post $post) { if ($this->getUser()) { if ($post->getAuthor()->getId() == $this->getUser()->getId()) { $message = $this->translator->trans('you.can.not.vote.for.your.posts'); $this->addFlash('danger', $message); if ($this->request->isXmlHttpRequest()) { return new JsonResponse(array('result' => false, 'message' => $message)); } return $this->redirect($this->generateUrl('topic_show', array('slug' => $post->getTopic()->getSlug()))); } } return null; }