<?php
namespace App\Controller\Front;
use App\Form\ContactType;
use App\Model\ContactDTO;
use App\Repository\ContentRepository;
use App\Services\MailService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class ContactController extends AbstractController
{
/**
* @Route("/contact", name="front_contact_page")
*/
public function contactPage()
{
return $this->render('front/contact/contact_page.html.twig');
}
/**
* @Route("/contact/submit-form", name="front_contact_form")
*/
public function contact(Request $request, MailService $mailService, ContentRepository $contentRepository)
{
$contactDTO = new ContactDTO();
$form = $this->createForm(ContactType::class, $contactDTO)->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($request->get('website') !== '') {
return $this->json([
'status' => 'error'
]);
}
$arrayErrors = [];
if (!$contactDTO->getFirstname()) {
$arrayErrors['firstname'] = 'Veuillez renseigner votre prénom';
}
if (!$contactDTO->getLastname()) {
$arrayErrors['lastname'] = 'Veuillez renseigner votre nom';
}
if (!$contactDTO->getEmail()) {
$arrayErrors['email'] = 'Veuillez renseigner votre email';
}
if (!$contactDTO->getPhone()) {
$arrayErrors['phone'] = 'Veuillez renseigner votre N° de téléphone';
}
if (!$contactDTO->getMessage()) {
$arrayErrors['message'] = 'Veuillez renseigner le message';
}
if (!$contactDTO->getSign()) {
$arrayErrors['message'] = 'Veuillez cocher la case';
}
if (!empty($arrayErrors)) {
return $this->json([
'status' => 'error',
'errors' => $arrayErrors
]);
}
$mailService->sendMail($contactDTO);
return $this->json([
'status' => 'ok'
]);
}
return $this->render('front/inc/contact.html.twig', [
'form' => $form->createView(),
'contactContent' => $contentRepository->findOneBy(['page' => 'contact', 'section' => 'contact'])
]);
}
}