<?php
namespace App\Controller\Blog;
use App\Entity\Pages\Pages;
use App\Entity\Pages\PagesHasBlocks;
use App\Form\Articles\ContactForm;
use App\Services\Core\Translations;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use App\Security\LoginFormAuthenticator;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Twig\Environment;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Part\DataPart;
class HomepageController extends AbstractController
{
private $em;
private $paginator;
private $params;
private $passwordEncoder;
private $tokenGenerator;
private $httpClient;
private $us;
private $ts;
private $authenticator;
private $guardHandler;
private $mail;
private $twig;
public function __construct(EntityManagerInterface $em,
PaginatorInterface $paginator,
UserPasswordEncoderInterface $passwordEncoder,
TokenGeneratorInterface $tokenGenerator,
ParameterBagInterface $params,
HttpClientInterface $httpClient,
\App\Services\Core\Users $us,
Translations $translationService,
LoginFormAuthenticator $authenticator,
GuardAuthenticatorHandler $guardHandler,
\App\Services\Mailing\Mails $mail,
Environment $twig
) {
$this->em = $em;
$this->paginator = $paginator;
$this->passwordEncoder = $passwordEncoder;
$this->tokenGenerator = $tokenGenerator;
$this->params = $params;
$this->httpClient = $httpClient;
$this->us = $us;
$this->ts = $translationService;
$this->authenticator = $authenticator;
$this->guardHandler = $guardHandler;
$this->mail = $mail;
$this->twig = $twig;
}
public function homepage(Request $request): Response
{
$locale = $request->getLocale();
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage','locale' => $locale]);
if($page == null) {
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'homepage','locale' => "en"]);
}
$blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod'],['sequence' => 'ASC']);
$page->setViews((int)$page->getViews() + 1);
$this->em->persist($page);
$this->em->flush();
return $this->render('vitrine/homepage.html.twig', [
'page' => $page,
'blocks' => $blocks
]);
}
public function error(Request $request): Response
{
$locale = $request->getLocale();
return $this->render('vitrine/error.html.twig');
}
public function contact(Request $request): Response
{
$session = $request->getSession();
$user = $this->getUser();
$locale = $request->getLocale();
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'contact','locale' => $locale]);
if($page == null) {
$page = $this->em->getRepository(Pages::class)->findOneBy(['name' => 'contact','locale' => "fr"]);
}
$blocks = $this->em->getRepository(PagesHasBlocks::class)->findBy(['page' => $page, 'type' => 'prod', 'startPage' => false],['sequence' => 'ASC']);
$page->setViews((int)$page->getViews() + 1);
$this->em->persist($page);
$this->em->flush();
$form = $this->createForm(ContactForm::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $request->request->all();
$data = $data['contact_form'];
// Honeypot antispam
if (!empty($data['website'])) {
if ($locale == "fr") {
return $this->redirectToRoute('contact');
}
return $this->redirectToRoute('locale_contact', ['_locale' => $locale]);
}
// reCAPTCHA v2 (optionnel via RECAPTCHA_ENABLED)
$recaptchaEnabled = filter_var($_ENV["RECAPTCHA_ENABLED"] ?? false, FILTER_VALIDATE_BOOLEAN);
if ($recaptchaEnabled) {
$recaptchaResponse = $request->request->get('g-recaptcha-response');
$captchaValid = false;
if (!empty($recaptchaResponse)) {
try {
$verify = $this->httpClient->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
'body' => [
'secret' => $_ENV["RECAPTCHA_SECRET_KEY"],
'response' => $recaptchaResponse,
'remoteip' => $request->getClientIp(),
],
]);
$result = $verify->toArray(false);
$captchaValid = $result['success'] ?? false;
} catch (\Throwable $e) {
$captchaValid = false;
}
}
if (!$captchaValid) {
if ($locale == "fr") {
$session->getFlashBag()->add('error', 'Veuillez valider le captcha');
return $this->redirectToRoute('contact');
}
$session->getFlashBag()->add('error', 'Please validate the captcha');
return $this->redirectToRoute('locale_contact', ['_locale' => $locale]);
}
}
$emailFrom = $_ENV["CONTACTFORM_EMAILFROM"];
$emailTo = $_ENV["CONTACTFORM_EMAILTO"];
$replaceTitle = $_ENV["CONTACTFORM_SUBJECT"];
$dsn = $_ENV["MAILER_DSN"];
$transport = Transport::fromDsn($dsn);
$mailer = new Mailer($transport);
$message = (new TemplatedEmail())
->from(new Address($emailFrom,$_ENV["CONTACTFORM_NAME"]))
->to($emailTo)
->subject($replaceTitle)
->html($this->twig->render("emails/contact.html.twig",[
'name' => $data['name'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'phone' => $data['phone'],
'enterprise' => $data['enterprise'],
'message' => $data['message'],
]))
;
$mailer->send($message);
if($locale == "fr") {
$session->getFlashBag()->add('success', 'Mail envoyé');
return $this->redirectToRoute('contact');
}
$session->getFlashBag()->add('success', 'Email sending');
return $this->redirectToRoute('locale_contact',['_locale' => $locale]);
}
return $this->render('vitrine/contact.html.twig', [
'page' => $page,
'blocks' => $blocks,
'form' => $form->createView()
]);
}
}