public function resultAction(Request $request, $pickupCity, $returnCity, $pickupDate, $returnDate) { $session = $this->get('session'); $query = new SearchQuery(); if ($session->has('pickupCity') && $session->get('pickupCity')->getName() == $pickupCity && ($session->has('returnCity') && $session->get('returnCity')->getName() == $returnCity) && ($session->has('pickupDateTime') && $session->get('pickupDateTime') == \DateTime::createFromFormat('d-m-Y-H-i', $pickupDate)) && ($session->has('returnDateTime') && $session->get('returnDateTime') == \DateTime::createFromFormat('d-m-Y-H-i', $returnDate))) { $query->setPickupCity($this->getDoctrine()->getRepository('AppBundle:City')->findOneByName($session->get('pickupCity')->getName())); $query->setReturnCity($this->getDoctrine()->getRepository('AppBundle:City')->findOneByName($session->get('returnCity')->getName())); $query->setPickupDateTime($session->get('pickupDateTime')); $query->setReturnDateTime($session->get('returnDateTime')); } else { if ($this->getDoctrine()->getRepository('AppBundle:City')->findOneByName($pickupCity) == null || $this->getDoctrine()->getRepository('AppBundle:City')->findOneByName($returnCity) == null || \DateTime::createFromFormat('d-m-Y-H-i', $pickupDate) <= new \DateTime('+ 2 hour') || \DateTime::createFromFormat('d-m-Y-H-i', $returnDate) <= new \DateTime('+ 3 hour') || \DateTime::createFromFormat('d-m-Y-H-i', $pickupDate) >= \DateTime::createFromFormat('d-m-Y-H-i', $returnDate)) { $error = "Invalid search!"; $query->setPickupDateTime(new \DateTime('+ 3 hour')); $query->setReturnDateTime(new \DateTime('+ 4 hour')); $form = $this->createForm(SearchQueryType::class, $query); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); $session->set('pickupCity', $data->getPickupCity()); $session->set('returnCity', $data->getReturnCity()); $session->set('pickupDateTime', $data->getPickupDateTime()); $session->set('returnDateTime', $data->getReturnDateTime()); return $this->redirectToRoute('results', array('pickupCity' => $data->getPickupCity()->getName(), 'returnCity' => $data->getReturnCity()->getName(), 'pickupDate' => $data->getPickupDateTime()->format('d-m-Y-H-i'), 'returnDate' => $data->getReturnDateTime()->format('d-m-Y-H-i')), 301); } return $this->render('AppBundle:default:results.html.twig', array('form' => $form->createView(), 'error' => $error)); } else { $session->set('pickupCity', $this->getDoctrine()->getRepository('AppBundle:City')->findOneByName($pickupCity)); $session->set('returnCity', $this->getDoctrine()->getRepository('AppBundle:City')->findOneByName($returnCity)); $session->set('pickupDateTime', \DateTime::createFromFormat('d-m-Y-H-i', $pickupDate)); $session->set('returnDateTime', \DateTime::createFromFormat('d-m-Y-H-i', $returnDate)); $query->setPickupCity($this->getDoctrine()->getRepository('AppBundle:City')->findOneByName($session->get('pickupCity')->getName())); $query->setReturnCity($this->getDoctrine()->getRepository('AppBundle:City')->findOneByName($session->get('returnCity')->getName())); $query->setPickupDateTime($session->get('pickupDateTime')); $query->setReturnDateTime($session->get('returnDateTime')); } } $form = $this->createForm(SearchQueryType::class, $query); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); $session->set('pickupCity', $data->getPickupCity()); $session->set('returnCity', $data->getReturnCity()); $session->set('pickupDateTime', $data->getPickupDateTime()); $session->set('returnDateTime', $data->getReturnDateTime()); return $this->redirectToRoute('results', array('pickupCity' => $data->getPickupCity()->getName(), 'returnCity' => $data->getReturnCity()->getName(), 'pickupDate' => $data->getPickupDateTime()->format('d-m-Y-H-i'), 'returnDate' => $data->getReturnDateTime()->format('d-m-Y-H-i')), 301); } $cars = $this->getDoctrine()->getRepository('AppBundle:Car')->findAllAvailable($session->get('pickupDateTime'), $session->get('returnDateTime')); $count = count($cars); $diff = date_diff($session->get('pickupDateTime'), $session->get('returnDateTime')); $hours = $diff->h; $hours = $hours + $diff->days * 24; $hours = $hours + $diff->i / 60; return $this->render('AppBundle:default:results.html.twig', array('form' => $form->createView(), 'count' => $count, 'cars' => $cars, 'hours' => $hours)); }
public function resultAction(Request $request, $pickupCity, $returnCity, $pickupDate, $returnDate) { $session = $this->get('session'); $query = new SearchQuery(); /*Repopulate form */ $query->setPickupCity($session->get('pickupCity')); $query->setReturnCity($session->get('returnCity')); $query->setPickupDateTime($session->get('pickupDateTime')); $query->setReturnDateTime($session->get('returnDateTime')); /*Trying to access not through search route */ if ($query->getPickupCity() == null || $query->getReturnCity() == null || $query->getPickupDateTime() == null || $query->getReturnDateTime() == null) { return $this->redirectToRoute('results_error'); } /*Trying to access not through search route */ if ($query->getPickupCity() != $pickupCity || $query->getReturnCity() != $returnCity || $query->getPickupDateTime() != \DateTime::createFromFormat('d-m-Y-H-i', $pickupDate) || $query->getReturnDateTime() != \DateTime::createFromFormat('d-m-Y-H-i', $returnDate)) { return $this->redirectToRoute('results_error'); } $form = $this->createForm(SearchQueryType::class, $query); /*Handle new search query */ $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $session = $this->get('session'); $data = $form->getData(); $session->set('pickupCity', $data->getPickupCity()); $session->set('returnCity', $data->getReturnCity()); $session->set('pickupDateTime', $data->getPickupDateTime()); $session->set('returnDateTime', $data->getReturnDateTime()); return $this->redirectToRoute('results', array('pickupCity' => $data->getPickupCity(), 'returnCity' => $data->getReturnCity(), 'pickupDate' => $data->getPickupDateTime()->format('d-m-Y-H-i'), 'returnDate' => $data->getReturnDateTime()->format('d-m-Y-H-i')), 301); } /*Display resutls logic */ $car = new Car(); $car->setSlug('fiat-126p'); $car->setModel('Fiat 126p'); $car->setClassId(1); $car->setNumberOfDoors(2); $car->setNumberOfSeats(4); $car->setTransmissionId(1); $car->setPricePerHour(30); return $this->render('AppBundle:default:results.html.twig', array('form' => $form->createView(), 'car' => $car)); }