src/Controller/Front/FormController.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Controller\Core\BaseFrontController;
  4. use App\Entity\FormClaim;
  5. use App\Entity\FormContact;
  6. use App\Entity\FormQuotation;
  7. use App\Entity\Forms;
  8. use App\Form\Front\FormClaimType;
  9. use App\Form\Front\FormContactType;
  10. use App\Form\Front\FormQuotationType;
  11. use App\Manager\ContactManager;
  12. use App\Manager\FormClaimManager;
  13. use App\Manager\FormContactManager;
  14. use App\Manager\FormQuotationManager;
  15. use App\Manager\FormsManager;
  16. use App\Manager\QuotationManager;
  17. use App\Service\CRM\ApiCRM;
  18. use App\Service\Mail\ClaimMail;
  19. use App\Service\Mail\ContactMail;
  20. use App\Service\Mail\QuotationMail;
  21. use App\Service\Recaptcha\Validator as RecaptchaValidator;
  22. use App\Traits\FormFileTrait;
  23. use DateInterval;
  24. use DateTime;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26. use Symfony\Component\HttpFoundation\RedirectResponse;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. class FormController extends BaseFrontController
  30. {
  31.     use FormFileTrait;
  32.     const FORM_CONTACT 'sd_form_contact_entity';
  33.     const FORM_QUOTATION 'sd_form_quotation_entity';
  34.     const FORM_CLAIM 'sd_form_claim_entity';
  35.     /**
  36.      * @Route("/contacto", name="contact", methods={"GET"})
  37.      * @Template("front/form/contact.html.twig")
  38.      */
  39.     public function contact(ContactManager $contactManager): array
  40.     {
  41.         $entity $this->getFormSession(self::FORM_CONTACT, new FormContact());
  42.         $form $this->getFormView(FormContactType::class, $entity'contact_action');
  43.         $this->locals['sd'] = $contactManager->find(1);
  44.         $this->locals['form'] = $form;
  45.         return $this->locals;
  46.     }
  47.     /**
  48.      * @Route("/contacto", name="contact_action", methods={"POST"})
  49.      */
  50.     public function contactAction(Request $requestFormContactManager $managerContactMail $mailApiCRM $apiCRMRecaptchaValidator $recaptchaValidator): RedirectResponse
  51.     {
  52.         $entity = new FormContact();
  53.         $form $this->getForm(FormContactType::class, $entity'contact_action');
  54.         $form->handleRequest($request);
  55.         if ($form->isValid() and $recaptchaValidator->validate($this->getRecaptchaToken(), $this->getClientIp())) {
  56.             $manager->save($entity);
  57.             $mail->load($entity)->send();
  58.             $crmLead $entity->leadToCRM();
  59.             $result $apiCRM->send($crmLead);
  60.             $entity->setCrmBody($crmLead->toCRM());
  61.             $entity->setCrmResult($result);
  62.             $manager->save($entity);
  63.             return $this->redirectToRoute('thanks_slug', ['slug' => 'contacto']);
  64.         }
  65.         $this->saveFormSession(self::FORM_CONTACT$entity);
  66.         return $this->redirectToRoute('contact');
  67.     }
  68.     /**
  69.      * @Route("/cotizar", name="quotation", methods={"GET"})
  70.      * @Template("front/form/quotation.html.twig")
  71.      */
  72.     public function quotation(QuotationManager $quotationManager): array
  73.     {
  74.         $entity $this->getFormSession(self::FORM_QUOTATION, new FormQuotation());
  75.         $form $this->getFormView(FormQuotationType::class, $entity'quotation_action');
  76.         $this->locals['sd'] = $quotationManager->find(1);
  77.         $this->locals['form'] = $form;
  78.         return $this->locals;
  79.     }
  80.     /**
  81.      * @Route("/cotizar", name="quotation_action", methods={"POST"})
  82.      */
  83.     public function quotationAction(Request $requestFormQuotationManager $managerQuotationMail $mailApiCRM $apiCRMRecaptchaValidator $recaptchaValidator): RedirectResponse
  84.     {
  85.         $entity = new FormQuotation();
  86.         $form $this->getForm(FormQuotationType::class, $entity'quotation_action');
  87.         $form->handleRequest($request);
  88.         if ($form->isValid() and $recaptchaValidator->validate($this->getRecaptchaToken(), $this->getClientIp())) {
  89.             $manager->save($entity);
  90.             $mail->load($entity)->send();
  91.             $crmLead $entity->leadToCRM();
  92.             $result $apiCRM->send($crmLead);
  93.             $entity->setCrmBody($crmLead->toCRM());
  94.             $entity->setCrmResult($result);
  95.             $manager->save($entity);
  96.             return $this->redirectToRoute('thanks_slug', ['slug' => 'cotizar']);
  97.         }
  98.         $this->saveFormSession(self::FORM_QUOTATION$entity);
  99.         return $this->redirectToRoute('quotation');
  100.     }
  101.     /**
  102.      * @Route("/libro-de-reclamaciones", name="claim", methods={"GET"})
  103.      * @Template("front/form/claim.html.twig")
  104.      */
  105.     public function claim(FormClaimManager $formClaimManagerFormsManager $formsManager): array
  106.     {
  107.         /** @var Forms $forms */
  108.         $forms $formsManager->find(1);
  109.         $responseDays = (int)$forms->getClaimResponseDays();
  110.         try {
  111.             $this->locals['answer_date'] = (new DateTime())->add(new DateInterval('P' $responseDays 'D'));
  112.         } catch (\Exception $exception) {
  113.             $this->locals['answer_date'] = new DateTime();
  114.         }
  115.         $entity = new FormClaim();
  116.         $code $this->generateCode($formClaimManager->lastCode());
  117.         $entity->setCode($code);
  118.         $entity $this->getFormSession(self::FORM_CLAIM$entity);
  119.         $form $this->getFormView(FormClaimType::class, $entity'claim_action');
  120.         $this->locals['form'] = $form;
  121.         $this->locals['entity'] = $entity;
  122.         return $this->locals;
  123.     }
  124.     /**
  125.      * @Route("/libro-de-reclamaciones", name="claim_action", methods={"POST"})
  126.      */
  127.     public function claimAction(Request $requestFormClaimManager $managerClaimMail $mailRecaptchaValidator $recaptchaValidator): RedirectResponse
  128.     {
  129.         $entity = new FormClaim();
  130.         $form $this->getForm(FormClaimType::class, $entity'claim_action');
  131.         $entity->setFile(null);
  132.         if ($request->files->get('form_file')) {
  133.             $file $this->saveFile($request->files->get('form_file'), 10000000,'reclamos');
  134.             $entity->setFile($file);
  135.         }
  136.         $code $this->generateCode($manager->lastCode());
  137.         $entity->setCode($code);
  138.         $form->handleRequest($request);
  139.         if ($form->isValid() and $recaptchaValidator->validate($this->getRecaptchaToken(), $this->getClientIp())) {
  140.             $entity->setSaved(true);
  141.             $manager->save($entity);
  142.             $mail->load($entity)->send();
  143.             $mail->loadClient($entity)->send();
  144.             $this->saveFormSession(self::FORM_CLAIM$entity);
  145.             $this->requestStack->getSession()->remove('sd_claim_code');
  146.             return $this->redirectToRoute('thanks');
  147.         }
  148.         $this->saveFormSession(self::FORM_CLAIM$entity);
  149.         return $this->redirectToRoute('claim');
  150.     }
  151.     public function generateCode($lastCode): string
  152.     {
  153.         $id $this->lastCodeId($lastCode);
  154.         return 'R' date('Y') . date('m') . '-' str_pad((string)($id 1), 6'0'STR_PAD_LEFT);
  155.     }
  156.     public function lastCodeId($lastCode): int
  157.     {
  158.         $code $this->lastCode($lastCode);
  159.         $array explode('-'$code);
  160.         return count($array) == ? (int)$array[1] : 0;
  161.     }
  162.     public function lastCode($lastCode): string
  163.     {
  164.         $code $lastCode;
  165.         return is_null($code) ? 'R' date('Y') . date('m') . '-' '100000' $code;
  166.     }
  167. }