public function postAction(Request $request) { /* @var $em \Doctrine\ORM\EntityManager */ $em = $this->get('doctrine')->getManager(); /* @var $user \AppBundle\Entity\User */ $user = $this->getUser(); if (!$user) { throw new UnauthorizedHttpException("You are not logged in."); } // a user cannot post more reviews than her reputation if (count($user->getReviews()) >= $user->getReputation()) { return new Response(json_encode("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 BadRequestHttpException("Unable to find card."); } if (!$card->getPack()->getDateRelease()) { return new Response(json_encode("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) { return new Response(json_encode("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) { return new Response(json_encode("Your review is empty.")); } $review = new Review(); $review->setCard($card); $review->setUser($user); $review->setRawtext($review_raw); $review->setText($review_html); $review->setnbVotes(0); $em->persist($review); $em->flush(); return new Response(json_encode(TRUE)); }
/** * Create new Review * @param Request $request * @param Accommodation $accommodation * @return \Symfony\Component\HttpFoundation\RedirectResponse */ public function reviewAddAction(Request $request, Accommodation $accommodation) { $em = $this->getDoctrine()->getManager(); $session = $request->getSession(); $review = new Review(); $reviewForm = $this->createForm(new ReviewType()); $reviewForm->handleRequest($request); if ($reviewForm->isValid()) { $data = $reviewForm->getData(); $total = array($data->getCleanliness(), $data->getComfort(), $data->getLocation(), $data->getFacilities(), $data->getStaff(), $data->getValueForMoney()); # If review is active - depanding on rate $total = array_sum($total) / 6; $status = $total < 4 ? 0 : 1; $review->setTotal($total); $review->setAccommodation($accommodation); $review->setTourist($this->getUser()); $review->setText($data->getText()); $review->setCleanliness($data->getCleanliness()); $review->setComfort($data->getComfort()); $review->setLocation($data->getLocation()); $review->setFacilities($data->getFacilities()); $review->setStaff($data->getStaff()); $review->setValueForMoney($data->getValueForMoney()); $review->setStatus($status); $em->persist($review); $em->flush(); # Calculate total accommodation rate with new review if ($status) { $this->get('AccommodationService')->calculateReviewRate($review); } return $this->redirect($this->generateUrl('app_accommodation_single', array('id' => $accommodation->getId()))); } $session->getFlashBag()->add('reviewFormErrors', (string) $reviewForm->getErrors(true, false)); return $this->redirect($this->generateUrl('app_accommodation_single', array('id' => $accommodation->getId()))); }