/** * @Route("/cars/{id}", name="car_desc") * function is responsible for the car hire and send an email to the user with information about the loan. */ public function carDescriptionAction(Request $request, $id) { if ($this->isGranted('ROLE_MOD')) { switch ($request->query->get('action', 'none')) { case 'accept': if ($request->query->has('id')) { $order = $this->getDoctrine()->getRepository("AppBundle:Orders")->find($request->query->get('id')); if ($order !== null && $order->getRate() !== null && $order->getOpinion() !== null) { $order->setRateAccepted(true); $this->getDoctrine()->getManager()->flush(); } } break; case 'remove': if ($request->query->has('id')) { $order = $this->getDoctrine()->getRepository("AppBundle:Orders")->find($request->query->get('id')); if ($order !== null && $order->getRate() !== null && $order->getOpinion() !== null) { $order->setRateAccepted(false); $order->setOpinion(null); $order->setRate(null); $this->getDoctrine()->getManager()->flush(); } } break; } } $categories = $this->getDoctrine()->getRepository("AppBundle:Categories")->findAll(); $car = $this->getDoctrine()->getRepository("AppBundle:Cars")->find($id); $parameters = explode(';', $car->getParameters()); $category = $this->getDoctrine()->getRepository("AppBundle:Categories")->find($car->getCategoryId()); $lastOrder = $this->getDoctrine()->getRepository("AppBundle:Orders")->findBy(['carId' => $car->getId()], ['expDate' => 'DESC'], 1); $orders = $this->getDoctrine()->getRepository("AppBundle:Orders")->findBy(['carId' => $car]); $users = []; for ($i = 0; $i < count($orders); $i++) { if ($orders[$i]->getRate() !== null) { $users[$i] = $this->getDoctrine()->getRepository("AppBundle:Users")->find($orders[$i]->getUserId()); } } if (empty($lastOrder) || $lastOrder[0]->getExpDate() < new \DateTime('now')) { $available = true; } else { $available = false; } $order = new Orders(); $order->setCarId($car->getId()); $order->setDate(new \DateTime('now')); $nowDate = new \DateTime(); $format = 'Y-m-d'; if ($this->isGranted('IS_AUTHENTICATED_FULLY')) { $order->setUserId($this->getUser()->getId()); } $form = $this->createFormBuilder($order)->add('expDate', ChoiceType::class, ['label' => 'Do kiedy', 'attr' => ['class' => 'form-control'], 'choices' => ['Dni' => ['1 dzień' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1D'))->format($format)), '2 dni' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1D'))->format($format)), '3 dni' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1D'))->format($format)), '4 dni' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1D'))->format($format)), '5 dni' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1D'))->format($format)), '6 dni' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1D'))->format($format))], 'Tygodnie' => ['1 tydzień' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1D'))->format($format)), '2 tygodnie' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1W'))->format($format)), '3 tygodnie' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1W'))->format($format))], 'Miesiące' => ['1 miesiąc' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1W'))->format($format)), '2 miesiące' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1M'))->format($format)), '3 miesiące' => \DateTime::createFromFormat($format, $nowDate->add(new \DateInterval('P1M'))->format($format))]]])->add('add', SubmitType::class, ['label' => 'Potwierdź', 'attr' => ['class' => 'btn btn-primary']])->getForm(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($order); $em->flush(); if ($this->getUser()->getMail() !== null) { $mail = \Swift_Message::newInstance()->setSubject('Wiadomość od Symfony 3')->setFrom('*****@*****.**')->setTo($this->getUser()->getMail())->setBody($this->renderView('order_mail.html.twig', ['user' => $this->getUser()->getUsername(), 'car' => $car->getName(), 'date' => $order->getDate()->format('d.m.Y'), 'expDate' => $order->getExpDate()->format('d.m.Y'), 'price' => $car->getPrice()]), 'text/html'); $this->get('mailer')->send($mail); } return $this->redirectToRoute('orderSuccess'); } return $this->render("car_desc.html.twig", ['title' => $car->getName() . " - szczegóły", 'rate_title' => "Opinie", 'car' => $car, 'category' => $category, 'categories' => $categories, 'available' => $available, 'parameters' => $parameters, 'form' => $form->createView(), 'rates' => $orders, 'users' => $users]); }