<?php
// src/Controller/DefaultController.php
namespace App\Controller;
use App\Entity\User;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Contacts;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Request;
use App\Security\Encoder\CustomEncoder;
class DefaultController extends AbstractController
{
private $mailer;
private $request;
public function __construct(\Swift_Mailer $mailer,CustomEncoder $customEncoder)
{
$this->mailer = $mailer;
$this->encoder = $customEncoder;
}
/**
* @Route("/", name="homepage")
*/
public function index()
{
/** @var User $user */
$user = $this->getUser();
if($user->hasRole('ROLE_APPRENANT') && !$user->hasRole('ROLE_INTERVENANT')) {
return $this->redirect('https://elearning.biopraxia.com'); // TODO use .env data !!
}
return $this->redirect($this->generateUrl('eleves'));
}
/**
* @Route("/rgpdform/{token}/{id}", name="rgpd_form", defaults={"token"="", "id"=0})
*
*/
public function RgpdResponse($token, $id)
{
if(empty($token) || $id==0):
return new Response("Vous n'êtes pas autorisé à accéder à cette page", 403);
endif;
$erreur="";
if( $this->CheckContactToken($id, $token)):
$session = new Session();
$session->set('type', 'contact');
$session->set('token', $token);
$session->set('id', $id);
elseif( $this->CheckReferentToken($id, $token) ):
$session = new Session();
$session->set('type', 'referent');
$session->set('token', $token);
$session->set('id', $id);
else:
$erreur="La requête n'est pas valide ou le token a déjà été utilisé";
endif;
return $this->render('rgpd/rgpdform.html.twig', array('erreur'=>$erreur));
}
/**
* @Route("/rgpdautoresponse/{token}/{id}/{type}", name="rgpd_autoresponse", defaults={"token"="", "id"=0, "type"=""})
*
*/
public function RgpdAutoResponse($token, $id, $type)
{
if (empty($token) || $id == 0 || empty($type)):
return new Response("Vous n'êtes pas autorisé à accéder à cette page", 403);
endif;
$erreur = '';
$result = [];
if ($this->CheckContactToken($id, $token)):
$result = $this->SaveContactChoix($id, $type);
elseif ($this->CheckReferentToken($id, $token)):
$result = $this->SaveReferentChoix($id, $type);
else:
$erreur = "La requête n'est pas valide ou le token a déjà été utilisé";
endif;
return $this->render('rgpd/rgpdform.html.twig', array('erreur' => $erreur, 'result' => $result, 'action' => 'end'));
}
/**
* @Route("/rgpdformresponse/response", name="rgpd_response")
*
*/
public function RgpdChoixResponse(Request $request)
{
$erreur="";
$result="";
$session = $request->getSession();
if(!$session->has('id') || !$session->has('type') || !$session->has('token')):
$erreur="Des paramétres sont manquants ou invalides, impossible de traiter cette demande";
elseif($this->CheckContactToken($session->get('id'), $session->get('token'))):
$result=$this->SaveContactChoix($session->get('id'), $request->request->get('RgpdChoix'));
elseif($this->CheckReferentToken($session->get('id'), $session->get('token'))):
$result=$this->SaveReferentChoix($session->get('id'), $request->request->get('RgpdChoix'));
else:
$erreur="Des paramétres sont manquants ou invalides, impossible de traiter cette demande";
endif;
$session->clear();
return $this->render('rgpd/rgpdform.html.twig', array('erreur'=>$erreur, 'result'=>$result, 'action'=>'end'));
}
private function SaveReferentChoix($contact_id, $choix)
{
$em = $this->getDoctrine()->getManager();
$contactEnvironnement = $this->getDoctrine()->getManager()->getRepository(Contacts::class);
$contact = $contactEnvironnement->find(array('xcontact'=>$contact_id));
try{
if($choix=='true'):
$contact->setReferentRgpdValid(1);
$contact->setReferentRgpdValiddate(new \DateTime('now'));
$em->persist($contact);
$em->flush();
$response="Votre accord a bien été enregistré, nous vous remercions";
$this->SendResponse($contact->getEmailreferent(), 'ok');
else:
$this->SendResponse($contact->getEmailreferent(), 'ko');
$contact->setReferentRgpdValid(-1);
$contact->setReferentRgpdValiddate(new \DateTime('now'));
$contact->setNomreferent(null);
$contact->setPrenomreferent(null);
$contact->setTelreferent(null);
$contact->setEmailreferent(null);
$contact->setAdr1referent(null);
$contact->setAdr2referent(null);
$contact->setAdr3referent(null);
$contact->setCodepostalreferent(null);
$contact->setVillereferent(null);
$em->persist($contact);
$em->flush();
$response="Conformément à votre choix, toutes les données vous concernant ont été détruites ";
endif;
}
catch (\Exception $e)
{
$response = $e->getMessage();
}
return $response;
}
private function SaveContactChoix($contact_id, $choix)
{
$em = $this->getDoctrine()->getManager();
$contactEnvironnement = $this->getDoctrine()->getManager()->getRepository(Contacts::class);
$contact = $contactEnvironnement->find(array('xcontact' => $contact_id));
try {
if ($choix == 'true'):
$contact->setContactRgpdValid(1);
$contact->setContactRgpdValiddate(new \DateTime('now'));
$em->persist($contact);
$em->flush();
$response = "Votre accord a bien été enregistré, nous vous remercions";
$this->SendResponse($contact->getEmail(), 'ok');
else:
$em->remove($contact);
$em->flush();
$response = "Conformément à votre choix, toutes les données vous concernant ont été détruites ";
$this->SendResponse($contact->getEmail(), 'ko');
endif;
} catch (\Exception $e) {
$response = $e->getMessage();
}
return $response;
}
private function CheckContactToken($contact_id, $token)
{
$contactEnvironnement = $this->getDoctrine()->getManager()->getRepository(Contacts::class);
$contact = $contactEnvironnement->find(array('xcontact'=>$contact_id));
if(empty($contact)):
return false;
elseif( ($contact->getContactRgpdToken()==$token && $contact->getContactRgpdValid()==0) ):
return true;
else:
return false;
endif;
}
private function CheckReferentToken($contact_id, $token)
{
$contactEnvironnement = $this->getDoctrine()->getManager()->getRepository(Contacts::class);
$contact = $contactEnvironnement->find(array('xcontact'=>$contact_id));
if(empty($contact)):
return false;
elseif( ($contact->getReferentRgpdToken()==$token && $contact->getReferentRgpdValid()==0) ):
return true;
else:
return false;
endif;
}
public function SendRgpd($type='contact', $contact_id, $hostname)
{
$contactEnvironnement = $this->getDoctrine()->getManager()->getRepository(Contacts::class);
$contact = $contactEnvironnement->find($contact_id);
$date = new \DateTime();
$token = sha1($date->format('YmdHisphp bin/console').rand(0,1000).$contact->getId());
$link_ok = $hostname.'/rgpdautoresponse/'.$token."/".$contact->getId()."/true"; // Pourquoi ne pas passer par le Router de SF ? oO
$link_ko = $hostname.'/rgpdautoresponse/'.$token."/".$contact->getId()."/false";
$result=0;
if($type=='contact'):
$destinataire=$contact->getEmail();
$datas="nom et prénom";
(!empty($contact->getDnaissance()))?$datas.=", date de naissance":"";
(!empty($contact->getAdr1()))?$datas.=", adresse":"";
(!empty($contact->getTelFixe()))?$datas.=", tél fixe":"";
(!empty($contact->getTelMobile()))?$datas.=", tél mobile":"";
(!empty($contact->getFax()))?$datas.=", fax":"";
(!empty($contact->getEmail()))?$datas.=", email":"";
(!empty($contact->getPhoto()))?$datas.=", photo d'identité":"";
(!empty($contact->getNomurgence()))?$datas.=", personne à contacter en cas d'urgence":"";
(!empty($contact->getNomreferent()))?$datas.=", personne référente":"";
$datas.=", vos inscriptions, vos participations à nos évènements, un historique du suivi administratif de votre dossier scolaire";
$ResultSend=$this->Sendmail($destinataire, $datas, $hostname, $link_ok, $link_ko);
if($ResultSend):
$contact->setContactRgpdSendDate(new \DateTime('now'));
$contact->setContactRgpdSend(1);
$contact->setContactRgpdValid(0);
$contact->setContactRgpdValidDate(null);
$contact->setContactRgpdToken($token);
$em = $this->getDoctrine()->getManager();
$em->persist($contact);
$em->flush();
$result=1;
endif;
endif;
if ($type == 'referent'):
$destinataire = $contact->getEmailreferent();
$datas = "nom et prénom";
(!empty($contact->getDnaissance())) ? $datas .= ", date de Naissance": "";
(!empty($contact->getAdr1referent())) ? $datas .= ", adresse": "";
(!empty($contact->getTelreferent())) ? $datas .= ", tél fixe": "";
(!empty($contact->getEmailreferent())) ? $datas .= ", email" : "";
$datas .= "</ul>";
$ResultSend = $this->Sendmail($destinataire, $datas, $hostname, $link_ok, $link_ko);
if ($ResultSend):
$contact->setReferentRgpdSendDate(new \DateTime('now'));
$contact->setReferentRgpdSend(1);
$contact->setReferentRgpdValid(0);
$contact->setReferentRgpdValidDate(null);
$contact->setReferentRgpdToken($token);
$em = $this->getDoctrine()->getManager();
$em->persist($contact);
$em->flush();
$result = 1;
endif;
endif;
return $result;
}
private function Sendmail($destinataire, $datas, $hostname, $link_ok, $link_ko)
{
$body = $this->renderView(
'Emails/rgpd.eleve.html.twig',
array(
'datas' => $datas,
'hostname' => $hostname,
'link_ok' => $link_ok,
'link_ko' => $link_ko
)
);
$message = (new \Swift_Message('Biopraxia - RGPD'))
->setFrom($this->getParameter('mail_reply'))
->setTo($destinataire)
->setBody($body, 'text/html');
$res = $this->mailer->send($message);
return $res;
}
private function SendResponse($destinataire, $type)
{
$message = (new \Swift_Message('Biopraxia - RGPD'))
->setFrom($this->getParameter('mail_reply'))
->setTo($destinataire)
->setBody(
$this->renderView(
// templates/emails/registration.html.twig
'Emails/reponse'.$type.'rgpd.eleve.html.twig'
),
'text/html'
);
$res=$this->mailer->send($message);
return $res;
}
}