src/Controller/Front/SubscribersFrontController.php line 33

  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use OlaSoft\Common;
  9. use App\Entity\Subscribers;
  10. use App\Form\SubscribersType;
  11. /**
  12.  * @Route("", priority=1)
  13.  */
  14. class SubscribersFrontController extends AbstractController
  15. {
  16.     public function __construct(ManagerRegistry $doctrine){
  17.         $this->doctrine =  $doctrine;
  18.     }
  19.     
  20.     private $doctrine;
  21.     
  22.     public function getDoctrine()
  23.     {
  24.         return $this->doctrine;
  25.     }
  26.     
  27.     /**
  28.      * @Route("/inscription/", name="subscribers", defaults={"isEmbed"=false})
  29.      */
  30.     public function form(Request $request$isEmbed true)
  31.     {
  32.         $em $this->getDoctrine()->getManager();
  33.         $subs = new Subscribers();
  34.         $form $this->createForm(SubscribersType::class, $subs);
  35.         $form->handleRequest($request);
  36.         $captchaResponse $_POST['g-recaptcha-response'] ?? null;
  37.         if ($form->isSubmitted() && $form->isValid() && $captchaResponse){
  38.             $captchaUrl Common::getParameter("recaptcha")["url"];
  39.             $captchaSecret Common::getParameter("recaptcha")["secret"];
  40.             $userIP $_SERVER['REMOTE_ADDR'];
  41.             $captcha file_get_contents($captchaUrl '?secret=' $captchaSecret '&response=' $captchaResponse'&remoteip=' $userIP);
  42.             $captcha json_decode($captcha);
  43.             if($captcha->success){
  44.                 $exist $em->getRepository(Subscribers::class)->findOneByEmail($subs->getEmail());
  45.                 if(!$exist){
  46.                     $browser=Common::getBrowser(nulltrue);
  47.                     $subs->setIP($request->getClientIP());
  48.                     $subs->setBrowser($browser['name']);
  49.                     $subs->setOS($browser['platform']);
  50.                     $subs->setDate(new \DateTime);
  51.                     $em->persist($subs);
  52.                     $em->flush();
  53.                 }
  54.                 $this->addFlash('success','Félicitations.<br>Votre inscription a été effectuée avec succès.');
  55.                 return $this->redirect($request->headers->get('referer'));
  56.             }
  57.             $this->addFlash("error","Une erreur s'est produite, veuillez cocher le Captcha svp.");
  58.         }
  59.         return $this->render($isEmbed 'Default/subscribers-form.html.twig' 'Default/subscribers.html.twig',[
  60.             'form'=>$form->createView()
  61.         ]);
  62.     }
  63. }