<?php
namespace App\Controller\Front;
use App\Controller\Core\BaseFrontController;
use App\Entity\FormContact;
use App\Form\Front\FormContactType;
use App\Manager\ContactManager;
use App\Manager\FormContactManager;
use App\Service\Mail\ContactMail;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class FormController extends BaseFrontController
{
const FORM_CONTACT = 'sd_form_contact_entity';
/**
* @Route("/contacto", name="contact", methods={"GET"})
* @Template("front/form/contact.html.twig")
*/
public function contact(ContactManager $contactManager): array
{
$entity = $this->getFormSession(self::FORM_CONTACT, new FormContact());
$form = $this->getFormView(FormContactType::class, $entity, 'contact_action');
$this->locals['sd'] = $contactManager->find(1);
$this->locals['form'] = $form;
return $this->locals;
}
/**
* @Route("/contacto", name="contact_action", methods={"POST"})
*/
public function contactAction(Request $request, FormContactManager $manager, ContactMail $mail): RedirectResponse
{
$entity = new FormContact();
$form = $this->getForm(FormContactType::class, $entity, 'contact_action');
$form->handleRequest($request);
if ($form->isValid() && $this->captchaValidate()) {
$manager->save($entity);
$mail->load($entity)->send();
return $this->redirectToRoute('thanks_slug', ['slug' => 'contacto']);
}
$this->saveFormSession(self::FORM_CONTACT, $entity);
return $this->redirectToRoute('contact');
}
}