<?php
namespace App\Controller;
use App\Entity\Evenement;
use App\Form\MonCompteType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;
class AppController extends AbstractController
{
private $doctrine;
public function __construct(ManagerRegistry $doctrine )
{
$this->doctrine = $doctrine;
}
/**
* @Route("/", name="main")
*/
public function homepage()
{
$securityContext = $this->container->get('security.authorization_checker');
if($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')){
if (sizeof($this->getUser()->getRoles()) > 1) {
return $this->redirectToRoute('choix_role');
}else{
return $this->redirectToRoute('reservation_mois');
}
}else{
return $this->redirectToRoute('arshdf_user_login');
}
}
/**
* @Route("/app/choix", name="choix_role")
*/
public function choix_role(){
$info = [];
$roles = [];
foreach($this->getUser()->getRoles() as $role){
if($role == 'ROLE_USER'){
$roles['Utilisateur'] = 'reservation_mois';
}elseif ($role == 'ROLE_HAB'){
$roles['Habilitation'] = 'toactivate';
}
}
$info['roles'] = $roles;
return $this->render('app/choix_profil.html.twig', $info);
}
/**
* @Route("/app/mon/compte", name="monCompte")
*/
public function monCompte(Request $request){
$info = [];
$form = $this->createForm(MonCompteType::class, $this->getUser());
$form->handleRequest($request);
$info['form'] = $form->createView();
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->doctrine->getManager();
$em->persist($this->getUser());
$em->flush();
return $this->redirectToRoute('choix_role');
}
return $this->render('app/monCompte.html.twig', $info);
}
/**
* @Route("/teletravail/automatique", name="teletravailAutomatique")
*/
public function teletravailAutomatique(){
$em = $this->doctrine->getManager();
// ==== DEBUT ==== Désactiver les comptes ldap inactifs
// $agent_a_desactiver = $this->doctrine->getRepository('App\Entity\Ldap')->findBy(['actif'=>'0']);
// foreach ($agent_a_desactiver as $a){
// $compte = $this->doctrine->getRepository('App\Entity\User')->findBy(['email'=>$a->getEmail()]);
// if(!empty($compte)){
// foreach ($compte as $user){
// $user->setDeleted(true);
// $user->setEnabled(false);
// $user->setToActivate(true);
// $em->persist($user);
// }
// }
// }
// $em->flush();
// ==== FIN ==== Désactiver les comptes ldap inactifs
// ==== DEBUT ==== Libérer les places télétravails
//0 : dimanche
$response = file_get_contents('teletravail.json');
$data = json_decode($response);
$day = strtotime(date('Y-m-d')." + 2 weeks");
$traduction = [
'Lundi' =>'Monday',
'Mardi' =>'Tuesday',
'Mercredi' =>'Wednesday',
'Jeudi' =>'Thursday',
'Vendredi' =>'Friday'
];
$agents = $this->doctrine->getRepository('App\Entity\User')->findAgentPlace();
foreach ($agents as $a){
if(isset($data->{trim($a->getEmail())})){
foreach ($data->{trim($a->getEmail())} as $j){
$d = new \DateTime( date('Y-m-d', strtotime($traduction[$j],$day)));
// je vérifie que la place n'est pas libéré
$evmntExiste = $this->doctrine->getRepository('App\Entity\Evenement')->findBy(['placeAppartientAgent'=>$a->getId(), 'dateLiberee'=>$d]);
if(sizeof($evmntExiste) == 0){
// je libère
$e = new Evenement();
$e->setPlaceAppartientAgent($a);
$e->setDateLibereeAutomatique(true);
$e->setDateLiberee($d);
$e->setDateCreated(new \DateTime("now"));
$em->persist($e);
}
}
}
}
$em->flush();
// ==== FIN ==== Libérer les places télétravails
return new Response('Ok');
}
/**
* @Route("/site/{id}", name="user_site")
*/
// public function user_site($id, Request $request){
// $info = [];
// $em = $this->getDoctrine()->getManager();
//
// $agent = $this->getDoctrine()->getRepository('ARSHDF\UserBundle\Entity\User')->find($id);
// $site = $this->getDoctrine()->getRepository('App:GestSite')->find(1);
// $user_site = $this->getDoctrine()->getRepository('App:UserSite')->findBy(['user'=>$agent]);
//
// if(null == $user_site){
// // créer l'enregistrement si n'existe pas
// $add = new UserSite();
// $add->setUser($agent);
// $add->setSite($site);
// $em->persist($add);
// $em->flush();
// $user_site = $this->getDoctrine()->getRepository('App:UserSite')->findBy(['user'=>$add->getUser()]);
// }
//
//
// $form = $this->createForm(UserSiteType::class, $user_site[0]);
// $form->handleRequest($request);
//
// if($form->isSubmitted() && $form->isValid()){
// $em->persist($user_site[0]);
// $em->flush();
// return $this->redirectToRoute('app_user_habilitation_index');
// }
//
//
// $info['form'] = $form->createView();
// $info['agent'] = $user_site;
// return $this->render('habilitation/user_site.html.twig', $info);
//
// }
}