<?php
namespace App\Controller\Front;
use App\Controller\Core\BaseFrontController;
use App\Entity\FormContact;
use App\Entity\Forms;
use App\Form\Front\FormClaimType;
use App\Form\Front\FormContactType;
use App\Manager\ClaimManager;
use App\Manager\ContactManager;
use App\Manager\FormClaimManager;
use App\Manager\FormContactManager;
use App\Manager\FormsManager;
use App\Service\Mail\ClaimMail;
use App\Service\Mail\ContactMail;
use DateInterval;
use DateTime;
use Exception;
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');
}
/**
* @Route("/libro-de-reclamaciones", name="claim", methods={"GET"})
* @Template("front/form/claim.html.twig")
*/
public function claim(ClaimManager $claimManager, FormClaimManager $formClaimManager, FormsManager $formsManager): array
{
/** @var Forms $forms */
$forms = $formsManager->find(1);
$responseDays = (int)$forms->getClaimResponseDays();
try {
$this->locals['answer_date'] = (new DateTime())->add(new DateInterval('P' . $responseDays . 'D'));
} catch (Exception $exception) {
$this->locals['answer_date'] = new DateTime();
}
$entity = $formClaimManager->create();
$code = $this->generateCode($formClaimManager->lastCode());
$entity->setCode($code);
$entity = $this->getFormSession('sd_form_claim_entity', $entity);
$this->saveFormSession('sd_form_claim_entity', $entity);
$form = $this->createForm(FormClaimType::class, $entity, [
'action' => $this->generateUrl('claim_action'),
'method' => 'POST',
])->createView();
$this->locals['sd'] = $claimManager->find(1);
$this->locals['form'] = $form;
$this->locals['entity'] = $entity;
return $this->locals;
}
/**
* @Route("/libro-de-reclamaciones", name="claim_action", methods={"POST"})
*/
public function claimAction(Request $request, FormsManager $formsManager, FormClaimManager $manager, ClaimMail $mail): RedirectResponse
{
/** @var Forms $forms */
$forms = $formsManager->find(1);
$entity = $manager->create();
$form = $this->createForm(FormClaimType::class, $entity, [
'action' => $this->generateUrl('claim_action'),
'method' => 'POST',
]);
$code = $this->generateCode($manager->lastCode());
$entity->setCode($code);
$responseDays = (int)$forms->getClaimResponseDays();
try {
$answerDate = (new DateTime())->add(new DateInterval('P' . $responseDays . 'D'));
} catch (\Exception $exception) {
$answerDate = new DateTime();
}
$entity->setAnswerDate($answerDate);
$form->handleRequest($request);
if ($form->isValid() && $this->captchaValidate()) {
$manager->save($entity);
$mail->load($entity)->send();
$mail->loadClient($entity)->send();
$this->requestStack->getSession()->remove('sd_claim_code');
$this->requestStack->getSession()->remove('sd_form_claim_entity');
return $this->redirectToRoute('claim_received');
}
$this->saveFormSession('sd_form_claim_entity', $entity);
return $this->redirectToRoute('claim');
}
public function generateCode($lastCode): string
{
$id = $this->lastCodeId($lastCode);
return 'R' . date('Y') . date('m') . '-' . str_pad((string)($id + 1), 6, '0', STR_PAD_LEFT);
}
public function lastCodeId($lastCode): int
{
$code = $this->lastCode($lastCode);
$array = explode('-', $code);
return count($array) == 2 ? (int)$array[1] : 0;
}
public function lastCode($lastCode): string
{
$code = $lastCode;
return is_null($code) ? 'R' . date('Y') . date('m') . '-' . '100000' : $code;
}
}