/** * Booking Action * @param Request $request * @param Accommodation $accommodation * @return \Symfony\Component\HttpFoundation\RedirectResponse */ public function bookingAction(Request $request, Accommodation $accommodation) { $em = $this->getDoctrine()->getManager(); $session = $request->getSession(); try { # User $role = $em->getRepository('UserBundle:Role')->findOneByName('guest'); $unit = $em->getRepository('AppBundle:Unit')->find($request->request->get('unit')); $checkIn = new \DateTime($request->request->get('checkIn')); $checkOut = new \DateTime($request->request->get('checkOut')); $price = $request->request->get('price'); $bookingForm = $this->createForm(new RegistrationType(array())); $bookingForm->handleRequest($request); # Array with form fields $data = $bookingForm->getData(); if (!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) { $user = new User(); $user->setRoles($role); $user->setUsername($data['username']); $user->setName($data['name']); $user->setSurename($data['surename']); $user->setEmail($data['email']); $user->setPassword($data['password']); $user->setIsActive(0); $user->encryptPassword(); # User validation $validator = $this->get('validator'); $errors = $validator->validate($user); $errorsString = (string) $errors; if (count($errors) > 0) { $response = array(); $response['error'] = $errorsString; $session->getFlashBag()->add('formErrors', (string) $errorsString); return $this->redirect($this->generateUrl('app_accommodation_single', array('id' => $accommodation->getId()))); } } else { $user = $this->getUser(); } # Booking $booking = new Booking(); $booking->setUnit($unit); $booking->setStatus(0); $booking->setPrice($price); $booking->setUser($user); $booking->setFromDate($checkIn); $booking->setToDate($checkOut); # Booking validation $validator = $this->get('validator'); $errorsBooking = $validator->validate($booking); $errorsString = (string) $errorsBooking; if (count($errorsBooking) > 0) { $response = array(); $response['error'] = $errorsString; $session->getFlashBag()->add('formErrors', (string) $errorsString); return $this->redirect($this->generateUrl('app_accommodation_single', array('id' => $accommodation->getId()))); } # Booking not allowed if (!$this->get('bookingService')->bookingAllowed($booking)) { return $this->redirect($this->generateUrl('user_403')); } $em->persist($user); $em->flush(); $em->persist($booking); $em->flush(); $session->getFlashBag()->add('formSuccess', $this->get('translator')->trans('booking_success')); $this->bookingRequestEmail($accommodation->getUser(), $booking, 'host'); $this->bookingRequestEmail($user, $booking, 'guest'); if ($user->getIsActive()) { return $this->redirect($this->generateUrl('app_profile')); } else { return $this->redirect($this->generateUrl('app_home')); } } catch (ExportException $e) { $session->getFlashBag()->add('formError', $e->getMessage()); return $this->redirect($this->generateUrl('app_home')); } }
/** * Accommodation * @param Request $request * @param Accommodation $accommodation * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ public function step3Action(Request $request, Accommodation $accommodation) { $em = $this->getDoctrine()->getManager(); if ($accommodation->getUser() != $this->getUser()) { return $this->redirect($this->generateUrl('app_home')); } // price for form if (!is_null($accommodation->getWifi())) { $newPrice = $this->get("currencyService")->fromEuro($accommodation->getWifi()); $accommodation->setWifi(round($newPrice, 2)); } $form = $this->createForm(new AccommodationType($request->getLocale()), $accommodation)->remove('name')->remove('accommodationCategory')->remove('address')->remove('checkIn')->remove('checkOut')->remove('minimumStay')->remove('city')->remove('web')->remove('accommodationCategory')->remove('advertisingPackage')->remove('prepay')->remove('email')->remove('phone')->remove('fees'); $form->handleRequest($request); if ($form->isValid()) { # All POST data $postData = $request->request->all(); if (intval($postData['wifi']) == 0) { $accommodation->setWifi(null); } else { $accommodation->setWifi($this->get("currencyService")->toEuro($postData['accommodation']['wifi'])); } $em->persist($accommodation); $em->flush(); return $this->redirect($this->generateUrl('app_profile_step_4', array('id' => $accommodation->getId()))); } return $this->render('AppBundle:Profile/Steps:step3.html.twig', array('form' => $form->createView(), 'accommodation' => $accommodation)); }