src/Controller/front/Contact/ContactController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller\front\Contact;
  3. use ReCaptcha\ReCaptcha;
  4. use App\Entity\Contact\Contact;
  5. use App\Form\Contact\ContactType;
  6. use App\Service\Mailer\MailerService;
  7. use App\Controller\Admin\CoreController;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Symfony\Component\Form\FormInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. #[Route('/contact')]
  15. class ContactController extends CoreController
  16. {
  17.     
  18.     #[Route('/'name'contact_message')]
  19.     public function index(
  20.         Request $request,
  21.         MailerService $mailer,
  22.         ManagerRegistry $doctrine,
  23.         TranslatorInterface $translator
  24.     ): Response {
  25.         $contact = new Contact();
  26.     
  27.         $form $this->createForm(ContactType::class, $contact, [
  28.             'idLangue' => $this->getCurrentLanguage(),
  29.         ]);
  30.         $form->handleRequest($request);
  31.     
  32.         if($form->isSubmitted() ) {
  33.             if($form->isValid()) {
  34.                 $em $doctrine->getManager();
  35.                 $this->hydrateContact($form$contact);
  36.                 $to $this->getParameter('email.to');
  37.                 $from =  $_ENV['EMAIL_FROM'];
  38.                 $subject $this->getParameter('email.subject');
  39.                 $mailer->sendEmail(
  40.                     $to,
  41.                     '<html><body><h1>' $subject '</h1>' .'<h4>NOM:</h4>'.$form->get('name')->getData().'<br>'.'<h4>EMAIL:</h4>'.$form->get('email')->getData() .
  42.                     '<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>',
  43.                     $subject,
  44.                     $from
  45.                 );
  46.                     
  47.                 $em->persist($contact);
  48.                 $em->flush();
  49.                 $this->addFlash('success'$translator->trans('front.form.msg_send'));
  50.                 return $this->redirectToRoute('contact_message');
  51.             }
  52.             else {
  53.                 $emailHoneypot $form->get('email_honeypot')->getData();
  54.                 if($emailHoneypot) {
  55.                     $this->addFlash('danger'$translator->trans('Vous êtes considéré comme Bot/Spammer !.'));
  56.                 }
  57.             }
  58.         }
  59.            
  60.         return $this->render('front/contact/index.html.twig', [
  61.             'form' => $form->createView()
  62.         ]);
  63.     }
  64.     
  65.     private function hydrateContact(FormInterface $formContact $contact): void
  66.     {
  67.         $contact->setName($form->get('name')->getData());
  68.         $contact->setEntreprise($form->get('entreprise')->getData());
  69.         $contact->setPhone($form->get('phone')->getData());
  70.         $contact->setEmail($form->get('email')->getData());
  71.         $contact->setMessage($form->get('message')->getData());
  72.         $contact->setIsAccepted($form->get('isAccepted')->getData());
  73.     }
  74.     
  75.  
  76. }