src/Controller/Front/ContactController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Form\ContactType;
  4. use App\Model\ContactDTO;
  5. use App\Repository\ContentRepository;
  6. use App\Services\MailService;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class ContactController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/contact", name="front_contact_page")
  14.      */
  15.     public function contactPage()
  16.     {
  17.         return $this->render('front/contact/contact_page.html.twig');
  18.     }
  19.     /**
  20.      * @Route("/contact/submit-form", name="front_contact_form")
  21.      */
  22.     public function contact(Request $requestMailService $mailServiceContentRepository $contentRepository)
  23.     {
  24.         $contactDTO = new ContactDTO();
  25.         $form $this->createForm(ContactType::class, $contactDTO)->handleRequest($request);
  26.         if ($form->isSubmitted() && $form->isValid()) {
  27.             if ($request->get('website') !== '') {
  28.                 return $this->json([
  29.                     'status' => 'error'
  30.                 ]);
  31.             }
  32.             $arrayErrors = [];
  33.             if (!$contactDTO->getFirstname()) {
  34.                 $arrayErrors['firstname'] = 'Veuillez renseigner votre prénom';
  35.             }
  36.             if (!$contactDTO->getLastname()) {
  37.                 $arrayErrors['lastname'] = 'Veuillez renseigner votre nom';
  38.             }
  39.             if (!$contactDTO->getEmail()) {
  40.                 $arrayErrors['email'] = 'Veuillez renseigner votre email';
  41.             }
  42.             if (!$contactDTO->getPhone()) {
  43.                 $arrayErrors['phone'] = 'Veuillez renseigner votre N° de téléphone';
  44.             }
  45.             if (!$contactDTO->getMessage()) {
  46.                 $arrayErrors['message'] = 'Veuillez renseigner le message';
  47.             }
  48.             if (!$contactDTO->getSign()) {
  49.                 $arrayErrors['message'] = 'Veuillez cocher la case';
  50.             }
  51.             if (!empty($arrayErrors)) {
  52.                 return $this->json([
  53.                     'status' => 'error',
  54.                     'errors' => $arrayErrors
  55.                 ]);
  56.             }
  57.             $mailService->sendMail($contactDTO);
  58.             return $this->json([
  59.                 'status' => 'ok'
  60.             ]);
  61.         }
  62.         return $this->render('front/inc/contact.html.twig', [
  63.             'form' => $form->createView(),
  64.             'contactContent' => $contentRepository->findOneBy(['page' => 'contact''section' => 'contact'])
  65.         ]);
  66.     }
  67. }