<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\AvocatsRepository;
use App\Entity\Avocats;
use App\Entity\JoursSemaine;
use App\Entity\Tranches;
use App\Repository\JoursSemaineRepository;
use App\Repository\JoursFermeturesRepository;
use App\Repository\TranchesRepository;
use App\Repository\RendezVousRepository;
use App\Repository\LieuxRepository;
use App\Repository\StatutsRepository;
use Symfony\Component\HttpFoundation\Request;
use Mailjet\Api\Client;
use Mailjet\Resources;
class AdminController extends AbstractController
{
/**
* @Route("/administration", name="app_admin")
*/
public function index(): Response
{
return $this->render('admin/index.html.twig', [
'controller_name' => 'AdminController',
]);
}
/**
* @Route("/tranchesHoraires/{id}", name="tranchesHoraires")
*/
public function tranchesHoraires(Request $request, $id,TranchesRepository $tranchesRepo, JoursFermeturesRepository $joursFermetureRepo): Response
{
$tranches = $tranchesRepo->findBy(array('JoursFermetures'=>$joursFermetureRepo->findOneById($id)));
$jours = $joursFermetureRepo->findOneById($id);
$em = $this->getDoctrine()->getManager();
if(!empty($_POST))
{
foreach($tranches as $res)
{
$res->setOuvert(0);
$em->persist($res);
$em->flush();
}
foreach($_POST['ouvert'] as $res)
{
$tr = $tranchesRepo->findOneById($res);
$tr->setOuvert(1);
$em->persist($tr);
$em->flush();
}
return $this->redirectToRoute('tranchesHoraires', ['id'=>$id,'success'=>'ok'], Response::HTTP_SEE_OTHER);
}
return $this->render('admin/tranches.html.twig', [
'tranches' => $tranches,
'jours'=>$jours
]);
}
/**
* @Route("/updateJoursSemaineAvocat", name="updateJoursSemaineAvocat")
*/
public function updateJoursSemaineAvocat(Request $request, JoursSemaineRepository $joursSemaineRepo, AvocatsRepository $avocatsRepository): Response
{
if(isset($_POST['lundi']))
{
$lundi = 1;
}
else
{
$lundi = 0;
}
if(isset($_POST['mardi']))
{
$mardi = 1;
}
else
{
$mardi = 0;
}
if(isset($_POST['mercredi']))
{
$mercredi = 1;
}
else
{
$mercredi = 0;
}
if(isset($_POST['jeudi']))
{
$jeudi = 1;
}
else
{
$jeudi = 0;
}
if(isset($_POST['vendredi']))
{
$vendredi = 1;
}
else
{
$vendredi = 0;
}
if(isset($_POST['samedi']))
{
$samedi = 1;
}
else
{
$samedi = 0;
}
if(isset($_POST['dimanche']))
{
$dimanche = 1;
}
else
{
$dimanche = 0;
}
$js = $joursSemaineRepo->findOneById($_POST['id']);
$js->setLundi($lundi);
$js->setMardi($mardi);
$js->setMercredi($mercredi);
$js->setJeudi($jeudi);
$js->setVendredi($vendredi);
$js->setSamedi($samedi);
$js->setDimanche($dimanche);
$joursSemaineRepo->add($js, true);
return $this->redirectToRoute('app_avocats_edit', ['id'=>$js->getAvocats()->getId()], Response::HTTP_SEE_OTHER);
}
/**
* @Route("/addLieuAvocat/{id}", name="addLieuAvocat")
*/
public function addLieuAvocat(Request $request, LieuxRepository $lieuxRepo,JoursSemaineRepository $joursSemaineRepo, AvocatsRepository $avocatsRepository,$id): Response
{
if(!empty($_POST))
{
$lieu = $lieuxRepo->findOneById($_POST['lieu']);
$avocat = $avocatsRepository->findOneById($_POST['avocat']);
$lieuExist = $joursSemaineRepo->findBy(array('Lieux'=>$lieu,'Avocats'=>$avocat));
if(count($lieuExist) > 0)
{
return $this->redirectToRoute('app_avocats_edit', ['id'=>$avocat->getId(),'erreur'=>'lieuExist'], Response::HTTP_SEE_OTHER);
}
else
{
$js = new JoursSemaine();
$js->setLundi(1);
$js->setMardi(1);
$js->setMercredi(1);
$js->setJeudi(1);
$js->setVendredi(1);
$js->setSamedi(0);
$js->setDimanche(0);
$js->setLieux($lieu);
$js->setAvocats($avocat);
$js->setHoAmLundi('07:00');
$js->setHfAmLundi('12:00');
$js->setHoPmLundi('14:00');
$js->setHfPmLundi('18:00');
$js->setHoAmMardi('07:00');
$js->setHfAmMardi('12:00');
$js->setHoPmMardi('14:00');
$js->setHfPmMardi('18:00');
$js->setHoAmMercredi('07:00');
$js->setHfAmMercredi('12:00');
$js->setHoPmMercredi('14:00');
$js->setHfPmMercredi('18:00');
$js->setHoAmJeudi('07:00');
$js->setHfAmJeudi('12:00');
$js->setHoPmJeudi('14:00');
$js->setHfPmJeudi('18:00');
$js->setHoAmVendredi('07:00');
$js->setHfAmVendredi('12:00');
$js->setHoPmVendredi('14:00');
$js->setHfPmVendredi('18:00');
$js->setHoAmSamedi('07:00');
$js->setHfAmSamedi('12:00');
$js->setHoPmSamedi('14:00');
$js->setHfPmSamedi('18:00');
$js->setHoAmDimanche('07:00');
$js->setHfAmDimanche('12:00');
$js->setHoPmDimanche('14:00');
$js->setHfPmDimanche('18:00');
$joursSemaineRepo->add($js, true);
return $this->redirectToRoute('app_avocats_edit', ['id'=>$avocat->getId()], Response::HTTP_SEE_OTHER);
}
}
return $this->render('admin/addLieuAvocat.html.twig', [
'lieux' => $lieuxRepo->findAll(),
'avocat'=>$id
]);
}
/**
* @Route("/calendrier/{id}", name="calendrier")
*/
public function calendrier(Request $request, RendezVousRepository $rdvRepo,JoursSemaineRepository $joursSemaineRepo, AvocatsRepository $avocatsRepository,$id): Response
{
$avocat=$avocatsRepository->findOneById($id);
$rdv = $rdvRepo->findBy(array('Avocats'=>$avocat));
return $this->render('admin/calendrier.html.twig', [
'rdv'=>$rdv,
'avocat'=>$avocat
]);
}
/**
* @Route("/listeRdv", name="listeRdv")
*/
public function listeRdv(Request $request, RendezVousRepository $rdvRepo, AvocatsRepository $avocatsRepository, TranchesRepository $tranchesRepository, JoursFermeturesRepository $joursFermeturesRepository)
{
// header('Access-Control-Allow-Origin: *');
$em = $this->getDoctrine()->getManager();
if(empty($_GET['avocat']))
{
$_GET['avocat'] = $this->getUser()->getAvocats();
}
$avocat=$avocatsRepository->findOneById($_GET['avocat']);
$rdv = $rdvRepo->findBy(array('Avocats'=>$avocat));
$rappels = array();
$i=-1;
$rappels = [];
foreach($rdv as $res)
{
// en attente
if($res->getStatuts()->getId() == 1)
{
$titre = $res->getLieux()->getLibelle().' - '.$res->getNom().' '.$res->getPrenom();
$rappels[] = array(
'id' => $res->getId(),
'title' => $titre,
'start' => str_replace(' ', 'T', $res->getDate()->format('Y-m-d H:i:s')),
'end' => str_replace(' ', 'T', $res->getDateFin()->format('Y-m-d H:i:s')),
'color' => '#f3cb55'
);
}
// validé
if($res->getStatuts()->getId() == 2)
{
$titre = $res->getLieux()->getLibelle().' - '.$res->getNom().' '.$res->getPrenom();
$rappels[] = array(
'id' => $res->getId(),
'title' => $titre,
'start' => str_replace(' ', 'T', $res->getDate()->format('Y-m-d H:i:s')),
'end' => str_replace(' ', 'T', $res->getDateFin()->format('Y-m-d H:i:s')),
'color' => '#32ae38'
);
}
}
// dispo
$jours = $joursFermeturesRepository->findBy(['Avocats'=>$avocat]);
$j = array();
foreach($jours as $res)
{
array_push($j, $res->getId());
}
$tranches = $tranchesRepository->findBy(['JoursFermetures'=>$j,'Ouvert'=>1]);
foreach($tranches as $res)
{
if($res->getDate()->format('Y-m-d') >= date('Y-m-d'))
{
$titre = 'Disponible';
$rappels[] = array(
'id' => $res->getId(),
'title' => $titre,
'start' => str_replace(' ', 'T', $res->getDate()->format('Y-m-d H:i:s')),
'end' => str_replace(' ', 'T', $res->getDate()->format('Y-m-d H:i:s')),
'color' => '#2986cc'
);
}
}
$response = new Response();
$response->setContent(json_encode($rappels));
// $response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Origin', '*');
return $response;
}
/**
* @Route("/liste-des-rendez-vous/{etat}", name="listeRdvAConfirmer")
*/
public function listeRdvAConfirmer(Request $request, RendezVousRepository $rdvRepo, AvocatsRepository $avocatsRepository, $etat)
{
// header('Access-Control-Allow-Origin: *');
$em = $this->getDoctrine()->getManager();
if(empty($_GET['avocat']))
{
$_GET['avocat'] = $this->getUser()->getAvocats();
}
$avocat=$avocatsRepository->findOneById($_GET['avocat']);
$rdv = $rdvRepo->findBy(array('Avocats'=>$avocat,'Statuts'=>$etat),array('id'=>'desc'));
return $this->render('admin/rdvEnAttente.html.twig', [
'rdv'=>$rdv,
'avocat'=>$avocat,
"etat"=>$etat
]);
}
/**
* @Route("/details-rendez-vous/{id}", name="detailsRdv")
*/
public function detailsRdv(Request $request, RendezVousRepository $rdvRepo, AvocatsRepository $avocatsRepository, $id)
{
// header('Access-Control-Allow-Origin: *');
$em = $this->getDoctrine()->getManager();
$rdv = $rdvRepo->findOneById($id);
return $this->render('admin/detailsRdv.html.twig', [
'rdvs'=>$rdv,
]);
}
/**
* @Route("/updateEtatRdv/{id}/{etat}/{page}", name="updateEtatRdv")
*/
public function updateEtatRdv(Request $request, RendezVousRepository $rdvRepo, AvocatsRepository $avocatsRepository, StatutsRepository $statutsRepository, $id,$etat,$page)
{
// header('Access-Control-Allow-Origin: *');
$em = $this->getDoctrine()->getManager();
$rdv = $rdvRepo->findOneById($id);
if($etat == 'accepter')
{
$statut = $statutsRepository->findOneById(2);
$rdv->setStatuts($statut);
$em->persist($rdv);
$em->flush();
$etatRedirect = 1;
$messageSMS = "M./Mme ".$rdv->getPrenom()." ".$rdv->getNom().", Nous vous confirmons le rendez-vous fixé en date du ".$rdv->getDate()->format('d-m-Y')." à ".$rdv->getDate()->format('H:i')." heures avec Maître ".$rdv->getAvocats()->getNom().". Lieu : ".$rdv->getLieux()->getAdresse().".";
//$messageSMS .= "Vous pouvez annuler ce rendez-vous jusqu’à 48h à l’avance via l'adresse suivante ".$rdv->getAvocats()->getEmail().". Nous attirons votre attention sur le fait que tout rendez-vous non annulé qui ne serait pas honoré est de nature à engendrer des désagréments pour notre cabinet de sorte que les frais y relatifs pourraient vous être réclamés à concurrence d’un montant forfaitaire de 50 € tvac.";
$subject = 'Confirmation du rendez-vous';
$message = $this->renderView(
// templates/emails/registration.txt.twig
'emails/confirmationFinale.html.twig',
[
'nom' => strtoupper($rdv->getNom()),
'prenom' => strtoupper($rdv->getPrenom()),
'date'=>$rdv->getDate()->format('d-m-Y'),
'heure'=>$rdv->getDate()->format('H:i'),
'email'=>$rdv->getEmail(),
'telephone'=>$rdv->getTelephone(),
'lieu'=>$rdv->getLieux()->getLibelle(),
'adresse'=>$rdv->getLieux()->getAdresse(),
'telAvocat'=>$rdv->getAvocats()->getTel(),
'emailAvocat'=>$rdv->getAvocats()->getEmail(),
'avocat'=>$rdv->getAvocats()->getNom().' '.$rdv->getAvocats()->getPrenom(),
]
);
}
if($etat == 'refuser')
{
$em->remove($rdv);
$em->flush();
$etatRedirect = 1;
if($_GET['reponse'] == 1)
{
$messageSMS = "M./Mme ".$rdv->getPrenom()." ".$rdv->getNom().", malheureusement, Maître ".$rdv->getAvocats()->getNom()." ne pourra vous recevoir en date du ".$rdv->getDate()->format('d-m-Y')." à ".$rdv->getDate()->format('H:i')." heures à ".$rdv->getLieux()->getAdresse().".";
}
else
{
$messageSMS = "M./Mme ".$rdv->getPrenom()." ".$rdv->getNom().", malheureusement, Maître ".$rdv->getAvocats()->getNom()." ne pourra vous recevoir en date du ".$rdv->getDate()->format('d-m-Y')." à ".$rdv->getDate()->format('H:i')." heures à ".$rdv->getLieux()->getAdresse().".";
}
$subject = 'Annulation du rendez-vous';
$message = $this->renderView(
// templates/emails/registration.txt.twig
'emails/annulation.html.twig',
[
'nom' => strtoupper($rdv->getNom()),
'prenom' => strtoupper($rdv->getPrenom()),
'date'=>$rdv->getDate()->format('d-m-Y'),
'heure'=>$rdv->getDate()->format('H:i'),
'email'=>$rdv->getEmail(),
'telephone'=>$rdv->getTelephone(),
'lieu'=>$rdv->getLieux()->getLibelle(),
'adresse'=>$rdv->getLieux()->getAdresse(),
'telAvocat'=>$rdv->getAvocats()->getTel(),
'emailAvocat'=>$rdv->getAvocats()->getEmail(),
'avocat'=>$rdv->getAvocats()->getNom().' '.$rdv->getAvocats()->getPrenom(),
'reponse'=>$_GET['reponse']
]
);
}
if($etat == 'archiver')
{
$statut = $statutsRepository->findOneById(3);
$rdv->setStatuts($statut);
$em->persist($rdv);
$em->flush();
$etatRedirect = 1;
}
if($etat == 'annuler')
{
$em->remove($rdv);
$em->flush();
$etatRedirect = 2;
if($_GET['reponse'] == 1)
{
$messageSMS = "M./Mme ".$rdv->getPrenom()." ".$rdv->getNom().", malheureusement, Maître ".$rdv->getAvocats()->getNom()." ne pourra pas vous recevoir en date du ".$rdv->getDate()->format('d-m-Y')." à ".$rdv->getDate()->format('H:i')." heures à ".$rdv->getLieux()->getAdresse().".";
}
else
{
$messageSMS = "M./Mme ".$rdv->getPrenom()." ".$rdv->getNom().", malheureusement, Maître ".$rdv->getAvocats()->getNom()." ne pourra pas vous recevoir en date du ".$rdv->getDate()->format('d-m-Y')." à ".$rdv->getDate()->format('H:i')." heures à ".$rdv->getLieux()->getAdresse().".";
}
$subject = 'Annulation du rendez-vous';
$message = $this->renderView(
// templates/emails/registration.txt.twig
'emails/annulation.html.twig',
[
'nom' => strtoupper($rdv->getNom()),
'prenom' => strtoupper($rdv->getPrenom()),
'date'=>$rdv->getDate()->format('d-m-Y'),
'heure'=>$rdv->getDate()->format('H:i'),
'email'=>$rdv->getEmail(),
'telephone'=>$rdv->getTelephone(),
'lieu'=>$rdv->getLieux()->getLibelle(),
'adresse'=>$rdv->getLieux()->getAdresse(),
'telAvocat'=>$rdv->getAvocats()->getTel(),
'emailAvocat'=>$rdv->getAvocats()->getEmail(),
'avocat'=>$rdv->getAvocats()->getNom().' '.$rdv->getAvocats()->getPrenom(),
'reponse'=>$_GET['reponse']
]
);
}
$mj = new \Mailjet\Client('9c42ddfa4db6f8f973a32c7087ea2d51',
'eace5eb7252f4ff3a1b483251360e04c', true,
['url' => "api.mailjet.com", 'version' => 'v4', 'call' => false]
);
if($etat != 'archiver')
{
/* $body = [
'Text' => $messageSMS,
'To' => $rdv->getTelephone(),
'From' => "LLVD",
];
$response = $mj->post(Resources::$SmsSend, ['body' => $body]);
$response->success();*/
// Données du message
/***************************** */
$data = array(
'key' => '0c33c31c02c1e3ef72692c7bec035c96',
'destinataires' => $rdv->getTelephone(),
'message' => $messageSMS,
'expediteur' => 'LLVD',
'date' => '',
'smslong' => 1
);
// Initialisation de cURL avec l'URL à appeler
$ch = curl_init('https://manager.envoyersmspro.com/api/envoyer/sms');
// Paramètres cURL pour activer le POST et retour de la réponse
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
// Passage des données
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data, '', '&'));
// Appel de l'API Envoyer SMS Pro et récupération de la réponse dans la variable $reponse_json
$reponse_json = curl_exec ($ch);
curl_close ($ch);
// Conversion JSON en tableau avec json_decode (http://fr2.php.net/manual/fr/function.json-decode.php)
$reponse_array = json_decode($reponse_json, true);
/*************************/
$to = $rdv->getEmail();
$mj = new \Mailjet\Client('9c42ddfa4db6f8f973a32c7087ea2d51',
'eace5eb7252f4ff3a1b483251360e04c',true,['version' => 'v3.1']);
$body = [
'Messages' => [
[
'From' => [
'Email' => "dontreply@llvd-avocats.be",
'Name' => "LLVD AVOCATS"
],
'To' => [
[
'Email' => $to,
]
],
'Bcc' => [ // 👈 C’est ici qu’on met Jocelyn en copie cachée
[
'Email' => 'jocelyn@piranha.lu',
]
],
'Subject' => $subject,
'HTMLPart' => $message,
]
]
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
$response->success();
}
if($page == 'liste')
{
return $this->redirectToRoute('listeRdvAConfirmer', ['etat'=>$etatRedirect], Response::HTTP_SEE_OTHER);
}
else
{
return $this->redirectToRoute('calendrier', ['id'=>$this->getUser()->getAvocats()->getId()], Response::HTTP_SEE_OTHER);
}
}
}