src/Controller/PlanningController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Appointment;
  4. use App\Form\AppointmentType;
  5. use App\Form\UndoMessageType;
  6. use App\Form\UndoType;
  7. use App\Repository\AppointmentDateRepository;
  8. use App\Repository\AppointmentRepository;
  9. use App\Repository\ParametersRepository;
  10. use App\Repository\TimeSlotRepository;
  11. use App\Service\AppointmentChecker;
  12. use App\Service\AppointmentInfosMaker;
  13. use App\Service\Calendar\Calendar;
  14. use App\Service\Mailer\Mailer;
  15. use App\Service\Tools\AppointmentInfosVerificator;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\Session\Session;
  20. use Symfony\Component\Messenger\MessageBusInterface;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. class PlanningController extends AbstractController
  23. {
  24.     /**
  25.      * @throws \Exception
  26.      */
  27.     #[Route('/planning/{reservation}'name'planning')]
  28.     public function planning(
  29.         Request $request,
  30.         AppointmentDateRepository $dateRepository,
  31.         AppointmentRepository $appointmentRepository,
  32.         TimeSlotRepository $timeSlotRepository,
  33.         AppointmentChecker $checker,
  34.         AppointmentInfosMaker $appointmentInfosMaker,
  35.         Mailer $mailer,
  36.         Calendar $calendar,
  37.         string $reservation null
  38.     ): Response {
  39.         if($reservation) {
  40.             $reservation $appointmentRepository->findOneBy([
  41.                 'id' => base64_decode($reservation)
  42.             ]);
  43.         }
  44.         $dates $dateRepository->findPlanning();
  45.         $appointment = new Appointment();
  46.         $form $this->createForm(AppointmentType::class, $appointment);
  47.         $form->handleRequest($request);
  48.         $undoForm $this->createForm(UndoMessageType::class);
  49.         $undoForm->handleRequest($request);
  50.         if($undoForm->isSubmitted() && $undoForm->isValid()) {
  51.             $undoAppointment $appointmentRepository->findOneById($undoForm->get('appointment')->getData());
  52.             if($undoAppointment) {
  53.                 $mailer->sendUndoMessage($undoAppointment$undoForm->get('message')->getData());
  54.                 if($undoAppointment->getLinkedTo()) {
  55.                     $linked $undoAppointment->getLinkedTo();
  56.                     $linked->setLinkedTo(null);
  57.                     $undoAppointment->setLinkedTo(null);
  58.                     $appointmentRepository->remove($linkedtrue);
  59.                 }
  60.                 $appointmentRepository->remove($undoAppointmenttrue);
  61.                 $this->addFlash('success''Le rendez-vous a bien été annulé.');
  62.                 return $this->redirectToRoute('planning', [
  63.                 ]);
  64.             }
  65.         }
  66.         if($form->isSubmitted() && $form->isValid()) {
  67.             $appointmentInfosMaker->addForm($request->request->All()['appointment']);
  68.             $infos $appointmentInfosMaker->getInfos();
  69.             if(count($infos) === && $appointmentInfosMaker->shouldHaveChildrenButHaveNot()) {
  70.                 $this->addFlash('error''Des informations sont manquantes. Merci de renouveler votre demande');
  71.                 return $this->redirectToRoute('planning', [
  72.                 ]);
  73.             }
  74.             if(!AppointmentInfosVerificator::isValid($infos)) {
  75.                 $this->addFlash('error''Vous n\'avez pas rempli tous les champs requis');
  76.                 return $this->redirectToRoute('planning', [
  77.                 ]);
  78.             }
  79.             if(!$checker->canReserve($form->get('email')->getData())) {
  80.                 $this->addFlash('error''Vous avez déjà un rendez-vous dans les jours à venir. Réservation impossible');
  81.                 return $this->redirectToRoute('planning', [
  82.                 ]);
  83.             }
  84.             if($form->get('slot')->getData() && $checker->canReserve($form->get('email')->getData())) {
  85.                 $slot $timeSlotRepository->findOneById($form->get('slot')->getData());
  86.                 if($slot->getAppointment()) {
  87.                     $success false;
  88.                     $this->addFlash('error''Désolé, ce créneau a déjà été pris entre temps');
  89.                 }
  90.                 if(!$slot->getAppointment()) {
  91.                     $appointment->setTimeSlot($slot);
  92.                     $appointment->setInfos($infos);
  93.                     $appointment->setCreatedAt(new \DateTimeImmutable('now', new \DateTimeZone('Europe/Paris')));
  94.                     $appointmentRepository->save($appointmenttrue);
  95.                     $this->addFlash('success''Votre demande a bien été enregistrée. Vous allez recevoir une confirmation par email');
  96.                     $success true;
  97.                     if ($checker->needsMultipleTimeSlots($appointment)) {
  98.                         $nextSlot $checker->getNextTimeSlot($appointment);
  99.                         if ($nextSlot && !$nextSlot->getAppointment()) {
  100.                             $nextAppointment = clone($appointment);
  101.                             $nextAppointment->setTimeSlot($nextSlot);
  102.                             $nextAppointment->setLinkedTo($appointment);
  103.                             $appointmentRepository->save($nextAppointmenttrue);
  104.                             $appointment->setLinkedTo($nextAppointment);
  105.                             $appointment->setIsMain(true);
  106.                             $appointmentRepository->save($appointmenttrue);
  107.                             $success true;
  108.                             $this->addFlash('success''Compte tenu des informations que vous nous avez fourni, nous vous avons réservé deux créneaux pour avoir le temps de vous accueillir convenablement.');
  109.                         } else {
  110.                             $appointmentRepository->remove($appointmenttrue);
  111.                             $this->addFlash(
  112.                                 'error',
  113.                                 'D\'après les informations que vous nous avez fourni, 
  114.                                     il semble qu\'il faille deux créneaux pour traiter convenablement votre demande. 
  115.                                     Hors il n\'y a pas de créneau suivant disponible. Merci de choisir un moment ou deux créneaux consécutifs sont disponibles.');
  116.                             $success false;
  117.                         }
  118.                     }
  119.                 }
  120.                 if($success) {
  121.                     $mailer->sendRegisterEmail($appointment);
  122.                 }
  123.                 return $this->redirectToRoute('planning', [
  124.                     'reservation' => base64_encode($appointment->getId()),
  125.                 ]);
  126.             }
  127.         }
  128.         return $this->render('admin/planning.html.twig', [
  129.             'dates' => $dates,
  130.             'form'  => $form->createView(),
  131.             'reservation' => $reservation,
  132.             'undoForm' => $undoForm->createView(),
  133.         ]);
  134.     }
  135.     #[Route('/undo_appointment/{id}'name:'undo_appointment'methods: ['GET''POST'])]
  136.     public function undoAppointment(
  137.         AppointmentRepository $appointmentRepository,
  138.         ParametersRepository $parametersRepository,
  139.         Mailer $mailer,
  140.         Request $request,
  141.         Appointment $appointment null
  142.     ) {
  143.         $params $parametersRepository->findOneBy([]);
  144.         if(!$appointment) {
  145.             $this->addFlash('error''Ce rendez-vous n\'existe pas ou a déjà été annulé');
  146.             return $this->redirectToRoute('planning');
  147.         }
  148.         $form $this->createForm(UndoType::class);
  149.         $form->handleRequest($request);
  150.         if(!$form->isSubmitted()) {
  151.             $code strtoupper(substr(uniqid(), -5));
  152.             $appointment->setUndoCode($code);
  153.             $appointmentRepository->save($appointmenttrue);
  154.             $mailer->sendUndoCode($appointment);
  155.         }
  156.         if($form->isSubmitted() && $form->isValid()) {
  157.             if($form->get('code')->getData() === $appointment->getUndoCode()) {
  158.                 if($appointment->getLinkedTo()) {
  159.                     $linked $appointment->getLinkedTo();
  160.                     $linked->setLinkedTo(null);
  161.                     $appointment->setLinkedTo(null);
  162.                     $appointmentRepository->remove($linkedtrue);
  163.                 }
  164.                 if($params->isWantsToreceiveEmailOnUndo()) {
  165.                     $mailer->sendUndoNotification($appointment);
  166.                 }
  167.                 $appointmentRepository->remove($appointmenttrue);
  168.                 $this->addFlash('success''Votre rendez-vous a bien été annulé');
  169.                 return $this->redirectToRoute('planning');
  170.             } else {
  171.                 $this->addFlash('error''Code erroné');
  172.             }
  173.         }
  174.         return $this->render('planning/undo.html.twig', [
  175.             'appointment' => $appointment,
  176.             'form' => $form->createView()
  177.         ]);
  178.     }
  179.     #[Route('/search'name'search'methods: ['POST'])]
  180.     public function search(Request $requestAppointmentRepository $appointmentRepository)
  181.     {
  182.         $search $request->request->get('search');
  183.         $isGlobal $request->request->get('global') === '1';
  184.         $appointments $isGlobal $appointmentRepository->globalSearch($search) : $appointmentRepository->search($search);
  185.         $data = [];
  186.         /** @var Appointment $appointment */
  187.         foreach ($appointments as $appointment) {
  188.             if(!$appointment->getLinkedTo() || ($appointment->getLinkedTo() && $appointment->isIsMain())) {
  189.                 $data[] = $appointment;
  190.             }
  191.         }
  192.         return $this->render('admin/_search_results.html.twig', [
  193.             'appointments' => $data,
  194.             'is_global'    => $isGlobal,
  195.         ]);
  196.     }
  197. }