src/Controller/VacationController.php line 265

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\DemandeVacation;
  4. use App\Entity\Employee;
  5. use App\Entity\Poste;
  6. use App\Entity\PosteDirector;
  7. use App\Entity\ResNotification;
  8. use App\Entity\Vacation;
  9. use App\Form\VacationType;
  10. use App\Repository\EmployeeRepository;
  11. use App\Repository\LocaliteRepository;
  12. use App\Repository\PosteRepository;
  13. use App\Repository\ResListeDeChoixRepository;
  14. use App\Repository\VacationRepository;
  15. use App\Utils\Consts;
  16. use DateInterval;
  17. use DateTime;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Exception;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\JsonResponse;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  27. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  28. use Symfony\Component\Security\Core\Security;
  29. use Symfony\Component\Translation\TranslatableMessage;
  30. use Symfony\Component\Workflow\WorkflowInterface;
  31. use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
  32. /**
  33.  * @Route("/vacation")
  34.  */
  35. class VacationController extends AbstractController
  36. {
  37.     private function setBaseBreadcrumb(Breadcrumbs $breadcrumbs)
  38.     {
  39.         $breadcrumbs->addRouteItem("Accueil"'index');
  40.         $breadcrumbs->addItem("Vacations",'/vacation');
  41.     }
  42.     /**
  43.      * @Route("/", name="app_vacation_index", methods={"GET"})
  44.      */
  45.     public function index(VacationRepository $vacationRepository,Security $security,Request $request,Breadcrumbs $breadcrumbs): Response
  46.     {
  47.         $this->setBaseBreadcrumb($breadcrumbs);
  48.         $route $request->attributes->get('_route');
  49.         $vacation = new Vacation();
  50.         $this->denyAccessUnlessGranted('VIEW'$vacation);
  51.         $breadcrumbs->addItem($route);
  52.         return $this->render('vacation/index.html.twig', [
  53.             'vacations' => $vacationRepository->findAll(),
  54.         ]);
  55.     }
  56. //    /**
  57. //     * @Route("/new/{id}", name="app_vacation_new", methods={"GET", "POST"})
  58. //     */
  59. //    public function new(Poste $poste, VacationRepository $vacationRepository,Request $request,Breadcrumbs $breadcrumbs): Response
  60. //    {
  61. //        $this->setBaseBreadcrumb($breadcrumbs);
  62. //        $breadcrumbs->addRouteItem("poste", 'app_poste_show', ['id' => $poste->getId()]);
  63. //        $route = $request->attributes->get('_route');
  64. //        $breadcrumbs->addItem($route);
  65. //
  66. //        $tokenStorage = $this->container->get('security.token_storage');
  67. //        $userConnected = $tokenStorage->getToken()->getUser();
  68. //
  69. //        $userPosteDirector = $poste->getPosteDirector()->getResponsable()->getPartner()->getUserAccount();
  70. //        $userResponsable = $poste->getResponsable()->getPartner()->getUserAccount();
  71. //
  72. //        $vacation = new Vacation();
  73. //        $vacation->setPoste($poste);
  74. //        $form = $this->createForm(VacationType::class, $vacation);
  75. //        $form->handleRequest($request);
  76. //
  77. //        if ($form->isSubmitted() && $form->isValid()) {
  78. //
  79. //            if (($userConnected == $userResponsable) || ($userConnected == $userPosteDirector)){
  80. //                if ($poste->getActive()) {
  81. //                    $vacation->setVacationDateTime(new \DateTime('now'));
  82. //                    $vacationRepository->add($vacation, true);
  83. //                }else{
  84. //                    $this->addFlash(
  85. //                        Consts::FLASH_MESSAGE_WARNING,
  86. //                        new TranslatableMessage('Vous ne pouvez pas enregistrer des vacations ! Ce poste est désactivé !')
  87. //                    );
  88. //                }
  89. //            }else{
  90. //                $this->addFlash(
  91. //                    Consts::FLASH_MESSAGE_WARNING,
  92. //                    new TranslatableMessage('Vous ne pouvez pas enregistrer des vacations pour ce poste !')
  93. //                );
  94. //            }
  95. //
  96. //            return $this->redirectToRoute('app_poste_show', ['id'=>$poste->getId()], Response::HTTP_SEE_OTHER);
  97. //        }
  98. //
  99. //        return $this->renderForm('vacation/new.html.twig', [
  100. //            'vacation' => $vacation,
  101. //            'form' => $form,
  102. //            'poste' => $poste,
  103. //        ]);
  104. //    }
  105.     /**
  106.      * @Route("/{id}", name="app_vacation_show", methods={"GET"})
  107.      */
  108.     public function show(Vacation $vacation,Request $request,Breadcrumbs $breadcrumbs): Response
  109.     {
  110.         $this->denyAccessUnlessGranted('VIEW'$vacation);
  111.         $this->setBaseBreadcrumb($breadcrumbs);
  112.         $breadcrumbs->addRouteItem("poste"'app_poste_show', ['id' => $vacation->getPoste()->getId()]);
  113.         $route $request->attributes->get('_route');
  114.         $breadcrumbs->addItem($route);
  115.         $options = ['action' => Consts::ACTION_SHOW];
  116.         $form $this->createForm(VacationType::class, $vacation,$options);
  117.         $editMode false;
  118.         return $this->render('vacation/edit.html.twig', [
  119.             'vacation' => $vacation,
  120.             'form'=>$form->createView(),
  121.             'editMode'=>$editMode
  122.         ]);
  123.     }
  124.     /**
  125.      * @Route("/{id}/edit", name="app_vacation_edit", methods={"GET", "POST"})
  126.      */
  127.     public function edit(Vacation $vacationVacationRepository $vacationRepository,Request $request,Breadcrumbs $breadcrumbs): Response
  128.     {
  129.         $this->denyAccessUnlessGranted('EDIT'$vacation);
  130.         $tokenStorage $this->container->get('security.token_storage');
  131.         $userConnected $tokenStorage->getToken()->getUser();
  132.         $userPosteDirector $vacation->getPoste()->getPosteDirector()->getResponsable()->getPartner()->getUserAccount();
  133.         $userResponsable $vacation->getPoste()->getResponsable()->getPartner()->getUserAccount();
  134.         $editMode true;
  135.         $options = ['action' => Consts::ACTION_EDIT];
  136.         $form $this->createForm(VacationType::class, $vacation$options);
  137.         $this->setBaseBreadcrumb($breadcrumbs);
  138.         $route $request->attributes->get('_route');
  139.         $breadcrumbs->addRouteItem("vacation"'app_vacation_show', ['id' => $vacation->getId()]);
  140.         $breadcrumbs->addItem($route);
  141.         if ($this->vacationWorkflow->can($vacationConsts::TRANSIION_to_accepted)) {
  142.             if (($userConnected == $userResponsable) || ($userConnected == $userPosteDirector)) {
  143.                 if ($vacation->getPoste()->getActive()) {
  144.                     $form->handleRequest($request);
  145.                     if ($form->isSubmitted() && $form->isValid()) {
  146.                         $vacationRepository->add($vacationtrue);
  147.                         return $this->redirectToRoute('app_vacation_show', ['id' => $vacation->getId()], Response::HTTP_SEE_OTHER);
  148.                     }
  149.                 }
  150.                 else{
  151.                     $this->addFlash(
  152.                         Consts::FLASH_MESSAGE_WARNING,
  153.                         new TranslatableMessage('Vous ne pouvez pas modifier cette vacation ! Ce poste est désactivé !')
  154.                     );
  155.                 }
  156.             }else{
  157.                 $this->addFlash(
  158.                     Consts::FLASH_MESSAGE_WARNING,
  159.                     new TranslatableMessage('Vous ne pouvez pas modifier cette vacation !')
  160.                 );
  161.             }
  162.         } else {
  163.             $this->addFlash(
  164.                 Consts::FLASH_MESSAGE_WARNING,
  165.                 new TranslatableMessage('Vous ne pouvez pas modifier cette vacation !')
  166.             );
  167.             return $this->redirectToRoute('app_vacation_show', ['id'=>$vacation->getId()], Response::HTTP_SEE_OTHER);
  168.         }
  169.         return $this->renderForm('vacation/edit.html.twig', [
  170.             'vacation' => $vacation,
  171.             'form' => $form,
  172.             'editMode' => $editMode
  173.         ]);
  174.     }
  175.     /**
  176.      * @Route("/{id}", name="app_vacation_delete", methods={"POST"})
  177.      */
  178.     public function delete(Request $requestVacation $vacationVacationRepository $vacationRepository): Response
  179.     {
  180.         if ($this->isCsrfTokenValid('delete'.$vacation->getId(), $request->request->get('_token'))) {
  181.             $vacationRepository->remove($vacationtrue);
  182.         }
  183.         return $this->redirectToRoute('app_vacation_index', [], Response::HTTP_SEE_OTHER);
  184.     }
  185.     /**
  186.      * @Route("/send/notification", name="app_send_notification", methods={"POST"})
  187.      */
  188.     public function demanderVacation(Request $requestEntityManagerInterface $entityManagerEmployeeRepository $employeeRepository,PosteRepository $posteRepositoryUrlGeneratorInterface $router){
  189.         $posteId $request->request->get('posteId');
  190.         $startHour $request->request->get('startHour');
  191.         $endhour $request->request->get('endhour');
  192.         $poste $posteRepository->find($posteId);
  193.         $responsable $poste->getResponsable();
  194.         //notification
  195.         $notificationHiearchie = new ResNotification();
  196.         $notificationHiearchie->setTypeNotification(Consts::NOTIFICATION_DEMANDE_VACATION);
  197.         $notificationHiearchie->setDateNotification(new \DateTime());
  198.         $notificationHiearchie->setDestination($responsable->getPartner()->getUserAccount());
  199. //        $vacationDemandeShowPath = $router->generate('app_vacation_new', ['id' => $posteId]);
  200. //        $notificationHiearchie->setActionName($vacationDemandeShowPath);
  201.         $notificationHiearchie->setMessage(
  202.             sprintf(Consts::NOTIFICATION_DEMANDE_VACATION_MESSAGE." entre ".$startHour." et ".$endhour)
  203.         );
  204.         //vacationDemande
  205.         $demandeVacation = new DemandeVacation();
  206.         $demandeVacation->setPoste($poste);
  207.         $demandeVacation->setHeureDebut($startHour);
  208.         $demandeVacation->setHeureFin($endhour);
  209.         $demandeVacation->setDemandeDateTime(new \DateTime());
  210.         $demandeVacation->setType(Consts::TYPE_TARDIF);
  211. //        dump(trr);
  212.         $entityManager->persist($notificationHiearchie);
  213.         $entityManager->persist($demandeVacation);
  214.         $entityManager->flush();
  215.         return new JsonResponse(['message' => 'Notification sent.']);
  216.     }
  217.     private $vacationWorkflow;
  218.     private $vacationWorkflowCentrale;
  219.     public function __construct(WorkflowInterface $vacationValidationStateMachine,WorkflowInterface $vacationValidationCentraleStateMachine)
  220.     {
  221.         $this->vacationWorkflow $vacationValidationStateMachine;
  222.         $this->vacationWorkflowCentrale $vacationValidationCentraleStateMachine;
  223.     }
  224.     /**
  225.      * @Route("/apply/transition/{id}/{transition}",name="apply_transition_vacation")
  226.      * @param Vacation $vacation
  227.      * @param $transition
  228.      * @param EntityManagerInterface $entityManager
  229.      * @return Response
  230.      */
  231.     public function apply_transition(Vacation $vacation$transitionEntityManagerInterface $entityManager): Response
  232.     {
  233.         if ($this->vacationWorkflow->can($vacation$transition)) {
  234.             $this->vacationWorkflow->apply($vacation$transition);
  235.             $entityManager->flush();
  236.             $this->addFlash(
  237.                 Consts::FLASH_MESSAGE_NOTICE,
  238.                 new TranslatableMessage('the transition "'.$transition.'" applied to your item !')
  239.             );
  240.         } else {
  241.             $this->addFlash(
  242.                 Consts::FLASH_MESSAGE_WARNING,
  243.                 new TranslatableMessage('You cant applied the transition "'.$transition.'" to your item!')
  244.             );
  245.         }
  246.         return $this->redirectToRoute('app_vacation_show', ['id' => $vacation->getId()]);
  247.     }
  248.     /**
  249.      * @Route("/apply/transition/{id}/{transition}",name="apply_transition_vacation_centrale")
  250.      * @param Vacation $vacation
  251.      * @param $transition
  252.      * @param EntityManagerInterface $entityManager
  253.      * @return Response
  254.      */
  255.     public function apply_transition_centrale(Vacation $vacation$transitionEntityManagerInterface $entityManager): Response
  256.     {
  257.         if ($this->vacationWorkflowCentrale->can($vacation$transition)) {
  258.             $this->vacationWorkflowCentrale->apply($vacation$transition);
  259.             $entityManager->flush();
  260.             $this->addFlash(
  261.                 Consts::FLASH_MESSAGE_NOTICE,
  262.                 new TranslatableMessage('the transition "'.$transition.'" applied to your item !')
  263.             );
  264.         } else {
  265.             $this->addFlash(
  266.                 Consts::FLASH_MESSAGE_WARNING,
  267.                 new TranslatableMessage('You cant applied the transition "'.$transition.'" to your item!')
  268.             );
  269.         }
  270.         return $this->redirectToRoute('app_vacation_show', ['id' => $vacation->getId()]);
  271.     }
  272.      /**
  273.      * @Route("/apply/transition/dashboard/{id}/{transition}",name="apply_transition_vacation_dashboard")
  274.      * @param Vacation $vacation
  275.      * @param $transition
  276.      * @param EntityManagerInterface $entityManager
  277.      * @return Response
  278.      */
  279.     public function apply_transition_dashboard(Vacation $vacation$transitionEntityManagerInterface $entityManager): Response
  280.     {
  281.         if ($this->vacationWorkflow->can($vacation$transition)) {
  282.             $this->vacationWorkflow->apply($vacation$transition);
  283.             $entityManager->flush();
  284.             return new JsonResponse(['message' => true]);
  285.         } else{
  286.             return new JsonResponse(['message' => false]);
  287.         }
  288.     }
  289.     /**
  290.      * @Route("/apply/transition/dashboardCentrale/{id}/{transition}",name="apply_transition_vacation_dashboard_centrale")
  291.      * @param Vacation $vacation
  292.      * @param $transition
  293.      * @param EntityManagerInterface $entityManager
  294.      * @return Response
  295.      */
  296.     public function apply_transition_dashboard_centrale(Vacation $vacation$transitionEntityManagerInterface $entityManager): Response
  297.     {
  298.         if ($this->vacationWorkflowCentrale->can($vacation$transition)) {
  299.             $this->vacationWorkflowCentrale->apply($vacation$transition);
  300.             $entityManager->flush();
  301.             return new JsonResponse(['message' => true]);
  302.         } else{
  303.             return new JsonResponse(['message' => false]);
  304.         }
  305.     }
  306. }