<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Entity\Config;
use App\Form\ContactForm;
use App\Services\ContactService;
use App\Services\MailerService;
use App\Services\CallApiServices;
use COM;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Routing\Annotation\Route;
use ReCaptcha\ReCaptcha; // Include the recaptcha lib
class ContactController extends AbstractController
{
#[Route('/contact', name: 'app_contact')]
public function index(FlashBagInterface $flash, ContactService $contactService, MailerService $mailerService, Request $request, CallApiServices $callApiServices): Response
{
$serviceId = $this->getParameter('app.serviceId');
$accountId = $this->getParameter('app.accountId');
$contact = new Contact();
$config = $callApiServices->configuratationPlateforme($accountId, $serviceId);
$recaptchaKey = $config['recaptchaTab']['sitekey'];
$secretkey = $config['recaptchaTab']['secretkey'];
$messageError = "";
$form = $this->createForm(ContactForm::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$recaptcha = new ReCaptcha($secretkey);
$resp = $recaptcha->verify($request->request->get('g-recaptcha-response'), $request->getClientIp());
if (!$resp->isSuccess()) {
// Do something if the submit wasn't valid ! Use the message to show something
$messageError = "Le reCAPTCHA n'a pas été saisi correctement. Réessayez.";
} else {
$contact = $form->getData();
$contactService->sendEmail($contact);
return $this->redirectToRoute('app_contact');
}
}
return $this->render('contact/index.html.twig', [
"contactForm" => $form->createView(),
"recaptchaKey" => $recaptchaKey,
"messageError" => $messageError,
]);
}
}