src/Controller/DefaultController.php line 29

Open in your IDE?
  1. <?php
  2. // src/Controller/DefaultController.php
  3. namespace App\Controller;
  4. use App\Entity\User;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use App\Entity\Contacts;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use App\Security\Encoder\CustomEncoder;
  12. class DefaultController extends AbstractController
  13. {
  14.     private $mailer;
  15.     private $request;
  16.     public function __construct(\Swift_Mailer $mailer,CustomEncoder $customEncoder)
  17.     {
  18.         $this->mailer $mailer;
  19.         $this->encoder $customEncoder;
  20.     }
  21.     /**
  22.      * @Route("/", name="homepage")
  23.      */
  24.     public function index()
  25.     {
  26.         /** @var User $user */
  27.         $user $this->getUser();
  28.         if($user->hasRole('ROLE_APPRENANT') && !$user->hasRole('ROLE_INTERVENANT')) {
  29.             return $this->redirect('https://elearning.biopraxia.com'); // TODO use .env data !!
  30.         }
  31.         return $this->redirect($this->generateUrl('eleves'));
  32.     }
  33.     /**
  34.      * @Route("/rgpdform/{token}/{id}", name="rgpd_form", defaults={"token"="", "id"=0})
  35.      *
  36.      */
  37.     public function RgpdResponse($token$id)
  38.     {
  39.         if(empty($token) || $id==0):
  40.             return new Response("Vous n'êtes pas autorisé à accéder à cette page"403);
  41.         endif;
  42.         $erreur="";
  43.         if( $this->CheckContactToken($id$token)):
  44.             $session = new Session();
  45.             $session->set('type''contact');
  46.             $session->set('token'$token);
  47.             $session->set('id'$id);
  48.         elseif( $this->CheckReferentToken($id$token) ):
  49.             $session = new Session();
  50.             $session->set('type''referent');
  51.             $session->set('token'$token);
  52.             $session->set('id'$id);
  53.         else:
  54.             $erreur="La requête n'est pas valide ou le token a déjà été utilisé";
  55.         endif;
  56.         return $this->render('rgpd/rgpdform.html.twig', array('erreur'=>$erreur));
  57.     }
  58.     /**
  59.      * @Route("/rgpdautoresponse/{token}/{id}/{type}", name="rgpd_autoresponse", defaults={"token"="", "id"=0, "type"=""})
  60.      *
  61.      */
  62.     public function RgpdAutoResponse($token$id$type)
  63.     {
  64.         if (empty($token) || $id == || empty($type)):
  65.             return new Response("Vous n'êtes pas autorisé à accéder à cette page"403);
  66.         endif;
  67.         $erreur '';
  68.         $result = [];
  69.         if ($this->CheckContactToken($id$token)):
  70.             $result $this->SaveContactChoix($id$type);
  71.         elseif ($this->CheckReferentToken($id$token)):
  72.             $result $this->SaveReferentChoix($id$type);
  73.         else:
  74.             $erreur "La requête n'est pas valide ou le token a déjà été utilisé";
  75.         endif;
  76.         return $this->render('rgpd/rgpdform.html.twig', array('erreur' => $erreur'result' => $result'action' => 'end'));
  77.     }
  78.     /**
  79.      * @Route("/rgpdformresponse/response", name="rgpd_response")
  80.      *
  81.      */
  82.     public function RgpdChoixResponse(Request $request)
  83.     {
  84.         $erreur="";
  85.         $result="";
  86.         $session  $request->getSession();
  87.         if(!$session->has('id') || !$session->has('type') || !$session->has('token')):
  88.             $erreur="Des paramétres sont manquants ou invalides, impossible de traiter cette demande";
  89.         elseif($this->CheckContactToken($session->get('id'), $session->get('token'))):
  90.             $result=$this->SaveContactChoix($session->get('id'), $request->request->get('RgpdChoix'));
  91.         elseif($this->CheckReferentToken($session->get('id'), $session->get('token'))):
  92.             $result=$this->SaveReferentChoix($session->get('id'), $request->request->get('RgpdChoix'));
  93.         else:
  94.             $erreur="Des paramétres sont manquants ou invalides, impossible de traiter cette demande";
  95.         endif;
  96.         $session->clear();
  97.         return $this->render('rgpd/rgpdform.html.twig', array('erreur'=>$erreur'result'=>$result'action'=>'end'));
  98.     }
  99.     private function SaveReferentChoix($contact_id$choix)
  100.     {
  101.         $em                   $this->getDoctrine()->getManager();
  102.         $contactEnvironnement $this->getDoctrine()->getManager()->getRepository(Contacts::class);
  103.         $contact              $contactEnvironnement->find(array('xcontact'=>$contact_id));
  104.         try{
  105.             if($choix=='true'):
  106.                 $contact->setReferentRgpdValid(1);
  107.                 $contact->setReferentRgpdValiddate(new \DateTime('now'));
  108.                 $em->persist($contact);
  109.                 $em->flush();
  110.                 $response="Votre accord a bien été enregistré, nous vous remercions";
  111.                 $this->SendResponse($contact->getEmailreferent(), 'ok');
  112.             else:
  113.                 $this->SendResponse($contact->getEmailreferent(), 'ko');
  114.                 $contact->setReferentRgpdValid(-1);
  115.                 $contact->setReferentRgpdValiddate(new \DateTime('now'));
  116.                 $contact->setNomreferent(null);
  117.                 $contact->setPrenomreferent(null);
  118.                 $contact->setTelreferent(null);
  119.                 $contact->setEmailreferent(null);
  120.                 $contact->setAdr1referent(null);
  121.                 $contact->setAdr2referent(null);
  122.                 $contact->setAdr3referent(null);
  123.                 $contact->setCodepostalreferent(null);
  124.                 $contact->setVillereferent(null);
  125.                 $em->persist($contact);
  126.                 $em->flush();
  127.                 $response="Conformément à votre choix, toutes les données vous concernant ont été détruites ";
  128.             endif;
  129.         }
  130.         catch (\Exception $e)
  131.         {
  132.             $response $e->getMessage();
  133.         }
  134.         return $response;
  135.     }
  136.     private function SaveContactChoix($contact_id$choix)
  137.     {
  138.         $em $this->getDoctrine()->getManager();
  139.         $contactEnvironnement $this->getDoctrine()->getManager()->getRepository(Contacts::class);
  140.         $contact $contactEnvironnement->find(array('xcontact' => $contact_id));
  141.         try {
  142.             if ($choix == 'true'):
  143.                 $contact->setContactRgpdValid(1);
  144.                 $contact->setContactRgpdValiddate(new \DateTime('now'));
  145.                 $em->persist($contact);
  146.                 $em->flush();
  147.                 $response "Votre accord a bien été enregistré, nous vous remercions";
  148.                 $this->SendResponse($contact->getEmail(), 'ok');
  149.             else:
  150.                 $em->remove($contact);
  151.                 $em->flush();
  152.                 $response "Conformément à votre choix, toutes les données vous concernant ont été détruites ";
  153.                 $this->SendResponse($contact->getEmail(), 'ko');
  154.             endif;
  155.         } catch (\Exception $e) {
  156.             $response $e->getMessage();
  157.         }
  158.         return $response;
  159.     }
  160.     private function CheckContactToken($contact_id$token)
  161.     {
  162.         $contactEnvironnement $this->getDoctrine()->getManager()->getRepository(Contacts::class);
  163.         $contact              $contactEnvironnement->find(array('xcontact'=>$contact_id));
  164.         if(empty($contact)):
  165.             return false;
  166.         elseif( ($contact->getContactRgpdToken()==$token && $contact->getContactRgpdValid()==0) ):
  167.             return true;
  168.         else:
  169.             return false;
  170.         endif;
  171.     }
  172.     private function CheckReferentToken($contact_id$token)
  173.     {
  174.         $contactEnvironnement $this->getDoctrine()->getManager()->getRepository(Contacts::class);
  175.         $contact              $contactEnvironnement->find(array('xcontact'=>$contact_id));
  176.         if(empty($contact)):
  177.             return false;
  178.         elseif( ($contact->getReferentRgpdToken()==$token && $contact->getReferentRgpdValid()==0) ):
  179.            return true;
  180.         else:
  181.             return false;
  182.         endif;
  183.     }
  184.     public function SendRgpd($type='contact'$contact_id$hostname)
  185.     {
  186.         $contactEnvironnement $this->getDoctrine()->getManager()->getRepository(Contacts::class);
  187.         $contact                   $contactEnvironnement->find($contact_id);
  188.         $date = new \DateTime();
  189.         $token sha1($date->format('YmdHisphp bin/console').rand(0,1000).$contact->getId());
  190.         $link_ok $hostname.'/rgpdautoresponse/'.$token."/".$contact->getId()."/true"// Pourquoi ne pas passer par le Router de SF ? oO
  191.         $link_ko $hostname.'/rgpdautoresponse/'.$token."/".$contact->getId()."/false";
  192.         $result=0;
  193.         if($type=='contact'):
  194.             $destinataire=$contact->getEmail();
  195.             $datas="nom et prénom";
  196.             (!empty($contact->getDnaissance()))?$datas.=", date de naissance":"";
  197.             (!empty($contact->getAdr1()))?$datas.=", adresse":"";
  198.             (!empty($contact->getTelFixe()))?$datas.=", tél fixe":"";
  199.             (!empty($contact->getTelMobile()))?$datas.=", tél mobile":"";
  200.             (!empty($contact->getFax()))?$datas.=", fax":"";
  201.             (!empty($contact->getEmail()))?$datas.=", email":"";
  202.             (!empty($contact->getPhoto()))?$datas.=", photo d'identité":"";
  203.             (!empty($contact->getNomurgence()))?$datas.=", personne à contacter en cas d'urgence":"";
  204.             (!empty($contact->getNomreferent()))?$datas.=", personne référente":"";
  205.             $datas.=", vos inscriptions, vos participations à nos évènements, un historique du suivi administratif de votre dossier scolaire";
  206.             $ResultSend=$this->Sendmail($destinataire$datas$hostname$link_ok$link_ko);
  207.             if($ResultSend):
  208.                 $contact->setContactRgpdSendDate(new \DateTime('now'));
  209.                 $contact->setContactRgpdSend(1);
  210.                 $contact->setContactRgpdValid(0);
  211.                 $contact->setContactRgpdValidDate(null);
  212.                 $contact->setContactRgpdToken($token);
  213.                 $em $this->getDoctrine()->getManager();
  214.                 $em->persist($contact);
  215.                 $em->flush();
  216.                 $result=1;
  217.             endif;
  218.         endif;
  219.         if ($type == 'referent'):
  220.             $destinataire $contact->getEmailreferent();
  221.             $datas        "nom et prénom";
  222.             (!empty($contact->getDnaissance())) ? $datas .= ", date de Naissance""";
  223.             (!empty($contact->getAdr1referent())) ? $datas .= ", adresse""";
  224.             (!empty($contact->getTelreferent())) ? $datas .= ", tél fixe""";
  225.             (!empty($contact->getEmailreferent())) ? $datas .= ", email" "";
  226.             $datas .= "</ul>";
  227.             $ResultSend $this->Sendmail($destinataire$datas$hostname$link_ok$link_ko);
  228.             if ($ResultSend):
  229.                 $contact->setReferentRgpdSendDate(new \DateTime('now'));
  230.                 $contact->setReferentRgpdSend(1);
  231.                 $contact->setReferentRgpdValid(0);
  232.                 $contact->setReferentRgpdValidDate(null);
  233.                 $contact->setReferentRgpdToken($token);
  234.                 $em $this->getDoctrine()->getManager();
  235.                 $em->persist($contact);
  236.                 $em->flush();
  237.                 $result 1;
  238.             endif;
  239.         endif;
  240.         return $result;
  241.     }
  242.     private function Sendmail($destinataire$datas$hostname$link_ok$link_ko)
  243.     {
  244.         $body $this->renderView(
  245.             'Emails/rgpd.eleve.html.twig',
  246.             array(
  247.                 'datas' => $datas,
  248.                 'hostname' => $hostname,
  249.                 'link_ok' => $link_ok,
  250.                 'link_ko' => $link_ko
  251.             )
  252.         );
  253.         $message = (new \Swift_Message('Biopraxia - RGPD'))
  254.             ->setFrom($this->getParameter('mail_reply'))
  255.             ->setTo($destinataire)
  256.             ->setBody($body'text/html');
  257.         $res $this->mailer->send($message);
  258.         return $res;
  259.     }
  260.     private function SendResponse($destinataire$type)
  261.     {
  262.         $message = (new \Swift_Message('Biopraxia - RGPD'))
  263.             ->setFrom($this->getParameter('mail_reply'))
  264.             ->setTo($destinataire)
  265.             ->setBody(
  266.                 $this->renderView(
  267.                 // templates/emails/registration.html.twig
  268.                     'Emails/reponse'.$type.'rgpd.eleve.html.twig'
  269.                 ),
  270.                 'text/html'
  271.             );
  272.         $res=$this->mailer->send($message);
  273.         return $res;
  274.     }
  275. }