Add contact page
This commit is contained in:
@@ -3,18 +3,26 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\News;
|
||||
use App\Entity\Contact;
|
||||
use App\Form\ContactType;
|
||||
use App\Service\MailService;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exception;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class PageController extends AbstractController
|
||||
{
|
||||
private EntityManagerInterface $entityManager;
|
||||
private MailService $mailService;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager)
|
||||
public function __construct(EntityManagerInterface $entityManager, MailService $mailService)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->mailService = $mailService;
|
||||
}
|
||||
|
||||
#[Route('/', name: 'home')]
|
||||
@@ -47,6 +55,43 @@ final class PageController extends AbstractController
|
||||
return $this->render('page/show.html.twig', []);
|
||||
}
|
||||
|
||||
#[Route('/contact', name: 'contact')]
|
||||
public function contact(Request $request): Response
|
||||
{
|
||||
$form = $this->createForm(ContactType::class);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$contact = new Contact();
|
||||
$contact->setSendAt(new DateTimeImmutable());
|
||||
$data = $form->getData();
|
||||
|
||||
$honeyPot = $form->getData()['mobilePhoneNumber'];
|
||||
if ($honeyPot !== NULL) {
|
||||
$contact->setIsValid(false);
|
||||
$this->addFlash('success', 'Votre message a été envoyé avec succès.');
|
||||
$this->entityManager->persist($contact);
|
||||
$this->entityManager->flush();
|
||||
return $this->redirectToRoute('contact');
|
||||
}
|
||||
try {
|
||||
$this->mailService->sendContactMail($data);
|
||||
$contact->setIsValid(true);
|
||||
$this->addFlash('success', 'Votre message a été envoyé avec succès.');
|
||||
} catch (Exception $e) {
|
||||
$contact->setIsValid(false);
|
||||
$this->addFlash('error', 'Une erreur est survenue lors de l’envoi du message.');
|
||||
}
|
||||
$this->entityManager->persist($contact);
|
||||
$this->entityManager->flush();
|
||||
return $this->redirectToRoute('contact');
|
||||
}
|
||||
|
||||
return $this->render('page/contact.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/mentions-legales', name: 'legalmentions')]
|
||||
public function legalmentions(): Response
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user