src/Controller/Front/FormController.php line 170

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Info;
  4. use App\Entity\InfoLandSale;
  5. use App\Exception\InvalidFileExtensionException;
  6. use App\Exception\InvalidFileSizeException;
  7. use App\Manager\InfoManager;
  8. use App\Service\Referrals\Client as ReferralClient;
  9. use App\Traits\ReferredToCRM;
  10. use Twig\Environment;
  11. use App\Interfaces\Em;
  12. use App\Interfaces\Csfr;
  13. use App\Traits\CmdTrait;
  14. use App\Traits\CrmTrait;
  15. use App\Traits\UtmTrait;
  16. use App\Entity\FormQuote;
  17. use App\Traits\AllowTrait;
  18. use App\Entity\FormLandSale;
  19. use App\Entity\FormReferred;
  20. use App\Interfaces\ParamTwig;
  21. use App\Manager\RegionManager;
  22. use App\Traits\FormTokenTrait;
  23. use App\Traits\SyncEmailTrait;
  24. use App\Traits\FileUploadTrait;
  25. use App\Entity\FormSubscription;
  26. use App\Manager\InfoFormManager;
  27. use App\Manager\LogQuoteManager;
  28. use App\Manager\PropertyManager;
  29. use App\Manager\FormQuoteManager;
  30. use App\Manager\InfoContactManager;
  31. use App\Manager\ThanksQuoteManager;
  32. use App\Manager\FormLandSaleManager;
  33. use App\Manager\FormReferredManager;
  34. use App\Manager\InfoLandSaleManager;
  35. use App\Manager\InfoReferredManager;
  36. use App\Manager\ThanksLandSaleManager;
  37. use App\Manager\ThanksReferredManager;
  38. use App\Manager\FormSubscriptionManager;
  39. use Doctrine\ORM\EntityManagerInterface;
  40. use App\Manager\ThanksSubscriptionManager;
  41. use Symfony\Component\HttpFoundation\Request;
  42. use Symfony\Component\HttpFoundation\Response;
  43. use Symfony\Component\Routing\Annotation\Route;
  44. use Symfony\Component\HttpFoundation\JsonResponse;
  45. use Symfony\Component\Validator\Validator\ValidatorInterface;
  46. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  47. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  48. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  49. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  50. class FormController extends AbstractController implements ParamTwigCsfrEm
  51. {
  52.     use CmdTrait;
  53.     use UtmTrait;
  54.     use FormTokenTrait;
  55.     use SyncEmailTrait;
  56.     use CrmTrait;
  57.     use FileUploadTrait;
  58.     use AllowTrait;
  59.     use ReferredToCRM;
  60.     protected array $locals = [];
  61.     private string $captcha_token_name '__tokenRecaptcha';
  62.     private string $captcha_action_name '__actionRecaptcha';
  63.     protected $sd_em;
  64.     protected $sd_validator;
  65.     public function __construct(EntityManagerInterface $emValidatorInterface $validator)
  66.     {
  67.         $this->sd_em $em;
  68.         $this->sd_validator $validator;
  69.         $this->locals['menu_active'] = '';
  70.     }
  71.     public function getEm(): EntityManagerInterface
  72.     {
  73.         return $this->sd_em;
  74.     }
  75.     public function getParam(): ParameterBagInterface
  76.     {
  77.         return $this->container->get('parameter_bag');
  78.     }
  79.     public function getTwig(): Environment
  80.     {
  81.         return $this->container->get('twig');
  82.     }
  83.     public function getCsrfManager(): CsrfTokenManagerInterface
  84.     {
  85.         return $this->container->get('security.csrf.token_manager');
  86.     }
  87.     public function sendBackgroundEmail($data)
  88.     {
  89.         $keys = [
  90.             'class' => 'c',
  91.             'id' => 'i',
  92.             'template' => 't',
  93.             'subject' => 'j',
  94.             'recipients' => 'r',
  95.             'file' => 'f',
  96.             'qlog' => 'k',
  97.         ];
  98.         $this->executeCommand(
  99.             $this->formatCommand('app:send-common-email'$data$keys)
  100.         );
  101.         return true;
  102.     }
  103.     public function sendQuoteEmail($data)
  104.     {
  105.         $keys = [
  106.             'id' => 'i',
  107.             'property' => 't',
  108.         ];
  109.         $this->executeCommand(
  110.             $this->formatCommand('app:send-quote-email'$data$keys)
  111.         );
  112.         return true;
  113.     }
  114.     public function sendReferredEmail($data)
  115.     {
  116.         $keys = [
  117.             'id' => 'i',
  118.         ];
  119.         $this->executeCommand(
  120.             $this->formatCommand('app:send-referred-email'$data$keys)
  121.         );
  122.         return true;
  123.     }
  124.     public function sendLeadCrms($data)
  125.     {
  126.         $keys = [
  127.             'id' => 'i',
  128.             'property' => 't',
  129.             'log' => 'l',
  130.         ];
  131.         $this->executeCommand(
  132.             $this->formatCommand('app:send-lead-crms'$data$keys)
  133.         );
  134.         return true;
  135.     }
  136.     #[Route(path'/guardar-utms/'name'store_utms')]
  137.     public function storeUtmsAction(Request $request): JsonResponse
  138.     {
  139.         $cookie $this->setUtmCookie($request);
  140.         if (!$cookie) {
  141.             return new JsonResponse(['success' => false]);
  142.         }
  143.         $response = new JsonResponse(['success' => true]);
  144.         $response->headers->setCookie($cookie);
  145.         return $response;
  146.     }
  147.     #[Route(path'/contactanos/'name'contact'methods: ['GET'])]
  148.     #[Template('front/form/contact.html.twig')]
  149.     public function contactAction(Request $requestInfoContactManager $icmgrPropertyManager $pmgr): array
  150.     {
  151.         $this->locals['home'] = $icmgr->find(1);
  152.         $this->locals['list_data'] = $pmgr->listContactOptions();
  153.         return $this->locals;
  154.     }
  155.     #[Route(path'/guardar-lead/{slug}/'name'register_quote_lead'methods: ['POST'])]
  156.     public function registerLead($slugRequest $requestPropertyManager $pmgrFormQuoteManager $fqmgrInfoManager $imgr): JsonResponse
  157.     {
  158.         $response = [
  159.             'success' => false,
  160.             'id' => 0,
  161.             'errors' => '',
  162.         ];
  163.         if (!$this->isValidCaptcha($request)) {
  164.             $response['errors'] = 'No se pudo validar la autenticidad del formulario.
  165.                 Recargue la página e intente de nuevo';
  166.             $response['formData'] = $request->request->all();
  167.             $response['recaptchaResponse'] = false;
  168.             return new JsonResponse($response);
  169.         }
  170.         $property $pmgr->loadBySlug($slug);
  171.         if (!$property) {
  172.             throw $this->createNotFoundException('No encontrado');
  173.         }
  174.         $data $request->request->all();
  175.         $token  $data['__token'];
  176.         $lead_id 0;
  177.         unset($data[$this->captcha_token_name]);
  178.         unset($data[$this->captcha_action_name]);
  179.         unset($data['__token']);
  180.         unset($data['acepto']);
  181.         if (!$this->validateToken($this->quote_intention$token)) {
  182.             $response['errors'] = 'Token no válido';
  183.             return new JsonResponse($response);
  184.         }
  185.         $tmp $fqmgr->findPreRegisteredLead($data['dni'], $property->getTitle(), $lead_id);
  186.         if ($tmp) {
  187.             $entity $tmp;
  188.         } else {
  189.             $entity $fqmgr->create();
  190.         }
  191.         /** @var FormQuote $entity */
  192.         $entity $this->fillEntity($request$entity$data);
  193.         /** @var Info $info */
  194.         $info $imgr->find(1);
  195.         if ($this->blockLead($entity->getEmail(), $entity->getPhone(), $info->getEmailBlacklist(), $info->getPhoneBlacklist())) {
  196.             $response = [
  197.                 'success' => true,
  198.                 'id' => -1,
  199.                 'errors' => '',
  200.             ];
  201.             return new JsonResponse($response);
  202.         }
  203.         if ($this->isQuoteLeadRegistered($entity$property)) {
  204.             $response['errors'] = "{$entity->getName()} te has registrado con anterioridad en este proyecto.";
  205.             return new JsonResponse($response);
  206.         }
  207.         $entity->setPropertyType($property->getPropertyType()->getTitle());
  208.         $entity->setProgram($property->getProgram() ?: null);
  209.         $entity->setProperty($property->getTitle());
  210.         $entity->setLocation($property->getLocation()->getTitle());
  211.         $entity->setIsCompleted(false);
  212.         $entity->setLight('rojo');
  213.         $entity->setPropertyId($property->getId());
  214.         $errors $this->validateEntity($entity);
  215.         if (!empty($errors)) {
  216.             $response['errors'] = $errors;
  217.             return new JsonResponse($response);
  218.         }
  219.         $fqmgr->save($entity);
  220.         $response = [
  221.             'success' => true,
  222.             'id' => $entity->getId(),
  223.             'errors' => '',
  224.         ];
  225.         return new JsonResponse($response);
  226.     }
  227.     #[Route(path'/cotizar/'name'quote'methods: ['POST'])]
  228.     public function quote(
  229.         Request $request,
  230.         PropertyManager $pmgr,
  231.         FormQuoteManager $fqmgr,
  232.         LogQuoteManager $lqmgr,
  233.         InfoFormManager $ifmgr,
  234.         InfoManager $imgr
  235.     ): JsonResponse {
  236.         set_time_limit(1800);
  237.         $slug $request->request->get('slug');
  238.         $response = [
  239.             'success' => false,
  240.             'url' => $this->generateUrl('property', ['slug' => $slug]),
  241.             'errors' => '',
  242.         ];
  243.         $property $pmgr->loadBySlug($slug);
  244.         if (!$property) {
  245.             throw $this->createNotFoundException('No encontrado');
  246.         }
  247.         $data $request->request->all();
  248.         $token  $data['__token'];
  249.         $lead_id $data['lead_id'];
  250.         $is_pin = !empty($data['code_pin']);
  251.         unset($data[$this->captcha_token_name]);
  252.         unset($data[$this->captcha_action_name]);
  253.         unset($data['__token']);
  254.         unset($data['slug']);
  255.         unset($data['code_pin']);
  256.         unset($data['acepto']);
  257.         unset($data['lead_id']);
  258.         if (!$this->validateToken($this->quote_intention$token)) {
  259.             $response['errors'] = 'Token no válido';
  260.             return new JsonResponse($response);
  261.         }
  262.         $tmp $fqmgr->findPreRegisteredLead($data['dni'], $property->getTitle(), $lead_id);
  263.         if ($tmp) {
  264.             $entity $tmp;
  265.         } else {
  266.             $entity $fqmgr->create();
  267.         }
  268.         /** @var FormQuote $entity */
  269.         $entity $this->fillEntity($request$entity$data);
  270.         /** @var Info $info */
  271.         $info $imgr->find(1);
  272.         if ($this->blockLead($entity->getEmail(), $entity->getPhone(), $info->getEmailBlacklist(), $info->getPhoneBlacklist())) {
  273.             $response = [
  274.                 'success' => true,
  275.                 'errors' => '',
  276.                 'url' => $this->generateUrl('thanks_quote', [
  277.                     'slug' => $property->getSlug(),
  278.                     'name' => $entity->getName(),
  279.                     'dni' => $entity->getDni(),
  280.                     'id' => $entity->getId(),
  281.                     'referred' => $this->generateUrl('property', ['slug' => $property->getSlug()]),
  282.                 ])
  283.             ];
  284.             return new JsonResponse($response);
  285.         }
  286.         if ($this->isQuoteLeadRegistered($entity$property)) {
  287.             $response['errors'] = "{$entity->getName()} te has registrado con anterioridad en este proyecto.";
  288.             return new JsonResponse($response);
  289.         }
  290.         $entity->setPropertyType($property->getPropertyType()->getTitle());
  291.         $entity->setProgram($property->getProgram() ?: null);
  292.         $entity->setProperty($property->getTitle());
  293.         $entity->setLocation($property->getLocation()->getTitle());
  294.         $entity->setPropertyId($property->getId());
  295.         $entity->setIsCompleted(true);
  296.         $color $is_pin 'verde' 'amarillo';
  297.         $entity->setLight($color);
  298.         $errors $this->validateEntity($entity);
  299.         if (!empty($errors)) {
  300.             $response['errors'] = $errors;
  301.             return new JsonResponse($response);
  302.         }
  303.         $entity->setCreated(new \DateTime());
  304.         $fqmgr->save($entity);
  305.         $log $lqmgr->create();
  306.         $log->setFormId($entity->getId());
  307.         $log->setFormFullName($entity->getName() . ' ' $entity->getLastname());
  308.         $log->setFormDni($entity->getDni());
  309.         $log->setFormCreated($entity->getCreated());
  310.         $log->setProject($entity->getProperty());
  311.         $log->setEmailDateSend(new \DateTime());
  312.         $lqmgr->save($log);
  313.         $home $ifmgr->find(1);
  314.         $rpta $this->sendSDSyncEmail([
  315.             'data' => ['detalle' => $entity],
  316.             'recipients' =>  $property->getFormEmail() ?: $home->getQuoteEmail(),
  317.             'subject' => 'Los Portales Vivienda - Cotización',
  318.             'template' => 'front/mail/quote.html.twig',
  319.             'qlog' => $log,
  320.             'file' => false,
  321.         ]);
  322.         if ($property->getLetSendCrm()) {
  323.             $this->executeSendLeadToCrms($log->getId(), $property->getId());
  324.         }
  325.         if ($property->getMailingShow()) {
  326.             $this->sendQuoteEmail([
  327.                 'id' => $entity->getId(),
  328.                 'property' => $property->getId()
  329.             ]);
  330.         }
  331.         $session $request->getSession();
  332.         $session->remove('pin_code');
  333.         return new JsonResponse([
  334.             'success' => true,
  335.             'errors' => '',
  336.             'url' => $this->generateUrl('thanks_quote', [
  337.                 'slug' => $property->getSlug(),
  338.                 'name' => $entity->getName(),
  339.                 'dni' => $entity->getDni(),
  340.                 'id' => $entity->getId(),
  341.                 'referred' => $this->generateUrl('property', ['slug' => $property->getSlug()]),
  342.             ])
  343.         ]);
  344.     }
  345.     #[Route(path'/enviar-lead-crm/'name'send_lead_crm'methods: ['GET'])]
  346.     public function sendLeadCrmAction(Request $request): JsonResponse
  347.     {
  348.         $rpta $this->executeSendLeadToCrms(
  349.             $request->query->getInt('logid'0),
  350.             $request->query->getInt('propertyid'0)
  351.         );
  352.         return new JsonResponse($rpta);
  353.     }
  354.     #[Route(path'/subscribirse/'name'subscribe'methods: ['POST'])]
  355.     public function subscribe(Request $requestFormSubscriptionManager $fsmgrInfoFormManager $ifmgr): JsonResponse
  356.     {
  357.         $response = [
  358.             'success' => false,
  359.             'url' => $this->generateUrl('home'),
  360.             'errors' => '',
  361.         ];
  362.         $data $request->request->all();
  363.         $token  $data['__token'];
  364.         unset($data['__token']);
  365.         unset($data['acepto']);
  366.         if (!$this->validateToken($this->subscription_intention$token)) {
  367.             $response['errors'] = 'Token no válido';
  368.             return new JsonResponse($response);
  369.         }
  370.         $entity $fsmgr->create();
  371.         $entity $this->fillEntity($request$entity$data);
  372.         $errors $this->validateEntity($entity);
  373.         if (!empty($errors)) {
  374.             $response['errors'] = $errors;
  375.             return new JsonResponse($response);
  376.         }
  377.         $fsmgr->save($entity);
  378.         $home $ifmgr->find(1);
  379.         $this->sendBackgroundEmail([
  380.             'class' => FormSubscription::class,
  381.             'id' => $entity->getId(),
  382.             'recipients' => $home->getSubscribeEmail(),
  383.             'subject' => 'Los Portales Vivienda - Subscripción',
  384.             'template' => 'front/mail/subscription.html.twig',
  385.         ]);
  386.         return new JsonResponse([
  387.             'success' => true,
  388.             'errors' => '',
  389.             'url' => $this->generateUrl('thanks_subscription', [
  390.                 'referred' => $this->generateUrl('home')
  391.             ])
  392.         ]);
  393.     }
  394.     #[Route(path'/venta-terreno/'name'land_sale'methods: ['GET''POST'])]
  395.     public function landSaleAction(
  396.         Request $request,
  397.         InfoLandSaleManager $ilsmgr,
  398.         InfoFormManager $ifmgr,
  399.         RegionManager $rmgr,
  400.         FormLandSaleManager $flsmgr
  401.     ): Response {
  402.         if (!$this->allowMenu('land_sale')) {
  403.             return $this->redirect($this->generateUrl('home'));
  404.         }
  405.         $intention 'land_sale';
  406.         /** @var InfoLandSale $home */
  407.         $home $ilsmgr->find(1);
  408.         $this->locals['home'] = $home;
  409.         $this->locals['info_form'] = $ifmgr->find(1);
  410.         $this->locals['regions'] = $rmgr->findBy(['active' => true]);
  411.         $this->locals['_token'] = $this->generateToken($intention);
  412.         if ($request->isMethod('post')) {
  413.             $data $request->request->all();
  414.             $image $request->files->get('land_image');
  415.             $plane $request->files->get('land_plane');
  416.             $directory 'terrenos';
  417.             $validImage true;
  418.             $validPlane true;
  419.             if ($image) {
  420.                 try {
  421.                     $this->isValidImage($image$home->getImageSize() * 1024 1024$home->getImageExts() ?? ['png''jpg''jpeg''pdf']);
  422.                 } catch (InvalidFileExtensionException|InvalidFileSizeException $e) {
  423.                     $validImage false;
  424.                     $errorMessage $e->getMessage();
  425.                     if ($e instanceof InvalidFileExtensionException and $home->getImageErrorExts()) {
  426.                         $errorMessage $home->getImageErrorExts();
  427.                     } elseif ($e instanceof InvalidFileSizeException and $home->getImageErrorSize()) {
  428.                         $errorMessage $home->getImageErrorSize();
  429.                     }
  430.                     $this->addFlash('error_message_image'$errorMessage);
  431.                 }
  432.             }
  433.             if ($plane) {
  434.                 try {
  435.                     $this->isValidImage($plane$home->getPlaneSize() * 1024 1024$home->getPlaneExts() ?? ['png''jpg''jpeg''pdf']);
  436.                 } catch (InvalidFileExtensionException|InvalidFileSizeException $e) {
  437.                     $validPlane false;
  438.                     $errorMessage $e->getMessage();
  439.                     if ($e instanceof InvalidFileExtensionException and $home->getPlaneErrorExts()) {
  440.                         $errorMessage $home->getPlaneErrorExts();
  441.                     } elseif ($e instanceof InvalidFileSizeException and $home->getPlaneErrorSize()) {
  442.                         $errorMessage $home->getPlaneErrorSize();
  443.                     }
  444.                     $this->addFlash('error_message_plane'$errorMessage);
  445.                 }
  446.             }
  447.             if (!$validImage or !$validPlane) {
  448.                 return $this->redirectToRoute('land_sale');
  449.             }
  450.             if ($image and $validImage) {
  451.                 $landImage $this->uploadLandSaleImages($image$directory);
  452.                 $data['land_image'] = $landImage;
  453.             }
  454.             if ($plane and $validPlane) {
  455.                 $planeImage $this->uploadLandSaleImages($plane$directory);
  456.                 $data['land_plane'] = $planeImage;
  457.             }
  458.             $token  $data['__token'];
  459.             unset($data['__token']);
  460.             unset($data['acepto']);
  461.             if (!$this->validateToken($intention$token)) {
  462.                 return $this->redirectToRoute('land_sale');
  463.             }
  464.             $entity $flsmgr->create();
  465.             $entity $this->fillEntity($request$entity$data);
  466.             $errors $this->validateEntity($entity);
  467.             if (!empty($errors)) {
  468.                 return $this->redirectToRoute('land_sale');
  469.             }
  470.             $flsmgr->save($entity);
  471.             $this->sendBackgroundEmail([
  472.                 'class' => FormLandSale::class,
  473.                 'id' => $entity->getId(),
  474.                 'recipients' => $this->locals['info_form']->getLandSaleEmail(),
  475.                 'subject' => 'Los Portales Vivienda - Venta de Terreno',
  476.                 'template' => 'front/mail/land_sale.html.twig',
  477.             ]);
  478.             $this->sendBackgroundEmail([
  479.                 'class' => FormLandSale::class,
  480.                 'id' => $entity->getId(),
  481.                 'recipients' => $entity->getEmail(),
  482.                 'subject' => 'Los Portales Vivienda - Venta de Terreno',
  483.                 'template' => 'front/mail/land_sale_client.html.twig',
  484.             ]);
  485.             return $this->redirectToRoute('thanks_land_sale', [
  486.                 'name' => $entity->getName(),
  487.                 'dni'  => $entity->getDni(),
  488.                 'referred' => $this->generateUrl('land_sale')
  489.             ]);
  490.         }
  491.         return $this->render("front/form/land_sale.html.twig"$this->locals);
  492.     }
  493.     #[Route(path'/imprimir-pdf/'name'print_pdf'methods: ['POST'])]
  494.     public function printPdfAction(Request $requestFormReferredManager $frmgrInfoReferredManager $irmgr): Response
  495.     {
  496.         $intention 'referred';
  497.         $data $request->request->all();
  498.         $token  $data['__token'];
  499.         unset($data['__token']);
  500.         unset($data['acepto']);
  501.         $response = [
  502.             'success' => false,
  503.             'errors' => '',
  504.         ];
  505.         if (!$this->validateToken($intention$token)) {
  506.             $response['errors'] = 'Token no válido';
  507.             return new JsonResponse($response);
  508.         }
  509.         $entity $frmgr->create();
  510.         $entity $this->fillEntity($request$entity$data);
  511.         $errors $this->validateEntity($entity);
  512.         if (!empty($errors)) {
  513.             $response['errors'] = $errors;
  514.             return new JsonResponse($response);
  515.         }
  516.         $this->locals['home'] = $irmgr->find(1);
  517.         $this->locals['entity'] = $entity;
  518.         return $this->render("front/mail/_body_referred.html.twig"$this->locals);
  519.     }
  520.     #[Route(path'/programa-de-referidos/'name'referred'methods: ['GET''POST'])]
  521.     public function referredAction(
  522.         Request $request,
  523.         InfoReferredManager $irmgr,
  524.         InfoFormManager $ifmgr,
  525.         FormReferredManager $frmgr
  526.     ): Response {
  527.         if (!$this->allowMenu('referred')) {
  528.             return $this->redirect($this->generateUrl('home'));
  529.         }
  530.         $intention 'referred';
  531.         $this->locals['home'] = $irmgr->find(1);
  532.         $this->locals['info_form'] = $ifmgr->find(1);
  533.         $this->locals['_token'] = $this->generateToken($intention);
  534.         if ($request->isMethod('post')) {
  535.             $response = [
  536.                 'success' => false,
  537.                 'url' => $this->generateUrl('referred'),
  538.                 'errors' => '',
  539.             ];
  540.             $data $request->request->all();
  541.             $token  $data['__token'];
  542.             unset($data['__token']);
  543.             unset($data['acepto']);
  544.             if (!$this->validateToken($intention$token)) {
  545.                 $response['errors'] = 'Token no válido';
  546.                 return new JsonResponse($response);
  547.             }
  548.             $entity $frmgr->create();
  549.             /** @var FormReferred $entity */
  550.             $entity $this->fillEntity($request$entity$data);
  551.             $session $request->getSession();
  552.             /** @var ReferralClient $client */
  553.             $client $session->get('sd_referral_client', new ReferralClient());
  554.             if (!$client->isValidClient()) {
  555.                 $response['errors'] = 'No se obtuvieron datos del usuario';
  556.                 return new JsonResponse($response);
  557.             }
  558.             $entity->setReferrerDni($client->getDocumentNumber());
  559.             $errors $this->validateEntity($entity);
  560.             if (!empty($errors)) {
  561.                 $response['errors'] = $errors;
  562.                 return new JsonResponse($response);
  563.             }
  564.             $frmgr->save($entity);
  565.             $session->remove('sd_referral_client');
  566.             if ($this->locals['home']->isSendToCrm()) {
  567.                 $this->sendReferredToCRM($entity);
  568.             }
  569.             $this->sendBackgroundEmail([
  570.                 'class' => FormReferred::class,
  571.                 'id' => $entity->getId(),
  572.                 'recipients' => $this->locals['info_form']->getReferredEmail(),
  573.                 'subject' => 'Los Portales Vivienda - Programa Referidos',
  574.                 'template' => 'front/mail/referred.html.twig',
  575.             ]);
  576.             $this->sendReferredEmail([
  577.                 'id' => $entity->getId(),
  578.             ]);
  579.             return new JsonResponse([
  580.                 'success' => true,
  581.                 'errors' => '',
  582.                 'url' => $this->generateUrl('thanks_referred', [
  583.                     'name' => $entity->getReferrerFullName(),
  584.                     'dni'  => $entity->getReferrerDni(),
  585.                     'referred' => $this->generateUrl('referred')
  586.                 ])
  587.             ]);
  588.         }
  589.         return $this->render("front/form/referred.html.twig"$this->locals);
  590.     }
  591.     #[Route(path'/gracias-por-contactarnos/'name'thanks'methods: ['GET'])]
  592.     #[Route(path'/gracias-por-contactarnos/{slug}/'name'thanks_quote'methods: ['GET'])]
  593.     #[Template('front/form/thanks_quote.html.twig')]
  594.     public function thanksQuoteAction(
  595.         Request $request,
  596.         PropertyManager $pmgr,
  597.         ThanksQuoteManager $tqmgr,
  598.         $slug ''
  599.     ): array {
  600.         $this->locals['user_name'] = $request->query->get('name''');
  601.         $this->locals['referred'] = $request->query->get('referred''');
  602.         $this->locals['lead_id'] = $request->query->get('id''');
  603.         $this->locals['property'] = $pmgr->loadBySlug($slug);
  604.         $this->locals['home'] = $tqmgr->find(1);
  605.         return $this->locals;
  606.     }
  607.     #[Route(path'/gracias-por-subscribirte/'name'thanks_subscription'methods: ['GET'])]
  608.     #[Template('front/form/thanks_subscription.html.twig')]
  609.     public function thanksSubscriptionAction(Request $requestThanksSubscriptionManager $tsmgr): array
  610.     {
  611.         $this->locals['user_name'] = $request->query->get('name''');
  612.         $this->locals['referred'] = $request->query->get('referred''');
  613.         $this->locals['home'] = $tsmgr->find(1);
  614.         return $this->locals;
  615.     }
  616.     #[Route(path'/gracias-por-referir/'name'thanks_referred'methods: ['GET'])]
  617.     #[Template('front/form/thanks_referred.html.twig')]
  618.     public function thanksReferredAction(Request $requestThanksReferredManager $trmgr): array
  619.     {
  620.         $this->locals['user_name'] = $request->query->get('name''');
  621.         $this->locals['referred'] = $request->query->get('referred''');
  622.         $this->locals['home'] = $trmgr->find(1);
  623.         return $this->locals;
  624.     }
  625.     #[Route(path'/gracias-por-escribirnos/'name'thanks_land_sale'methods: ['GET'])]
  626.     #[Template('front/form/thanks_land_sale.html.twig')]
  627.     public function thanksLandSaleAction(Request $requestThanksLandSaleManager $tlsmgr): array
  628.     {
  629.         $this->locals['user_name'] = $request->query->get('name''');
  630.         $this->locals['referred'] = $request->query->get('referred''');
  631.         $this->locals['home'] = $tlsmgr->find(1);
  632.         return $this->locals;
  633.     }
  634.     #[Route(path'/lead-from-code/'name'lead_from_code'methods: ['POST'])]
  635.     public function leadFromCodeAction(Request $requestFormQuoteManager $fqmgr): JsonResponse
  636.     {
  637.         $code $request->request->getInt('idcontacto'0);
  638.         $start = (int) $this->getParameter('inmobiliario_number');
  639.         $code $code $start;
  640.         if ($code 1) {
  641.             return new JsonResponse([]);
  642.         }
  643.         $lead $fqmgr->find($code);
  644.         if (!$lead) {
  645.             return new JsonResponse([]);
  646.         }
  647.         $data = [];
  648.         $keys = [
  649.             'name',
  650.             'lastname',
  651.             'email',
  652.             'dni',
  653.             'phone',
  654.             'schedule',
  655.             'property_type',
  656.             'program',
  657.             'location',
  658.             'property',
  659.             'utm_source',
  660.             'utm_medium',
  661.             'utm_campaign',
  662.             'utm_content',
  663.             'utm_term',
  664.             'gclid'
  665.         ];
  666.         foreach ($keys as $key) {
  667.             $method 'get' $this->convertAttributeToMethod($key);
  668.             $data[$key] = $lead->$method();
  669.         }
  670.         return new JsonResponse($data);
  671.     }
  672.     private function validateEntity($entity): string
  673.     {
  674.         $errors $this->sd_validator->validate($entity);
  675.         if ((is_countable($errors) ? count($errors) : 0) < 1) {
  676.             return '';
  677.         }
  678.         return (string) $errors;
  679.     }
  680.     private function fillEntity(Request $request$entity$data)
  681.     {
  682.         foreach ($data as $attr => $value) {
  683.             $method 'set' $this->convertAttributeToMethod($attr);
  684.             $entity->$method($value);
  685.         }
  686.         $entity $this->fillUtmDataEntity($request$entity);
  687.         return $entity;
  688.     }
  689.     private function fillUtmDataEntity(Request $request$entity)
  690.     {
  691.         $cookie_data $this->getUtmCookie($request);
  692.         foreach ($cookie_data as $key => $value) {
  693.             $method 'set' $this->convertAttributeToMethod($key);
  694.             $entity->$method($value);
  695.         }
  696.         return $entity;
  697.     }
  698.     private function convertAttributeToMethod($attribute): string
  699.     {
  700.         $tmp explode('_'$attribute);
  701.         $tmp array_map('ucwords'$tmp);
  702.         return implode(''$tmp);
  703.     }
  704.     private function isQuoteLeadRegistered($entity$property): bool
  705.     {
  706.         $lead $this
  707.             ->getEm()
  708.             ->getRepository(FormQuote::class)
  709.             ->validRegistrationDate($entity->getDni(), $property)
  710.         ;
  711.         return !is_null($lead);
  712.     }
  713.     private function blockLead(?string $email, ?string $phone, ?string $emailBlacklist, ?string $phoneBlacklist): bool
  714.     {
  715.         if (is_null($emailBlacklist) and is_null($phoneBlacklist)) {
  716.             return false;
  717.         }
  718.         $emailBlacklistList preg_split("/\r\n|\n|\r/"$emailBlacklist);
  719.         $phoneBlacklistList preg_split("/\r\n|\n|\r/"$phoneBlacklist);
  720.         if (is_null($email) and is_null($phone)) {
  721.             return true;
  722.         }
  723.         if (is_array($emailBlacklistList) and !empty($emailBlacklistList)) {
  724.             if (in_array($email$emailBlacklistList)) {
  725.                 return true;
  726.             }
  727.         }
  728.         if (is_array($phoneBlacklistList) and !empty($phoneBlacklistList)) {
  729.             if (in_array($phone$phoneBlacklistList)) {
  730.                 return true;
  731.             }
  732.         }
  733.         return false;
  734.     }
  735.     private function isValidCaptcha(Request $request): bool
  736.     {
  737.         $private_key '6LfPOd8hAAAAABdYI7e-SczpNScS4cOlzes1g74F';
  738.         $remote_ip $request->getClientIp();
  739.         $token_recaptcha $request->request->get($this->captcha_token_name'');
  740.         $action_recaptcha $request->request->get($this->captcha_action_name'');
  741.         $url 'https://www.google.com/recaptcha/api/siteverify';
  742.         $data = [
  743.             'secret' => $private_key,
  744.             'response' => $token_recaptcha,
  745.             'remoteip' => $remote_ip,
  746.             'action' => $action_recaptcha,
  747.         ];
  748.         $options = [
  749.             'http' => [
  750.                 'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
  751.                 'method'  => 'POST',
  752.                 'content' => http_build_query($data)
  753.             ]
  754.         ];
  755.         $context  stream_context_create($options);
  756.         $gresponse file_get_contents($urlfalse$context);
  757.         $response json_decode($gresponsetrue512JSON_THROW_ON_ERROR);
  758.         if (!(is_array($response) && array_key_exists('success'$response))) {
  759.             return false;
  760.         }
  761.         return (bool) $response['success'];
  762.     }
  763. }