src/Controller/Admin/AppointmentCrudController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Appointment;
  4. use App\Entity\EventLog;
  5. use App\Form\AppointmentFileType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  9. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  10. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  11. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
  12. use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
  13. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  14. use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
  15. use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
  16. use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
  17. use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
  18. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  19. use EasyCorp\Bundle\EasyAdminBundle\Field\TelephoneField;
  20. use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
  21. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\Mailer\MailerInterface;
  26. use Symfony\Component\Mime\Email;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. class AppointmentCrudController extends AbstractCrudController
  30. {
  31.     public static function getEntityFqcn(): string
  32.     {
  33.         return Appointment::class;
  34.     }
  35.     public function configureCrud(Crud $crud): Crud
  36.     {
  37.         return $crud
  38.             ->setDateTimeFormat('dd/MM/yyyy HH:mm')
  39.             ->showEntityActionsAsDropdown()
  40.             ->setDefaultSort([
  41.                 'appointmentAt' => 'DESC',
  42.             ])
  43.             ->setSearchFields([
  44.                 'vehicleType.name',
  45.                 'card.cardNumber',
  46.                 'plateNumber',
  47.                 'phoneNumber',
  48.                 'email',
  49.             ])
  50.             ->setPaginatorPageSize(200);
  51.     }
  52.     /**
  53.      * @Route("/%easyadmin_url%/live", name="admin_dashboard_live")
  54.      * @IsGranted("ROLE_SIVEC_WORKER")
  55.      */
  56.     public function liveDashboard(): Response
  57.     {
  58.         /**
  59.          * "Live" features (without page refresh)
  60.          * Clock displaying current time
  61.          * Appointments list refresh (for instance if a new appointment is taken for the afternoon during the morning)
  62.          * Hide appointments after 30min of the initial datetime
  63.          */
  64.         return $this->render('admin/index_live.html.twig', [
  65.         ]);
  66.     }
  67.     public function configureActions(Actions $actions): Actions
  68.     {
  69.         $cancelAction Action::new('cancel''Cancel''fa fa-window-close')
  70.             ->linkToRoute('admin_appointment_cancel', function (Appointment $appointment) {
  71.                 return ['id' => $appointment->getId()];
  72.             })
  73.             ->addCssClass("btn btn-link pr-0 text-warning");
  74.         return $actions
  75.             ->add(Crud::PAGE_INDEXAction::DETAIL)
  76.             ->add(Crud::PAGE_INDEX$cancelAction)
  77.             ->add(Crud::PAGE_DETAIL$cancelAction)
  78.             ->setPermission(Action::NEW, 'ROLE_SIVEC_OPERATOR')
  79.             ->setPermission(Action::DELETE'ROLE_SUPER_ADMIN')
  80.             ->setPermission(Action::EDIT'ROLE_SIVEC_OPERATOR')
  81.             ->setPermission($cancelAction'ROLE_SIVEC_OPERATOR')
  82.             ->reorder(Crud::PAGE_INDEX, [Action::DETAILAction::EDIT'cancel'Action::DELETE]);
  83.     }
  84.     public function configureFields(string $pageName): iterable
  85.     {
  86.         return [
  87.             IdField::new('id''Id')->hideOnForm(),
  88.             AssociationField::new('vehicleType'),
  89.             AssociationField::new('card')
  90.                 ->setHelp('Card number'),
  91.             TextField::new('confirmationToken'),
  92.             TextField::new('plateNumber')
  93.                 ->formatValue(function ($value) {
  94.                     return strtoupper(str_replace(' '''$value));
  95.                 }),
  96.             DateTimeField::new('appointmentAt')
  97.                 ->renderAsNativeWidget(false),
  98.             TextEditorField::new('comment')->hideOnIndex(),
  99.             TelephoneField::new('phoneNumber'),
  100.             EmailField::new('email'),
  101.             DateTimeField::new('createdAt')->hideOnForm(),
  102.             TextField::new('statusAsText''Status')->hideOnForm(),
  103.             BooleanField::new('isMaximumLimitAccepted')
  104.                 ->setRequired(true)
  105.                 ->renderAsSwitch(false),
  106.             BooleanField::new('isConditionsAccepted')
  107.                 ->setRequired(true)
  108.                 ->renderAsSwitch(false),
  109.             BooleanField::new('isConfirmed')
  110.                 ->setRequired(true)
  111.                 ->renderAsSwitch(false),
  112.             CollectionField::new('appointmentFiles')
  113.                 ->setTemplatePath('admin/field/appointment_file.html.twig')
  114.                 ->onlyOnDetail(),
  115.         ];
  116.     }
  117.     public function deleteEntity(EntityManagerInterface $entityManager$entityInstance): void
  118.     {
  119.         /**
  120.          * @var Appointment $entityInstance
  121.          */
  122.         foreach ($entityInstance->getEventLogs() as $eventLog) {
  123.             $eventLog->setAppointment(null);
  124.         }
  125.         $eventLog = new EventLog();
  126.         $eventLog->setAction(EventLog::APPOINTMENT_DELETE);
  127.         $eventLog->setUser($this->getUser());
  128.         $entityManager->persist($eventLog);
  129.         parent::deleteEntity($entityManager$entityInstance);
  130.     }
  131.     /**
  132.      * @Route("/%easyadmin_url%/appointment/{id}/cancel", name="admin_appointment_cancel")
  133.      * @IsGranted("ROLE_SIVEC_OPERATOR")
  134.      * @param EntityManagerInterface $entityManager
  135.      * @param MailerInterface $mailer
  136.      * @param TranslatorInterface $translator
  137.      * @param $id
  138.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  139.      * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
  140.      */
  141.     public function cancelAppointment(Request $requestEntityManagerInterface $entityManagerMailerInterface $mailerTranslatorInterface $translator$id)
  142.     {
  143.         $appointment $entityManager->getRepository(Appointment::class)->find($id);
  144.         if ($appointment and $appointment->getStatus() !== Appointment::STATUS_CANCELLED) {
  145.             $appointment->setStatus(Appointment::STATUS_CANCELLED);
  146.             $appointment->setCancelDate(new \DateTime());
  147.             $eventLog = new EventLog();
  148.             $eventLog->setAction(EventLog::APPOINTMENT_CANCEL);
  149.             $eventLog->setAppointment($appointment);
  150.             $eventLog->setUser($this->getUser());
  151.             $entityManager->persist($eventLog);
  152.             $entityManager->flush();
  153.             $this->addFlash('success''Le rendez-vous a été annulé');
  154.             if ($appointment->getEmail()) {
  155.                 // Get email confirmation content
  156.                 $emailTemplate $this->renderView('mail/appointment_cancellation.html.twig', array(
  157.                     'appointment' => $appointment,
  158.                 ));
  159.                 $email         = (new Email())
  160.                     ->from('noreply@sivec.lu')
  161.                     ->to($appointment->getEmail())
  162.                     ->subject($translator->trans('email.appointment_cancellation.subject'))
  163.                     ->html($emailTemplate);
  164.                 $mailer->send($email);
  165.                 $this->addFlash('success''Un email de notification a été envoyé');
  166.             }
  167.         } else {
  168.             $this->addFlash('warning''Le rendez-vous a déjà été annulé ou n\'existe pas');
  169.         }
  170.         return $this->redirect($request->headers->get('referer'));
  171.     }
  172. }