src/Controller/Front/SubscribersFrontController.php line 33
<?php
namespace App\Controller\Front;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Response;
use OlaSoft\Common;
use App\Entity\Subscribers;
use App\Form\SubscribersType;
/**
* @Route("", priority=1)
*/
class SubscribersFrontController extends AbstractController
{
public function __construct(ManagerRegistry $doctrine){
$this->doctrine = $doctrine;
}
private $doctrine;
public function getDoctrine()
{
return $this->doctrine;
}
/**
* @Route("/inscription/", name="subscribers", defaults={"isEmbed"=false})
*/
public function form(Request $request, $isEmbed = true)
{
$em = $this->getDoctrine()->getManager();
$subs = new Subscribers();
$form = $this->createForm(SubscribersType::class, $subs);
$form->handleRequest($request);
$captchaResponse = $_POST['g-recaptcha-response'] ?? null;
if ($form->isSubmitted() && $form->isValid() && $captchaResponse){
$captchaUrl = Common::getParameter("recaptcha")["url"];
$captchaSecret = Common::getParameter("recaptcha")["secret"];
$userIP = $_SERVER['REMOTE_ADDR'];
$captcha = file_get_contents($captchaUrl . '?secret=' . $captchaSecret . '&response=' . $captchaResponse. '&remoteip=' . $userIP);
$captcha = json_decode($captcha);
if($captcha->success){
$exist = $em->getRepository(Subscribers::class)->findOneByEmail($subs->getEmail());
if(!$exist){
$browser=Common::getBrowser(null, true);
$subs->setIP($request->getClientIP());
$subs->setBrowser($browser['name']);
$subs->setOS($browser['platform']);
$subs->setDate(new \DateTime);
$em->persist($subs);
$em->flush();
}
$this->addFlash('success','Félicitations.<br>Votre inscription a été effectuée avec succès.');
return $this->redirect($request->headers->get('referer'));
}
$this->addFlash("error","Une erreur s'est produite, veuillez cocher le Captcha svp.");
}
return $this->render($isEmbed ? 'Default/subscribers-form.html.twig' : 'Default/subscribers.html.twig',[
'form'=>$form->createView()
]);
}
}