src/Controller/UserCongeController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\UserConges;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpFoundation\Cookie;
  10. use Symfony\Component\Validator\Constraints\DateTime;
  11. use Symfony\Component\Translation\TranslatorInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use App\Repository\UserCongesRepository;
  14. use App\Service\ServiceTeaminfo;
  15. use App\Repository\InterventionsRepository;
  16. class UserCongeController extends Controller
  17. {
  18.     public $InterventionsRepository;
  19.     public $UserCongesRepository;
  20.     public $serviceTeaminfo;
  21.     public function __construct(UserCongesRepository $UserCongesRepositoryServiceTeaminfo $serviceTeaminfoInterventionsRepository $InterventionsRepository)
  22.     {
  23.         $this->InterventionsRepository $InterventionsRepository;
  24.         $this->UserCongesRepository $UserCongesRepository;
  25.         $this->ServiceTeaminfo $serviceTeaminfo;
  26.     }
  27.     public function index(Request $request)
  28.     {
  29.         return $this->render('conge/index.html.twig', [
  30.             'titre_page' => 'Congés',
  31.             'add_js_files' => "conge_js",
  32.         ]);
  33.     }
  34.     public function add(Request $requestTokenStorageInterface $tokenStorage)
  35.     {
  36.         $path parse_url($request->headers->get('referer'), PHP_URL_PATH);
  37.         $router $this->get('router');
  38.         $routeName $router->match($path)['_route'];
  39.         $user_selected $tokenStorage->getToken()->getUser();
  40.         $users $this->getDoctrine()->getRepository(User::class)->findAll();
  41.         return $this->render('conge/conge_add.html.twig', [
  42.             'users' => $users,
  43.             'user_selected' => $user_selected,
  44.             'titre_page' => 'conge',
  45.             'add_js_files' => "conge_js",
  46.             'redirectUrl' => $routeName
  47.         ]);
  48.     }
  49.     public function conge_validation(Request $request)
  50.     {
  51.         $id_user $request->get('id_user');
  52.         if ($request->get("start_date"))
  53.             $start_date = new \Datetime($request->get('start_date'));
  54.         if ($request->get("end_date"))
  55.             $end_date = new \Datetime($request->get('end_date'));
  56.         $conges_user $this->UserCongesRepository->conge_validation($id_user$start_date->format("Y/m/d"), $end_date->format("Y/m/d"));
  57.         return new Response(count($conges_user));
  58.     }
  59.     public function add_check(Request $requestTokenStorageInterface $tokenStorageTranslatorInterface $translator)
  60.     {
  61.         $entityManager $this->getDoctrine()->getManager();
  62.         $conge = new UserConges();
  63.         $date_debut = new \Datetime($request->get('date_debut'));
  64.         $date_fin = new \Datetime($request->get('date_fin'));
  65.         $interval $date_debut->diff($date_fin);
  66.         $numberOfDays $interval->days;
  67.         $user_id $request->get('user');
  68.         $currentUser $tokenStorage->getToken()->getUser();
  69.         if ($user_id)
  70.             $user $this->getDoctrine()->getRepository(User::class)->find($user_id);
  71.         else
  72.             $user $currentUser;
  73.         for ($i 0$i <= $numberOfDays$i++) {
  74.             $check_user_conge $this->CheckUserConge($date_debut$date_debut$user_id);
  75.             if ($check_user_conge) {
  76.                 $conge = new UserConges();
  77.                 $conge->setDateDebut($date_debut);
  78.                 $conge->setDateFin($date_debut);
  79.                 $conge->setUser($user);
  80.                 $conge->setState("accepte");
  81.                 $entityManager->persist($conge);
  82.                 $entityManager->flush();
  83.             }
  84.             $date_debut->modify("+1 days");
  85.         }
  86.         return $this->redirectToRoute(
  87.             "liste_conges",
  88.             array(
  89.                 'valide_msg' => $translator->trans("Disponibilité ajouté avec success"),
  90.                 'no_valide_msg' => null
  91.             )
  92.         );
  93.     }
  94.     public function CheckUserConge($date_debut$date_fin$user_id)
  95.     {
  96.         $user_conge $this->getDoctrine()->getRepository(UserConges::class)->findby(array('user' => $user_id'date_debut' => $date_debut'date_fin' => $date_fin));
  97.         if ($user_conge) {
  98.             return false;
  99.         } else {
  100.             return true;
  101.         }
  102.     }
  103.     public function get_user_info_by_date(Request $request)
  104.     {
  105.         $user_id $request->get('user_id');
  106.         $date $request->get('date');
  107.         $users = [$user_id];
  108.         $filters = ["users" => $users"start_date" => $date"end_date" => $date];
  109.         $interventions_equipe = [];
  110.         $interventions = [];
  111.         $projects = [];
  112.         $equipes = [];
  113.         $interventions_equipe $this->InterventionsRepository->getInterventionsByFilters($filtersfalsefalsefalsefalsetrue);
  114.         $interventions $this->InterventionsRepository->getInterventionsByFilters($filtersfalsetrue);
  115.         foreach ($interventions as $key => $value) {
  116.             if ($value->getProject() && !array_key_exists($value->getProject()->getId(), $projects)) {
  117.                 $projects[$value->getProject()->getId()]["libelle"] = $value->getProject()->getLibelle();
  118.                 $projects[$value->getProject()->getId()]["client"] = $value->getProject()->getClient()->getDenomination();
  119.                 $projects[$value->getProject()->getId()]["id"] = $value->getProject()->getId();
  120.                 $projects[$value->getProject()->getId()]["start"] = $value->getProject()->getDateDebut();
  121.                 $projects[$value->getProject()->getId()]["end"] = $value->getProject()->getDateFin();
  122.             }
  123.         }
  124.         $Date = new \DateTime($request->get('date'));
  125.         $strDate $Date->format("d/m/Y");
  126.         $html $this->get('twig')->render('conge/modalUserInfoByDate.html.twig', ["projects" => $projects"date" => $strDate]);
  127.         return new Response($html);
  128.     }
  129.     public function AddOneDayDispo(Request $request)
  130.     {
  131.         $entityManager $this->getDoctrine()->getManager();
  132.         $userConge = new UserConges();
  133.         $userConge->setUser($this->getDoctrine()->getRepository(User::class)->find($request->get('IdUser')));
  134.         $userConge->setDateDebut(new \DateTime(substr($request->get('date'), 010)));
  135.         $userConge->setDateFin(new \DateTime(substr($request->get('date'), 010)));
  136.         $userConge->setState("accepte");
  137.         $entityManager->persist($userConge);
  138.         $entityManager->flush();
  139.         $cookies $request->cookies;
  140.         $colors_array = array();
  141.         if ($cookies->has('users_conges_colors'))
  142.             $colors_array unserialize($cookies->get('users_conges_colors'));
  143.         $color_result $this->ServiceTeaminfo->getItemColor($request->get('IdUser'), $colors_arraycount($colors_array), false);
  144.         $colors_array $color_result["graphic_view_colors"];
  145.         if ($color_result["cookie_updated"]) {
  146.             $headers_to_send = new Response();
  147.             $cookie_to_send = new Cookie('users_conges_colors'serialize($colors_array), time() + (365 24 60 60), '/'nullfalsefalse);
  148.             $headers_to_send->headers->setCookie($cookie_to_send);
  149.             $headers_to_send->sendHeaders();
  150.         }
  151.         $conge_element = array();
  152.         $conge_element["id"] = $userConge->getId();
  153.         $conge_element["color"] = $color_result['item_color'];
  154.         $conge_element["path"] = $this->generateUrl("conge_modify", array('id' => $userConge->getId()));
  155.         return new Response(json_encode($conge_element));
  156.     }
  157.     public function modify(Request $request$id)
  158.     {
  159.         $url parse_url($request->headers->get('referer'), PHP_URL_PATH);
  160.         $userConge $this->getDoctrine()->getRepository(UserConges::class)->find($id);
  161.         $users $this->getDoctrine()->getRepository(User::class)->findAll();
  162.         $user_selected $this->getDoctrine()->getRepository(User::class)->find($userConge->getUser());
  163.         return $this->render('conge/conge_modify.html.twig', [
  164.             'users' => $users,
  165.             'user_selected' => $user_selected,
  166.             'userConge' => $userConge,
  167.             'titre_page' => 'conge',
  168.             'add_js_files' => "conge_js",
  169.             'redirectUrl' => $url
  170.         ]);
  171.     }
  172.     public function modify_check(Request $requestTranslatorInterface $translator)
  173.     {
  174.         $entityManager $this->getDoctrine()->getManager();
  175.         $conge $this->getDoctrine()->getRepository(UserConges::class)->find($request->get('idUserConge'));
  176.         if ($request->get('users')) {
  177.             $user_id $request->get('users');
  178.             $user $this->getDoctrine()->getRepository(User::class)->find($user_id[0]);
  179.             $conge->setUser($user);
  180.         }
  181.         if ($request->get("date_debut"))
  182.             $conge->setDateDebut(new \Datetime($request->get('date_debut')));
  183.         if ($request->get("date_fin"))
  184.             $conge->setDateFin(new \Datetime($request->get('date_fin')));
  185.         $entityManager->persist($conge);
  186.         $entityManager->flush();
  187.         if ($request->get("modify_ajax"))
  188.             return new Response("done");
  189.         $url $request->get("redirectUrl");
  190.         $router $this->get('router');
  191.         $routeName $router->match($url)['_route'];
  192.         $url_parts explode("/"$url);
  193.         krsort($url_parts);
  194.         $url_parts array_values($url_parts);
  195.         $id $url_parts[0];
  196.         $parameters = array(
  197.             'valide_msg' => $translator->trans("Disponibilité modifié avec success"),
  198.             'no_valide_msg' => null
  199.         );
  200.         if (intval($id) == true) {
  201.             $parameters['id'] = intval($id);
  202.             $parameters['from'] = $url_parts[1];
  203.         }
  204.         return $this->redirectToRoute($routeName$parameters);
  205.     }
  206.     function addUserToArray($User$Techniciens)
  207.     {
  208.         $Technicien = array();
  209.         $Technicien["id"] = $User->getId();
  210.         $Technicien["nom"] = $User->getNom();
  211.         $Technicien["prenom"] = $User->getPrenom();
  212.         array_push($Techniciens$Technicien);
  213.         return $Techniciens;
  214.     }
  215.     public function getTimelineUsers(Request $request)
  216.     {
  217.         $AllUsers $this->getDoctrine()->getRepository(User::class)->findAll();
  218.         $Techniciens = array();
  219.         $id_user $request->get("id_user");
  220.         $User null;
  221.         $AllUsers null;
  222.         if ($id_user)
  223.             $User $this->getDoctrine()->getRepository(User::class)->find($id_user);
  224.         else {
  225.             $currentFilter = (array) json_decode($this->forward('App\Controller\UserFilterController::verify_cookie')->getContent());
  226.             if (isset($currentFilter["users"])) {
  227.                 foreach ($currentFilter["users"] as $user_id) {
  228.                     $current_user $this->getDoctrine()->getRepository(User::class)->find($user_id);
  229.                     $AllUsers[] = $current_user;
  230.                 }
  231.             } else {
  232.                 $AllUsers $this->getDoctrine()->getRepository(User::class)->findBy([], ['nom' => 'ASC''prenom' => 'ASC']);
  233.             }
  234.         }
  235.         if (in_array("ROLE_admin"$this->getUser()->getRoles())) {
  236.             if ($User)
  237.                 $Techniciens $this->addUserToArray($User$Techniciens);
  238.             else if ($AllUsers)
  239.                 foreach ($AllUsers as $key => $User) {
  240.                     $Techniciens $this->addUserToArray($User$Techniciens);
  241.                 }
  242.         } else
  243.             $Techniciens $this->addUserToArray($this->getUser(), $Techniciens);
  244.         return new Response(json_encode($Techniciens));
  245.     }
  246.     public function getTimelineConges(Request $request)
  247.     {
  248.         if ($request->get("user_id")) {
  249.             $currentFilter = (array) json_decode($this->forward('App\Controller\UserFilterController::verify_cookie')->getContent());
  250.             $start_date = new \Datetime($request->get("start_date") . " 00:00:00");
  251.             $end_date = new \Datetime($request->get("end_date") . " 23:59:59");
  252.             $currentFilter["users"] = [$request->get("user_id")];
  253.             $conges_objects $this->UserCongesRepository->getCongeByDate($currentFilter$start_date->format("Y/m/d"), $end_date->format("Y/m/d"));
  254.             $conges = array();
  255.             $cookies $request->cookies;
  256.             $colors_array = array();
  257.             if ($cookies->has('users_conges_colors'))
  258.                 $colors_array unserialize($cookies->get('users_conges_colors'));
  259.             $cookie_updated false;
  260.             foreach ($conges_objects as $i => $conge) {
  261.                 $user $conge->getUser();
  262.                 $conge_element = array();
  263.                 $conge_element["id"] = $conge->getId();
  264.                 $conge_start_date $conge->getDateDebut();
  265.                 $conge_end_date $conge->getDateFin();
  266.                 if ($conge_start_date $start_date)
  267.                     $conge_start_date $start_date;
  268.                 if ($conge_end_date $end_date)
  269.                     $conge_end_date $end_date;
  270.                 $conge_element["user_id"] = $user->getId();
  271.                 $conge_element["path"] = $this->generateUrl("conge_modify", array('id' => $conge->getId()));
  272.                 $color_result $this->ServiceTeaminfo->getItemColor($user->getId(), $colors_array$i$cookie_updated);
  273.                 $conge_element["color"] = $color_result['item_color'];
  274.                 $cookie_updated $color_result["cookie_updated"];
  275.                 $colors_array $color_result["graphic_view_colors"];
  276.                 $interval = new \DateInterval('PT12H');
  277.                 $begin = clone $conge_start_date;
  278.                 $end = clone $conge_end_date;
  279.                 $end->setTime(001);
  280.                 $date_range = new \DatePeriod($begin$interval$end);
  281.                 foreach ($date_range as $date) {
  282.                     $conge_element["start_date"] = $date->format("Y-m-d 00:00:00");
  283.                     $conge_element["end_date"] = $date->format("Y-m-d 11:59:59");
  284.                     $conges[] = $conge_element;
  285.                     $conge_element["start_date"] = $date->format("Y-m-d 12:00:00");
  286.                     $conge_element["end_date"] = $date->format("Y-m-d 23:59:59");
  287.                     $conges[] = $conge_element;
  288.                 }
  289.             }
  290.             if ($cookie_updated) {
  291.                 $headers_to_send = new Response();
  292.                 $cookie_to_send = new Cookie('users_conges_colors'serialize($colors_array), time() + (365 24 60 60), '/'nullfalsefalse);
  293.                 $headers_to_send->headers->setCookie($cookie_to_send);
  294.                 $headers_to_send->sendHeaders();
  295.             }
  296.             return new Response(json_encode($conges));
  297.         }
  298.         return new Response("none");
  299.     }
  300.     public function delete(Request $requestTranslatorInterface $translator)
  301.     {
  302.         $entityManager $this->getDoctrine()->getManager();
  303.         $conge $this->getDoctrine()->getRepository(UserConges::class)->find($request->get('id'));
  304.         if ($conge) {
  305.             $entityManager->remove($conge);
  306.             $entityManager->flush();
  307.             return new Response("done");
  308.         }
  309.         return new Response("not found");
  310.     }
  311.     public function liste_conges(Request $requestTokenStorageInterface $tokenStorageTranslatorInterface $translator)
  312.     {
  313.         $currentFilter json_decode($this->forward('App\Controller\UserFilterController::verify_cookie')->getContent());
  314.         $user $tokenStorage->getToken()->getUser();
  315.         $vue_graphique $vue_liste null;
  316.         $parameters = [
  317.             'titre_page' => $translator->trans('Congés'),
  318.             'defaultLimit' => $this->ServiceTeaminfo::DEFAULT_LIMIT,
  319.             'vue_graphique' => $vue_graphique,
  320.             'add_js_files' => "conge_js",
  321.         ];
  322.         return $this->render('conge/index_conges.html.twig'$parameters);
  323.     }
  324.     public function change_etat(Request $request)
  325.     {
  326.         $id $request->get('id');
  327.         $etat $request->get('etat');
  328.         $msg "";
  329.         $user_conge $this->getDoctrine()->getRepository(UserConges::class)->find($id);
  330.         if ($user_conge) {
  331.             $user_conge->setState($etat);
  332.             $entityManager $this->getDoctrine()->getManager();
  333.             $entityManager->flush();
  334.             $msg "ok";
  335.         }
  336.         return new Response($msg);
  337.     }
  338.     public function get_etat($etat$id)
  339.     {
  340.         switch ($etat) {
  341.             case "accepte":
  342.                 return '<div class="etat_icon resolu" data-id=' $id ' data-etat="refuse"><i id = "accepte" class="pe-7s-check check"></i></div>';
  343.                 break;
  344.             case "refuse":
  345.                 return '<div class="etat_icon n_resolu" data-id=' $id ' data-etat="accepte"><i id = "refuse" class="pe-7s-close-circle uncheck"></i></div>';
  346.                 break;
  347.             default:
  348.                 return '<div class="etat_icon null" data-id=' $id '  data-etat="accepte"><i  id="null" class="pe-7s-info"></i></div>';
  349.         }
  350.     }
  351.     public function liste_conges_server_processing(Request $requestServiceTeaminfo $ServiceTeaminfo)
  352.     {
  353.         $draw intval($request->get('draw'));
  354.         $length $request->get('length');
  355.         $start $request->get('start');
  356.         $search $request->get('search')['value'];
  357.         $orders $request->get('order');
  358.         $order $request->get('order')[0]['dir'];
  359.         $order_num $request->get('order')[0]['column'];
  360.         $columns $request->get('columns');
  361.         $deleted false;
  362.         $currentFilter json_decode($this->forward('App\Controller\UserFilterController::verify_cookie')->getContent());
  363.         if ($request->get('id_user') != null)
  364.             $id $request->get('id_user');
  365.         else
  366.             $id null;
  367.         $results $this->UserCongesRepository->getAllConges($search$order$order_num$length$start$deleted$id$currentFilter);
  368.         $listeconges $results["results"];
  369.         $tot_data $results["count"];
  370.         $conges = array();
  371.         foreach ($listeconges as $key => $value) {
  372.             $conge = array();
  373.             if ($value) {
  374.                 $conge["nom"] = $value->getUser()->getNom() . " " $value->getUser()->getPrenom();
  375.                 $conge["date_debut"] = $value->getDateDebut()->format("d-m-Y");
  376.                 $conge["date_fin"] = $value->getDateFin()->format("d-m-Y");
  377.                 $conge["etat"] = $this->get_etat($value->getState(), $value->getId());
  378.             }
  379.             $conges[] = $conge;
  380.         }
  381.         $output = array(
  382.             "draw" => $draw,
  383.             "recordsTotal" => intval($tot_data),
  384.             "recordsFiltered" => intval($tot_data),
  385.             "data" => $conges
  386.         );
  387.         return new Response(json_encode($output), 200, ['Content-Type' => 'application/json']);
  388.     }
  389.     public function getCongeById(Request $request)
  390.     {
  391.         $conge $this->getDoctrine()->getRepository(UserConges::class)->find($request->get("conge_id"));
  392.         if ($conge) {
  393.             $conge_object = array(
  394.                 "id" => $conge->getId(),
  395.                 "user_id" => $conge->getUser()->getId(),
  396.                 "start" => $conge->getDateDebut()->format("Y-m-d"),
  397.                 "end" => $conge->getDateFin()->format("Y-m-d"),
  398.                 "path" => $this->generateUrl("conge_modify", array('id' => $conge->getId()))
  399.             );
  400.             return new Response(json_encode($conge_object));
  401.         }
  402.         return new Response('none');
  403.     }
  404.     public function getCongeByUserAndDate(Request $request)
  405.     {
  406.         $old_conge $this->getDoctrine()->getRepository(UserConges::class)->find($request->get("conge_id"));
  407.         $user_id $request->get("user_id");
  408.         $start_date $request->get("start_date");
  409.         $end_date $request->get("end_date");
  410.         $conges $this->getDoctrine()->getRepository(UserConges::class)->getCongeByDate(["users" => [$user_id]], $start_date$end_date$request->get("conge_id"));
  411.         if ($conges)
  412.             return new Response(count($conges));
  413.         return new Response('none');
  414.     }
  415.     public function ExportVueGraphiqueConge(Request $request)
  416.     {
  417.         $start_date = new \Datetime($request->get("date_debut") . " 00:00:00");
  418.         $end_date = new \Datetime($request->get("date_fin") . " 23:59:59");
  419.           
  420.         $Techniciens = array();
  421.         $id_user $request->get("id_user");
  422.         $AllUsers = array();
  423.         $currentFilter = (array) json_decode($this->forward('App\Controller\UserFilterController::verify_cookie')->getContent(),true);
  424.         if (isset($currentFilter["users"])) {
  425.             foreach ($currentFilter["users"] as $user_id) {
  426.                 $current_user $this->getDoctrine()->getRepository(User::class)->find($user_id);
  427.                 $AllUsers[] = $current_user;
  428.             }
  429.         } else {
  430.             $AllUsers $this->getDoctrine()->getRepository(User::class)->findBy([], ['nom' => 'ASC''prenom' => 'ASC']);
  431.         }
  432.         if (in_array("ROLE_admin"$this->getUser()->getRoles())) {
  433.             if ($AllUsers){
  434.                 foreach ($AllUsers as $key => $User) {
  435.                     $Techniciens $this->addUserToArray($User$Techniciens);
  436.                 }
  437.             }
  438.         } else
  439.             $Techniciens $this->addUserToArray($this->getUser(), $Techniciens);
  440.         $conges_objects $this->UserCongesRepository->getCongeByDate($currentFilter$start_date->format("Y/m/d"), $end_date->format("Y/m/d"));
  441.         
  442.         $conges = array();
  443.         $tabDaysOfWeek = array();
  444.         $tabDaysOfWeekHeader = array();
  445.          
  446.         $current_date = clone $start_date;
  447.         while ($current_date <= $end_date) {
  448.             setlocale(LC_TIME'fr_FR.UTF-8');
  449.             
  450.             $dayOfWeek strftime("%A"$current_date->getTimestamp());
  451.             $dayOfMonth $current_date->format("d");
  452.             $tabDaysOfWeekHeader[] = $dayOfWeek " " $dayOfMonth;
  453.             $tabDaysOfWeek[] = $current_date->format("Y-m-d");
  454.             $current_date->modify("+1 day");
  455.         }
  456.         
  457.         
  458.         foreach ($conges_objects as $CongeUser) {
  459.             $user_id $CongeUser->getUser()->getId();
  460.             $conge_start_date $CongeUser->getDateDebut();
  461.             $conge_end_date $CongeUser->getDateFin();
  462.             $conge_end_date->modify("+1 day");
  463.             if (!isset($conges[$user_id])) {
  464.                 $conges[$user_id] = array();
  465.             }
  466.             
  467.             $interval = new \DateInterval('P1D');
  468.             $period = new \DatePeriod($conge_start_date$interval$conge_end_date);
  469.             foreach($period as $date){
  470.                 $conges[$user_id][] = $date->format("Y-m-d");
  471.             }
  472.         }
  473.         $Users $this->getDoctrine()->getRepository(User::class)->findBy(['deleted' => null'client' => null'isActive' => 1]);
  474.         $usersConge = array();
  475.         foreach ($Users as $user) {
  476.             $usersConge[] = array(
  477.                 'id' => $user->getId(),
  478.                 'nom' => $user->getNom() . " " $user->getPrenom()
  479.             );
  480.         }
  481.         
  482.         $titre $start_date->format('d') . " - " $end_date->format('d') . " " $this->ServiceTeaminfo::MONTHS[intval($end_date->format('m'))]["libelle"];
  483.         $parameters = array(
  484.             'tabDaysOfWeek' => $tabDaysOfWeek,
  485.             'tabDaysOfWeekHeader' => $tabDaysOfWeekHeader,
  486.             'usersConge' => $Techniciens,
  487.             'conges' => $conges,
  488.             'titre' => $titre,
  489.         );
  490.         $html $this->get('twig')->render('conge/ExportVueGraphiqueTemplateConge.html.twig'$parameters);
  491.         $url $this->ServiceTeaminfo->generatePdf($html"uploads""VueGraphique");
  492.         return new Response($url);
  493.     }
  494. }