/** * @param Request $request * @param Product $product * @Route("/{slug}", name="create_review") * @Method("POST") * @Security("has_role('ROLE_USER')") * @Template("AppBundle:shop/Review:form.html.twig") * @return array|\Symfony\Component\HttpFoundation\RedirectResponse */ public function createAction(Request $request, Product $product) { $user = $this->getUser(); $Review = new Review(); $Review->setProduct($product); $Review->setUser($user); $form = $this->createForm(ReviewType::class, $Review, ['action' => $this->generateUrl('create_review', ['slug' => $product->getSlug()]), 'method' => 'POST'])->add('save', SubmitType::class, ['label' => 'review.send']); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($Review); $em->flush(); return $this->redirect($this->generateUrl('product_view', ['slug' => $product->getSlug(), 'tab' => 'reviews'])); } return ['form' => $form->createView()]; }
public function postAction(Request $request) { /* @var $em \Doctrine\ORM\EntityManager */ $em = $this->getDoctrine()->getManager(); /* @var $user \AppBundle\Entity\User */ $user = $this->getUser(); if (!$user) { throw $this->createAccessDeniedException("You are not logged in."); } // a user cannot post more reviews than her reputation if (count($user->getReviews()) >= $user->getReputation()) { throw new \Exception("Your reputation doesn't allow you to write more reviews."); } $card_id = filter_var($request->get('card_id'), FILTER_SANITIZE_NUMBER_INT); /* @var $card Card */ $card = $em->getRepository('AppBundle:Card')->find($card_id); if (!$card) { throw new \Exception("This card does not exist."); } if (!$card->getPack()->getDateRelease()) { throw new \Exception("You may not write a review for an unreleased card."); } // checking the user didn't already write a review for that card $review = $em->getRepository('AppBundle:Review')->findOneBy(array('card' => $card, 'user' => $user)); if ($review) { throw new \Exception("You cannot write more than 1 review for a given card."); } $review_raw = trim($request->get('review')); $review_raw = preg_replace('%(?<!\\()\\b(?:(?:https?|ftp)://)(?:((?:(?:[a-z\\d\\x{00a1}-\\x{ffff}]+-?)*[a-z\\d\\x{00a1}-\\x{ffff}]+)(?:\\.(?:[a-z\\d\\x{00a1}-\\x{ffff}]+-?)*[a-z\\d\\x{00a1}-\\x{ffff}]+)*(?:\\.[a-z\\x{00a1}-\\x{ffff}]{2,6}))(?::\\d+)?)(?:[^\\s]*)?%iu', '[$1]($0)', $review_raw); $review_html = $this->get('texts')->markdown($review_raw); if (!$review_html) { throw new \Exception("Your review is empty."); } $review = new Review(); $review->setCard($card); $review->setUser($user); $review->setTextMd($review_raw); $review->setTextHtml($review_html); $review->setNbVotes(0); $em->persist($review); $em->flush(); return new JsonResponse(['success' => TRUE]); }