src/Controller/Front/FormController.php line 66

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Controller\Core\BaseFrontController;
  4. use App\Entity\FormContact;
  5. use App\Entity\Forms;
  6. use App\Form\Front\FormClaimType;
  7. use App\Form\Front\FormContactType;
  8. use App\Manager\ClaimManager;
  9. use App\Manager\ContactManager;
  10. use App\Manager\FormClaimManager;
  11. use App\Manager\FormContactManager;
  12. use App\Manager\FormsManager;
  13. use App\Service\Mail\ClaimMail;
  14. use App\Service\Mail\ContactMail;
  15. use DateInterval;
  16. use DateTime;
  17. use Exception;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. class FormController extends BaseFrontController
  23. {
  24.     const FORM_CONTACT 'sd_form_contact_entity';
  25.     /**
  26.      * @Route("/contacto", name="contact", methods={"GET"})
  27.      * @Template("front/form/contact.html.twig")
  28.      */
  29.     public function contact(ContactManager $contactManager): array
  30.     {
  31.         $entity $this->getFormSession(self::FORM_CONTACT, new FormContact());
  32.         $form $this->getFormView(FormContactType::class, $entity'contact_action');
  33.         $this->locals['sd'] = $contactManager->find(1);
  34.         $this->locals['form'] = $form;
  35.         return $this->locals;
  36.     }
  37.     /**
  38.      * @Route("/contacto", name="contact_action", methods={"POST"})
  39.      */
  40.     public function contactAction(Request $requestFormContactManager $managerContactMail $mail): RedirectResponse
  41.     {
  42.         $entity = new FormContact();
  43.         $form $this->getForm(FormContactType::class, $entity'contact_action');
  44.         $form->handleRequest($request);
  45.         if ($form->isValid() && $this->captchaValidate()) {
  46.             $manager->save($entity);
  47.             $mail->load($entity)->send();
  48.             return $this->redirectToRoute('thanks_slug', ['slug' => 'contacto']);
  49.         }
  50.         $this->saveFormSession(self::FORM_CONTACT$entity);
  51.         return $this->redirectToRoute('contact');
  52.     }
  53.     /**
  54.      * @Route("/libro-de-reclamaciones", name="claim", methods={"GET"})
  55.      * @Template("front/form/claim.html.twig")
  56.      */
  57.     public function claim(ClaimManager $claimManagerFormClaimManager $formClaimManagerFormsManager $formsManager): array
  58.     {
  59.         /** @var Forms $forms */
  60.         $forms $formsManager->find(1);
  61.         $responseDays = (int)$forms->getClaimResponseDays();
  62.         try {
  63.             $this->locals['answer_date'] = (new DateTime())->add(new DateInterval('P' $responseDays 'D'));
  64.         } catch (Exception $exception) {
  65.             $this->locals['answer_date'] = new DateTime();
  66.         }
  67.         $entity $formClaimManager->create();
  68.         $code $this->generateCode($formClaimManager->lastCode());
  69.         $entity->setCode($code);
  70.         $entity $this->getFormSession('sd_form_claim_entity'$entity);
  71.         $this->saveFormSession('sd_form_claim_entity'$entity);
  72.         $form $this->createForm(FormClaimType::class, $entity, [
  73.             'action' => $this->generateUrl('claim_action'),
  74.             'method' => 'POST',
  75.         ])->createView();
  76.         $this->locals['sd'] = $claimManager->find(1);
  77.         $this->locals['form'] = $form;
  78.         $this->locals['entity'] = $entity;
  79.         return $this->locals;
  80.     }
  81.     /**
  82.      * @Route("/libro-de-reclamaciones", name="claim_action", methods={"POST"})
  83.      */
  84.     public function claimAction(Request $requestFormsManager $formsManagerFormClaimManager $managerClaimMail $mail): RedirectResponse
  85.     {
  86.         /** @var Forms $forms */
  87.         $forms $formsManager->find(1);
  88.         $entity $manager->create();
  89.         $form $this->createForm(FormClaimType::class, $entity, [
  90.             'action' => $this->generateUrl('claim_action'),
  91.             'method' => 'POST',
  92.         ]);
  93.         $code $this->generateCode($manager->lastCode());
  94.         $entity->setCode($code);
  95.         $responseDays = (int)$forms->getClaimResponseDays();
  96.         try {
  97.             $answerDate = (new DateTime())->add(new DateInterval('P' $responseDays 'D'));
  98.         } catch (\Exception $exception) {
  99.             $answerDate = new DateTime();
  100.         }
  101.         $entity->setAnswerDate($answerDate);
  102.         $form->handleRequest($request);
  103.         if ($form->isValid() && $this->captchaValidate()) {
  104.             $manager->save($entity);
  105.             $mail->load($entity)->send();
  106.             $mail->loadClient($entity)->send();
  107.             $this->requestStack->getSession()->remove('sd_claim_code');
  108.             $this->requestStack->getSession()->remove('sd_form_claim_entity');
  109.             return $this->redirectToRoute('claim_received');
  110.         }
  111.         $this->saveFormSession('sd_form_claim_entity'$entity);
  112.         return $this->redirectToRoute('claim');
  113.     }
  114.     public function generateCode($lastCode): string
  115.     {
  116.         $id $this->lastCodeId($lastCode);
  117.         return 'R' date('Y') . date('m') . '-' str_pad((string)($id 1), 6'0'STR_PAD_LEFT);
  118.     }
  119.     public function lastCodeId($lastCode): int
  120.     {
  121.         $code $this->lastCode($lastCode);
  122.         $array explode('-'$code);
  123.         return count($array) == ? (int)$array[1] : 0;
  124.     }
  125.     public function lastCode($lastCode): string
  126.     {
  127.         $code $lastCode;
  128.         return is_null($code) ? 'R' date('Y') . date('m') . '-' '100000' $code;
  129.     }
  130. }