<?php
namespace App\Controller;
use App\Entity\DemandeVacation;
use App\Entity\Employee;
use App\Entity\Poste;
use App\Entity\PosteDirector;
use App\Entity\ResNotification;
use App\Entity\Vacation;
use App\Form\VacationType;
use App\Repository\EmployeeRepository;
use App\Repository\LocaliteRepository;
use App\Repository\PosteRepository;
use App\Repository\ResListeDeChoixRepository;
use App\Repository\VacationRepository;
use App\Utils\Consts;
use DateInterval;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Component\Workflow\WorkflowInterface;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
/**
* @Route("/vacation")
*/
class VacationController extends AbstractController
{
private function setBaseBreadcrumb(Breadcrumbs $breadcrumbs)
{
$breadcrumbs->addRouteItem("Accueil", 'index');
$breadcrumbs->addItem("Vacations",'/vacation');
}
/**
* @Route("/", name="app_vacation_index", methods={"GET"})
*/
public function index(VacationRepository $vacationRepository,Security $security,Request $request,Breadcrumbs $breadcrumbs): Response
{
$this->setBaseBreadcrumb($breadcrumbs);
$route = $request->attributes->get('_route');
$vacation = new Vacation();
$this->denyAccessUnlessGranted('VIEW', $vacation);
$breadcrumbs->addItem($route);
return $this->render('vacation/index.html.twig', [
'vacations' => $vacationRepository->findAll(),
]);
}
// /**
// * @Route("/new/{id}", name="app_vacation_new", methods={"GET", "POST"})
// */
// public function new(Poste $poste, VacationRepository $vacationRepository,Request $request,Breadcrumbs $breadcrumbs): Response
// {
// $this->setBaseBreadcrumb($breadcrumbs);
// $breadcrumbs->addRouteItem("poste", 'app_poste_show', ['id' => $poste->getId()]);
// $route = $request->attributes->get('_route');
// $breadcrumbs->addItem($route);
//
// $tokenStorage = $this->container->get('security.token_storage');
// $userConnected = $tokenStorage->getToken()->getUser();
//
// $userPosteDirector = $poste->getPosteDirector()->getResponsable()->getPartner()->getUserAccount();
// $userResponsable = $poste->getResponsable()->getPartner()->getUserAccount();
//
// $vacation = new Vacation();
// $vacation->setPoste($poste);
// $form = $this->createForm(VacationType::class, $vacation);
// $form->handleRequest($request);
//
// if ($form->isSubmitted() && $form->isValid()) {
//
// if (($userConnected == $userResponsable) || ($userConnected == $userPosteDirector)){
// if ($poste->getActive()) {
// $vacation->setVacationDateTime(new \DateTime('now'));
// $vacationRepository->add($vacation, true);
// }else{
// $this->addFlash(
// Consts::FLASH_MESSAGE_WARNING,
// new TranslatableMessage('Vous ne pouvez pas enregistrer des vacations ! Ce poste est désactivé !')
// );
// }
// }else{
// $this->addFlash(
// Consts::FLASH_MESSAGE_WARNING,
// new TranslatableMessage('Vous ne pouvez pas enregistrer des vacations pour ce poste !')
// );
// }
//
// return $this->redirectToRoute('app_poste_show', ['id'=>$poste->getId()], Response::HTTP_SEE_OTHER);
// }
//
// return $this->renderForm('vacation/new.html.twig', [
// 'vacation' => $vacation,
// 'form' => $form,
// 'poste' => $poste,
// ]);
// }
/**
* @Route("/{id}", name="app_vacation_show", methods={"GET"})
*/
public function show(Vacation $vacation,Request $request,Breadcrumbs $breadcrumbs): Response
{
$this->denyAccessUnlessGranted('VIEW', $vacation);
$this->setBaseBreadcrumb($breadcrumbs);
$breadcrumbs->addRouteItem("poste", 'app_poste_show', ['id' => $vacation->getPoste()->getId()]);
$route = $request->attributes->get('_route');
$breadcrumbs->addItem($route);
$options = ['action' => Consts::ACTION_SHOW];
$form = $this->createForm(VacationType::class, $vacation,$options);
$editMode = false;
return $this->render('vacation/edit.html.twig', [
'vacation' => $vacation,
'form'=>$form->createView(),
'editMode'=>$editMode
]);
}
/**
* @Route("/{id}/edit", name="app_vacation_edit", methods={"GET", "POST"})
*/
public function edit(Vacation $vacation, VacationRepository $vacationRepository,Request $request,Breadcrumbs $breadcrumbs): Response
{
$this->denyAccessUnlessGranted('EDIT', $vacation);
$tokenStorage = $this->container->get('security.token_storage');
$userConnected = $tokenStorage->getToken()->getUser();
$userPosteDirector = $vacation->getPoste()->getPosteDirector()->getResponsable()->getPartner()->getUserAccount();
$userResponsable = $vacation->getPoste()->getResponsable()->getPartner()->getUserAccount();
$editMode = true;
$options = ['action' => Consts::ACTION_EDIT];
$form = $this->createForm(VacationType::class, $vacation, $options);
$this->setBaseBreadcrumb($breadcrumbs);
$route = $request->attributes->get('_route');
$breadcrumbs->addRouteItem("vacation", 'app_vacation_show', ['id' => $vacation->getId()]);
$breadcrumbs->addItem($route);
if ($this->vacationWorkflow->can($vacation, Consts::TRANSIION_to_accepted)) {
if (($userConnected == $userResponsable) || ($userConnected == $userPosteDirector)) {
if ($vacation->getPoste()->getActive()) {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$vacationRepository->add($vacation, true);
return $this->redirectToRoute('app_vacation_show', ['id' => $vacation->getId()], Response::HTTP_SEE_OTHER);
}
}
else{
$this->addFlash(
Consts::FLASH_MESSAGE_WARNING,
new TranslatableMessage('Vous ne pouvez pas modifier cette vacation ! Ce poste est désactivé !')
);
}
}else{
$this->addFlash(
Consts::FLASH_MESSAGE_WARNING,
new TranslatableMessage('Vous ne pouvez pas modifier cette vacation !')
);
}
} else {
$this->addFlash(
Consts::FLASH_MESSAGE_WARNING,
new TranslatableMessage('Vous ne pouvez pas modifier cette vacation !')
);
return $this->redirectToRoute('app_vacation_show', ['id'=>$vacation->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('vacation/edit.html.twig', [
'vacation' => $vacation,
'form' => $form,
'editMode' => $editMode
]);
}
/**
* @Route("/{id}", name="app_vacation_delete", methods={"POST"})
*/
public function delete(Request $request, Vacation $vacation, VacationRepository $vacationRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$vacation->getId(), $request->request->get('_token'))) {
$vacationRepository->remove($vacation, true);
}
return $this->redirectToRoute('app_vacation_index', [], Response::HTTP_SEE_OTHER);
}
/**
* @Route("/send/notification", name="app_send_notification", methods={"POST"})
*/
public function demanderVacation(Request $request, EntityManagerInterface $entityManager, EmployeeRepository $employeeRepository,PosteRepository $posteRepository, UrlGeneratorInterface $router){
$posteId = $request->request->get('posteId');
$startHour = $request->request->get('startHour');
$endhour = $request->request->get('endhour');
$poste = $posteRepository->find($posteId);
$responsable = $poste->getResponsable();
//notification
$notificationHiearchie = new ResNotification();
$notificationHiearchie->setTypeNotification(Consts::NOTIFICATION_DEMANDE_VACATION);
$notificationHiearchie->setDateNotification(new \DateTime());
$notificationHiearchie->setDestination($responsable->getPartner()->getUserAccount());
// $vacationDemandeShowPath = $router->generate('app_vacation_new', ['id' => $posteId]);
// $notificationHiearchie->setActionName($vacationDemandeShowPath);
$notificationHiearchie->setMessage(
sprintf(Consts::NOTIFICATION_DEMANDE_VACATION_MESSAGE." entre ".$startHour." et ".$endhour)
);
//vacationDemande
$demandeVacation = new DemandeVacation();
$demandeVacation->setPoste($poste);
$demandeVacation->setHeureDebut($startHour);
$demandeVacation->setHeureFin($endhour);
$demandeVacation->setDemandeDateTime(new \DateTime());
$demandeVacation->setType(Consts::TYPE_TARDIF);
// dump(trr);
$entityManager->persist($notificationHiearchie);
$entityManager->persist($demandeVacation);
$entityManager->flush();
return new JsonResponse(['message' => 'Notification sent.']);
}
private $vacationWorkflow;
private $vacationWorkflowCentrale;
public function __construct(WorkflowInterface $vacationValidationStateMachine,WorkflowInterface $vacationValidationCentraleStateMachine)
{
$this->vacationWorkflow = $vacationValidationStateMachine;
$this->vacationWorkflowCentrale = $vacationValidationCentraleStateMachine;
}
/**
* @Route("/apply/transition/{id}/{transition}",name="apply_transition_vacation")
* @param Vacation $vacation
* @param $transition
* @param EntityManagerInterface $entityManager
* @return Response
*/
public function apply_transition(Vacation $vacation, $transition, EntityManagerInterface $entityManager): Response
{
if ($this->vacationWorkflow->can($vacation, $transition)) {
$this->vacationWorkflow->apply($vacation, $transition);
$entityManager->flush();
$this->addFlash(
Consts::FLASH_MESSAGE_NOTICE,
new TranslatableMessage('the transition "'.$transition.'" applied to your item !')
);
} else {
$this->addFlash(
Consts::FLASH_MESSAGE_WARNING,
new TranslatableMessage('You cant applied the transition "'.$transition.'" to your item!')
);
}
return $this->redirectToRoute('app_vacation_show', ['id' => $vacation->getId()]);
}
/**
* @Route("/apply/transition/{id}/{transition}",name="apply_transition_vacation_centrale")
* @param Vacation $vacation
* @param $transition
* @param EntityManagerInterface $entityManager
* @return Response
*/
public function apply_transition_centrale(Vacation $vacation, $transition, EntityManagerInterface $entityManager): Response
{
if ($this->vacationWorkflowCentrale->can($vacation, $transition)) {
$this->vacationWorkflowCentrale->apply($vacation, $transition);
$entityManager->flush();
$this->addFlash(
Consts::FLASH_MESSAGE_NOTICE,
new TranslatableMessage('the transition "'.$transition.'" applied to your item !')
);
} else {
$this->addFlash(
Consts::FLASH_MESSAGE_WARNING,
new TranslatableMessage('You cant applied the transition "'.$transition.'" to your item!')
);
}
return $this->redirectToRoute('app_vacation_show', ['id' => $vacation->getId()]);
}
/**
* @Route("/apply/transition/dashboard/{id}/{transition}",name="apply_transition_vacation_dashboard")
* @param Vacation $vacation
* @param $transition
* @param EntityManagerInterface $entityManager
* @return Response
*/
public function apply_transition_dashboard(Vacation $vacation, $transition, EntityManagerInterface $entityManager): Response
{
if ($this->vacationWorkflow->can($vacation, $transition)) {
$this->vacationWorkflow->apply($vacation, $transition);
$entityManager->flush();
return new JsonResponse(['message' => true]);
} else{
return new JsonResponse(['message' => false]);
}
}
/**
* @Route("/apply/transition/dashboardCentrale/{id}/{transition}",name="apply_transition_vacation_dashboard_centrale")
* @param Vacation $vacation
* @param $transition
* @param EntityManagerInterface $entityManager
* @return Response
*/
public function apply_transition_dashboard_centrale(Vacation $vacation, $transition, EntityManagerInterface $entityManager): Response
{
if ($this->vacationWorkflowCentrale->can($vacation, $transition)) {
$this->vacationWorkflowCentrale->apply($vacation, $transition);
$entityManager->flush();
return new JsonResponse(['message' => true]);
} else{
return new JsonResponse(['message' => false]);
}
}
}