/** * Render comments reqursively */ private function renderComments($comments, $parent = null) { $content = ''; foreach ($comments as $comment) { if ($comment->getParent() == $parent) { $newComment = new Comment(); $newComment->setParent($comment); $form = $this->kernel->getContainer()->get('form.factory')->create(new CommentType(), $newComment); $form->get('post')->setData($comment->getPost()); $data['form'] = $form->createView(); $data['parent'] = $comment; $data['form_id'] = 'form-comment-' . $comment->getId(); $comment->form = $this->kernel->getContainer()->get('templating')->render('content/blog/comment/form.html', $data); $comment->childs = $this->renderComments($comments, $comment); $data['comment'] = $comment; $content .= '<li>' . $this->kernel->getContainer()->get('templating')->render('content/blog/comment/index.html', $data) . '</li>'; } } if ($content !== '') { $content = '<ol>' . $content . '</ol>'; } return $content; }
public function commentSubmitAction(Request $request) { $referer = $request->server->get('HTTP_REFERER'); $requestedComment = $request->request->get('blog_comment'); if ($parentId = $requestedComment['parent']) { $parent = $this->getDoctrine()->getManager()->getRepository('DrafterbitBlogBundle:Comment')->find($parentId); } else { $parent = null; } $newComment = new Comment(); $newComment->setParent($parent); $form = $this->createForm(new CommentType(), $newComment); $form->handleRequest($request); if ($form->isValid()) { $comment = $form->getData(); $comment->setCreatedAt(new \DateTime()); $comment->setUpdatedAt(new \DateTime()); $comment->setDeletedAt(NULL); // @todo status if ($this->get('system')->get('blog.comment_moderation')) { $comment->setStatus(0); } else { $comment->setStatus(1); } $em = $this->getDoctrine()->getManager(); $em->persist($comment); $em->flush(); $this->sendMails($comment); if ($this->get('system')->get('blog.comment_moderation')) { $data['post_url'] = $referer; return $this->render('DrafterbitBlogBundle:Comment:pending.html.twig', $data); } return new RedirectResponse($referer . '#comment-' . $comment->getId()); } else { $errors = []; // @todo clean this, make a recursive // create service, FormErrorExtractor maybe $formView = $form->createView(); foreach ($formView as $inputName => $view) { if (isset($view->vars['errors'])) { foreach ($view->vars['errors'] as $error) { $errors[$view->vars['label']] = $error->getMessage(); } } } $data['post_url'] = $referer; $data['errors'] = $errors; $content = $this->renderView('DrafterbitBlogBundle:Comment:error.html.twig', $data); return new Response($content); } }
/** * @Route("/blog/comment/quick-reply", name="drafterbit_blog_comment_quickreply") */ public function quickReply(Request $request) { $postId = $request->request->get('postId'); $content = $request->request->get('comment'); $parentId = $request->request->get('parentId'); $em = $this->getDoctrine()->getManager(); $post = $em->getRepository('DrafterbitBlogBundle:Post')->find($postId); $parent = $em->getRepository('DrafterbitBlogBundle:Comment')->find($parentId); $comment = new Comment(); $author = $this->getUser(); $comment->setAuthorName($author->getRealName()); $comment->setAuthorEmail($author->getEmail()); $comment->setAuthorUrl($author->getUrl()); $comment->setContent($content); $comment->setPost($post); $comment->setParent($parent); $comment->setCreatedAt(new \DateTime()); $comment->setUpdatedAt(new \DateTime()); $comment->setDeletedAt(NULL); $comment->setStatus(Comment::STATUS_APPROVED); $comment->setSubscribe(0); $em->persist($comment); $em->flush(); return new JsonResponse(['msg' => 'Comment saved', 'status' => 'success']); }