src/Controller/HolidaysController.php line 325

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Holidays;
  4. use App\Entity\SettingsOffice;
  5. use App\Entity\User;
  6. use App\Form\HolidaysType;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class HolidaysController extends AbstractController
  14. {
  15.     private $translator;
  16.     public function __construct(TranslatorInterface $translator) {
  17.         $this->translator = $translator;
  18.     }
  19.     
  20.     /**
  21.      * @Route("/holidays/add", name="holiday_form")
  22.      */
  23.     public function createHolidaysAction(Request $request) {
  24.         $holiday = new Holidays();
  25.         $form = $this->createHolidaysCreateForm($holiday);
  26.         return $this->render('holidays/add-holidays.html.twig', array('form' => $form->createView()));
  27.     }
  28.     private function createHolidaysCreateForm(Holidays $entity)
  29.     {
  30.         $form = $this->createForm(HolidaysType::class, $entity, array(
  31.             'action' => $this->generateUrl('holidays_create'),
  32.             'method' => 'POST'
  33.         ));
  34.         return $form;
  35.     }
  36.     /**
  37.      * @Route("/holidays/create", name="holidays_create")
  38.      */
  39.     public function createAction(Request $request, LoggerInterface $logger)
  40.     {
  41.         $holiday = new Holidays();
  42.         $form = $this->createHolidaysCreateForm($holiday);
  43.         $form->handleRequest($request);
  44.             if($form->isValid())
  45.             {
  46.                 $em = $this->getDoctrine()->getManager();
  47.                 /* Obtengo usuario logueado */
  48.                 $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  49.                 $user_id = $user_logueado->getId();
  50.                 $holiday->setCreatedId($user_id);
  51.                 $holiday->setUpdatedId($user_id);
  52.                 /* Gestión de eventos en Log */
  53.                 $user_lastname = $user_logueado->getLastname();
  54.                 $user_name = $user_logueado->getName();
  55.                 $user_email = $user_logueado->getEmail();
  56.                 $user_rol = $user_logueado->getRoles();
  57.                 $event_url = $request->getPathInfo();
  58.                 $event_complete = $user_name.' '.$user_lastname.' - '.$user_email.' - '.$user_rol[0].' | '.$event_url;
  59.                 try
  60.                 {
  61.                     $office = $form->get('office')->getData();
  62.                     $holiday->setOffice($office->getId());
  63.                     $em->persist($holiday);
  64.                     $em->flush();
  65.                     $event = 'The holiday has been created. ';
  66.                     $successMessage = $this->translator->trans($event);
  67.                     $this->addFlash('mensajeholidays', $successMessage);
  68.                     $logger->info($event_complete.' | '.$event);
  69.                 } catch (\Exception $e){
  70.                     $event = 'An error occurred: '.$e->getMessage();
  71.                     /* Para el log */
  72.                     $logger->error($event_complete.' | '.$event);
  73.                     /* Para el usuario */
  74.                     $errorMessage = $this->translator->trans($event);
  75.                     $this->addFlash('mensajeholidayserror', $errorMessage);
  76.                 }
  77.                 /* Fin Gestión de eventos en Log */
  78.                 //return $this->render('holidays/add-holidays.html.twig', array('form' => $form->createView()));
  79.                 return $this->redirectToRoute('holidays_index');
  80.             }else{
  81.                 $errorMessage = $this->translator->trans('Error, some fields are empty');
  82.                 $this->addFlash('mensajeholidayserror', $errorMessage);
  83.             }
  84.         //return $this->render('holidays/add-holidays.html.twig', array('form' => $form->createView()));
  85.         return $this->redirectToRoute('holidays_index');
  86.     }
  87.     /**
  88.      * @Route("/holidays/list", name="holidays_index")
  89.      */
  90.     public function listAction(Request $request) {
  91.         $em = $this->getDoctrine()->getManager();
  92.         $holidays = $em->getRepository(Holidays::class)->findAll();
  93.         $data = array();
  94.         foreach($holidays as $holiday) {
  95.             $offices = $em->getRepository(SettingsOffice::class)->findOneById($holiday->getOffice());
  96.             $holiday->setOffice($offices->getOffice());
  97.             $data[] = array(
  98.                     'id' => $holiday->getId(),
  99.                     'date' => $holiday->getDate(),
  100.                     'office' => $holiday->getOffice(),
  101.                     'color' => $holiday->getColor(),
  102.                     'title' => $holiday->getTitle(),
  103.                 );
  104.             //$destination = $em->getRepository(Destination::class)->findOneById($idea->getDestinationId());
  105.             //$supplierIdeaServicesControl = $em->getRepository(SupplierIdeaServicesControl::class)->findOneByIdeaId($idea->getId());
  106. //            if($idea->getDestinationId() == '0' OR is_null($idea->getDestinationId())){
  107. //                $data[] = array(
  108. //                    'id' => $idea->getId(),
  109. //                    'idc' => $supplierIdeaServicesControl->getId(),
  110. //                    'name' => $idea->getName(),
  111. //                    'destination' => 'Sin Destino',
  112. //                    'observation' => $idea->getObservation()
  113. //                );
  114. //            }else{
  115. //                $data[] = array(
  116. //                    'id' => $idea->getId(),
  117. //                    'idc' => $supplierIdeaServicesControl->getId(),
  118. //                    'name' => $idea->getName(),
  119. //                    'destination' => $destination->getTitle(),
  120. //                    'observation' => $idea->getObservation()
  121. //                );
  122. //            }
  123.         }
  124.         return $this->render('holidays/list-holidays.html.twig', array(
  125.             'holidays' => $data
  126.         ));
  127.     }
  128.     /**
  129.      * @Route("/holidays/deleted/{id}", name="holidays_deleted")
  130.      *
  131.      */
  132. //    public function deleteAction($id, Request $request, LoggerInterface $logger)
  133.     public function deleteAction($id, Request $request, LoggerInterface $logger)
  134.     {
  135.         $em = $this->getDoctrine()->getManager();
  136. //        $var = $em->getRepository(ProposalDocument::class);
  137. //        $delete = $var->findOneById($id);
  138.         $holidays = $em->getRepository(Holidays::class)->findOneById($id);
  139.         if (!empty($holidays))
  140.             {
  141.             try{
  142.                 $em->remove($holidays);
  143.                 $em->flush();
  144.                 }
  145.             catch (\Exception $e){
  146.             $event = 'An error occurred: '.$e->getMessage();
  147.             /* Para el log */
  148.             $logger->error($event);
  149.             /* Para el usuario */
  150.             $errorMessage = $this->translator->trans($event);
  151.             $this->addFlash('mensajeholidayserror', $errorMessage);
  152.         }
  153.             }
  154.         $event = 'The Item has been deleted.';
  155.         $successMessage = $this->translator->trans($event);
  156.         $this->addFlash('mensajeholidays', $successMessage);
  157.         /* Fin Gestión de eventos en Log */
  158.         return $this->redirectToRoute('holidays_index');
  159.     }
  160.     /**
  161.      * @Route("/holidays/edit/{id}", name="holidays_edit")
  162.      */
  163.     public function editHolidaysAction($id, Request $request)
  164.     {
  165.         $em = $this->getDoctrine()->getManager();
  166.         $holidays = $em->getRepository(Holidays::class)->findOneById($id);
  167.         $form = $this->createEditHolidaysForm($holidays, $id);
  168.         return $this->render('holidays/edit-holidays.html.twig',
  169.             array(
  170.                 'id' =>$id,
  171.                 'holidays' => $holidays,
  172.                 'form' => $form->createView()
  173.             )
  174.         );
  175.     }
  176.     private function createEditHolidaysForm(Holidays $entity, $id)
  177.     {
  178.         $form = $this->createForm(HolidaysType::class, $entity,
  179.             array(
  180.                 'action' => $this->generateUrl('holidays_update',
  181.                     array(
  182.                         'id' => $id
  183.                     )
  184.                 ), 'method' => 'PUT'));
  185.         return $form;
  186.     }
  187.     /**
  188.      * @Route("/holidays/update/{id}", name="holidays_update")
  189.      */
  190.     public function updateHolidaysAction($id, Request $request, LoggerInterface $logger)
  191.     {
  192.         $em = $this->getDoctrine()->getManager();
  193.         $holiday = $em->getRepository(Holidays::class)->findOneById($id);
  194.         $form = $this->createEditHolidaysForm($holiday, $id);
  195.         $form->handleRequest($request);
  196.         if($form->isValid())
  197.         {
  198.             $em = $this->getDoctrine()->getManager();
  199.             /* Obtengo usuario logueado */
  200.             $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  201.             $user_id = $user_logueado->getId();
  202.            // $holiday->setCreatedId($user_id);
  203.             $holiday->setUpdatedId($user_id);
  204.             /* Gestión de eventos en Log */
  205.             $user_lastname = $user_logueado->getLastname();
  206.             $user_name = $user_logueado->getName();
  207.             $user_email = $user_logueado->getEmail();
  208.             $user_rol = $user_logueado->getRoles();
  209.             $event_url = $request->getPathInfo();
  210.             $event_complete = $user_name.' '.$user_lastname.' - '.$user_email.' - '.$user_rol[0].' | '.$event_url;
  211.             try
  212.         {
  213.             $office = $form->get('office')->getData();
  214.             $holiday->setOffice($office->getId());
  215.             $em->persist($holiday);
  216.             $em->flush();
  217.             $event = 'The holiday has been updated. ';
  218.             $successMessage = $this->translator->trans($event);
  219.             $this->addFlash('mensajeholidays', $successMessage);
  220.             $logger->info($event_complete.' | '.$event);
  221.         } catch (\Exception $e){
  222.             $event = 'An error occurred: '.$e->getMessage();
  223.             /* Para el log */
  224.             $logger->error($event_complete.' | '.$event);
  225.             /* Para el usuario */
  226.             $errorMessage = $this->translator->trans($event);
  227.             $this->addFlash('mensajeholidayserror', $errorMessage);
  228.             return $this->render('holidays/add-holidays.html.twig', array('form' => $form->createView()));
  229.         }
  230.         /* Fin Gestión de eventos en Log */
  231.             }else{
  232.         $errorMessage = $this->translator->trans('Error, some fields are empty');
  233.         $this->addFlash('mensajeholidayserror', $errorMessage);
  234.         }
  235.         return $this->redirectToRoute('holidays_index');
  236.     }
  237.     /**
  238.      * @Route("/holidays/events", name="get_holidays_select")
  239.      */
  240.     public function holidaysSelectAction(Request $request) {
  241.         $em = $this->getDoctrine()->getManager();
  242.         /* Obtengo usuario logueado */
  243.         /**
  244.          * @var User    $user_logueado
  245.          */
  246.         // Se enviará la lista de días festivos correspondientes a la oficina del usuario que está logueado
  247.         $user_logueado = $this->get('security.token_storage')->getToken()->getUser();
  248.         $userOffice = $user_logueado->getOffice();
  249.         $holidays = $em->getRepository(Holidays::class)->findBy(
  250.             array(
  251.                 'office' => $userOffice
  252.             )
  253.         )
  254.         ;
  255.         $datos = array();
  256.         if (!empty($holidays)){
  257.             foreach($holidays as $holiday){
  258.                 $offices = $em->getRepository(SettingsOffice::class)->findOneById($holiday->getOffice());
  259.                 $holiday->setOffice($offices->getOffice());
  260.                 $colorHay = $holiday->getColor();
  261.                 if (empty($colorHay)){
  262.                     $color = "#BDBDBD";
  263.                 }else{
  264.                     $color = $holiday->getColor();
  265.                 }
  266.                 $datos[] = array(
  267.                     "id" => $holiday->getId(),
  268.                     "title" => $holiday->getTitle(),
  269.                     "tooltip" => $holiday->getTitle().", Office: ".$holiday->getOffice(),
  270.                     "start" => $holiday->getDate(),
  271.                     "end" => $holiday->getDate(),
  272.                     "color" => $color,
  273.                     "url" => "#"
  274.                 );
  275.             }
  276.         }
  277.         else
  278.         {
  279.             $datos = [];
  280.         }
  281.         $return = array(
  282.             'holidays' => $datos,
  283.         );
  284.         $response = new JsonResponse($return);
  285.         return $response;
  286.     }
  287. }