<?php
namespace App\Controller\front\Contact;
use ReCaptcha\ReCaptcha;
use App\Entity\Contact\Contact;
use App\Form\Contact\ContactType;
use App\Service\Mailer\MailerService;
use App\Controller\Admin\CoreController;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/contact')]
class ContactController extends CoreController
{
#[Route('/', name: 'contact_message')]
public function index(
Request $request,
MailerService $mailer,
ManagerRegistry $doctrine,
TranslatorInterface $translator
): Response {
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact, [
'idLangue' => $this->getCurrentLanguage(),
]);
$form->handleRequest($request);
if($form->isSubmitted() ) {
if($form->isValid()) {
$em = $doctrine->getManager();
$this->hydrateContact($form, $contact);
$to = $this->getParameter('email.to');
$from = $_ENV['EMAIL_FROM'];
$subject = $this->getParameter('email.subject');
$mailer->sendEmail(
$to,
'<html><body><h1>' . $subject . '</h1>' .'<h4>NOM:</h4>'.$form->get('name')->getData().'<br>'.'<h4>EMAIL:</h4>'.$form->get('email')->getData() .
'<br><h4>ENTREPRISE:</h4>'.$form->get('entreprise')->getData() . '<br><h4>TELEPHONE:</h4>'.$form->get('phone')->getData().'<br><h4>MESSAGE:</h4>'.$form->get('message')->getData() . '</body></html>',
$subject,
$from
);
$em->persist($contact);
$em->flush();
$this->addFlash('success', $translator->trans('front.form.msg_send'));
return $this->redirectToRoute('contact_message');
}
else {
$emailHoneypot = $form->get('email_honeypot')->getData();
if($emailHoneypot) {
$this->addFlash('danger', $translator->trans('Vous êtes considéré comme Bot/Spammer !.'));
}
}
}
return $this->render('front/contact/index.html.twig', [
'form' => $form->createView()
]);
}
private function hydrateContact(FormInterface $form, Contact $contact): void
{
$contact->setName($form->get('name')->getData());
$contact->setEntreprise($form->get('entreprise')->getData());
$contact->setPhone($form->get('phone')->getData());
$contact->setEmail($form->get('email')->getData());
$contact->setMessage($form->get('message')->getData());
$contact->setIsAccepted($form->get('isAccepted')->getData());
}
}