/**
  * List comments made on a product
  *
  * @param Request        $request
  * @param integer|string $id
  *
  * @AclAncestor("pim_enrich_product_comment")
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function listCommentsAction(Request $request, $id)
 {
     $product = $this->findProductOr404($id);
     $comment = $this->commentBuilder->buildComment($product, $this->getUser());
     $createForm = $this->formFactory->create('pim_comment_comment', $comment);
     $comments = $this->commentManager->getComments($product);
     $replyForms = [];
     foreach ($comments as $comment) {
         $reply = $this->commentBuilder->buildReply($comment, $this->getUser());
         $replyForm = $this->formFactory->create('pim_comment_comment', $reply, ['is_reply' => true]);
         $replyForms[$comment->getId()] = $replyForm->createView();
     }
     return $this->templating->renderResponse('PimCommentBundle:Comment:_commentList.html.twig', ['createForm' => $createForm->createView(), 'replyForms' => $replyForms, 'comments' => $comments]);
 }
 /**
  * Reply to a comment
  *
  * @param Request $request
  *
  * @throws \LogicException
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function replyAction(Request $request)
 {
     if (true !== $request->isXmlHttpRequest()) {
         throw new \LogicException('The request should be an Xml Http request.');
     }
     $reply = $this->commentBuilder->newInstance();
     $replyForm = $this->formFactory->create('pim_comment_comment', $reply, ['is_reply' => true]);
     $replyForm->submit($request);
     if (true !== $replyForm->isValid()) {
         return new JsonResponse('The form is not valid.', 400);
     }
     $now = new \DateTime();
     $reply->setCreatedAt($now);
     $reply->setRepliedAt($now);
     $reply->setAuthor($this->getUser());
     $comment = $reply->getParent();
     $comment->setRepliedAt($now);
     $this->commentManager->save($reply);
     return $this->templating->renderResponse('PimCommentBundle:Comment:_thread.html.twig', ['replyForms' => [$comment->getId() => $replyForm->createView()], 'comment' => $comment]);
 }
 /**
  * Reply to a product comment
  *
  * @param Request $request
  * @param string  $id
  * @param string  $commentId
  *
  * @return JsonResponse
  */
 public function postReplyAction(Request $request, $id, $commentId)
 {
     $product = $this->findProductOr404($id);
     $data = json_decode($request->getContent(), true);
     $data['parent'] = $commentId;
     $reply = $this->commentBuilder->buildComment($product, $this->getUser());
     $form = $this->formFactory->create('pim_comment_comment', $reply, ['is_reply' => true, 'csrf_protection' => false]);
     $form->submit($data, false);
     if ($form->isValid()) {
         $now = new \DateTime();
         $reply->setCreatedAt($now);
         $reply->setRepliedAt($now);
         $comment = $reply->getParent();
         $comment->setRepliedAt($now);
         $this->commentSaver->save($reply);
         return new JsonResponse($this->normalizer->normalize($reply, 'json'));
     }
     $violations = $this->validator->validate($reply);
     $errors = [];
     foreach ($violations as $violation) {
         $errors[$violation->getPropertyPath()] = ['message' => $violation->getMessage(), 'invalid_value' => $violation->getInvalidValue()];
     }
     return new JsonResponse($errors, 400);
 }