src/Service/ProcessFormService.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Form;
  4. use App\Entity\FormLead;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Exception;
  8. class ProcessFormService extends JsonFormManager
  9. {
  10.     // Frames
  11.     public const FRAME_MAIN 'main';
  12.     public const FRAME_EMBED 'embed';
  13.     public const FRAMES = [
  14.         self::FRAME_MAIN,
  15.         self::FRAME_EMBED
  16.     ];
  17.     public const STRING_FRAMES self::FRAME_MAIN '|' self::FRAME_EMBED;
  18.     private const DAY_FORMAT 'Y-m-d';
  19.     /**
  20.      * @param Form $form
  21.      * @return bool
  22.      */
  23.     public function checkActiveStatus(Form $form): bool
  24.     {
  25.         return $form->isActive() && $form->getCampaign()->isActive() && $form->getCampaign()->getCompany()->isActive();
  26.     }
  27.     /**
  28.      * @param array $field
  29.      * @param FormLead|null $formLead
  30.      * @return array
  31.      * @throws Exception
  32.      */
  33.     public function buildOptions(array $field, ?FormLead $formLead): array
  34.     {
  35.         if (!$formLead) {
  36.             return $field['options'];
  37.         }
  38.         if (isset($field['options']['attr']['allow_data']) && !$field['options']['attr']['allow_data']) {
  39.             return $field['options'];
  40.         }
  41.         $data null;
  42.         foreach ($formLead->getFormLeadData() as $item) {
  43.             if (self::buildFieldName($item->getField(), $item->getType()) !== $field['name']) {
  44.                 continue;
  45.             }
  46.             $type self::getTypeFromFieldName($field['name']);
  47.             if (!self::formTypeIsSupported($type)) {
  48.                 throw new Exception(sprintf('El tipo %s no está soportado'$type));
  49.             }
  50.             switch ($type) {
  51.                 case self::CHECKBOX_TYPE:
  52.                     $data unserialize($item->getLastValue());
  53.                     break;
  54.                 case self::DATETIME_TYPE:
  55.                 case self::DATE_TYPE:
  56.                     $data = new DateTime($item->getLastValue());
  57.                     break;
  58.                 default:
  59.                     $data $item->getLastValue();
  60.             }
  61.         }
  62.         if ($data) {
  63.             $field['options']['data'] = $data;
  64.         }
  65.         return $field['options'];
  66.     }
  67.     /**
  68.      * @param FormLead $formLead
  69.      * @return ArrayCollection
  70.      * @throws Exception
  71.      */
  72.     public function getNotLeadDataInForm(FormLead $formLead): ArrayCollection
  73.     {
  74.         $leadDataInForm $this->getLeadDataInForm($formLead);
  75.         $result = new ArrayCollection();
  76.         foreach ($formLead->getFormLeadData() as $item) {
  77.             if (!$leadDataInForm->contains($item)) {
  78.                 $result->add($item);
  79.             }
  80.         }
  81.         return $result;
  82.     }
  83.     /**
  84.      * @param FormLead $formLead
  85.      * @return ArrayCollection
  86.      * @throws Exception
  87.      */
  88.     public function getLeadDataInForm(FormLead $formLead): ArrayCollection
  89.     {
  90.         $fieldList $this->getFormDefinitionFields();
  91.         $result = new ArrayCollection();
  92.         foreach ($fieldList as $field) {
  93.             foreach ($formLead->getFormLeadData() as $item) {
  94.                 if (JsonFormManager::buildFieldName($item->getField(), $item->getType()) === $field['name']) {
  95.                     $result->add($item);
  96.                 }
  97.             }
  98.         }
  99.         return $result;
  100.     }
  101.     /**
  102.      * @param FormLead $formLead
  103.      * @return bool
  104.      */
  105.     public function isAllowedRenderForm(FormLead $formLead): bool
  106.     {
  107.         $formInstance $this->getForm();
  108.         if (!$formInstance->isTemporarilyAllow()) {
  109.             return true;
  110.         }
  111.         $date $this->calculatePeriodStart($formLead);
  112.         $dateStart = clone $date;
  113.         $endStep $formInstance->getNotAllowDuringStep() ?: $formInstance->getNotAllowStep();
  114.         $endScale $formInstance->getNotAllowDuringScale() ?: $formInstance->getNotAllowScale();
  115.         $dateEnd DateTimeService::modifyDatetime($date$endStep$endScaleDateTimeService::OPERATION_ADD);
  116.         if (
  117.             $dateStart->format(self::DAY_FORMAT) <= $dateEnd->format(self::DAY_FORMAT) &&
  118.             $this->getTodayFormat() <= $dateEnd->format(self::DAY_FORMAT)
  119.         ) {
  120.             if ($formInstance->isOneUpdateByPeriod()) {
  121.                 $updatedAt $formLead->getPeriodUpdatedAt()->format(self::DAY_FORMAT);
  122.                 return !(
  123.                     $updatedAt >= $dateStart->format(self::DAY_FORMAT) &&
  124.                     $updatedAt <= $dateEnd->format(self::DAY_FORMAT)
  125.                 );
  126.             }
  127.             return true;
  128.         }
  129.         return false;
  130.     }
  131.     /**
  132.      * @param FormLead $formLead
  133.      * @return DateTime|false
  134.      */
  135.     private function calculatePeriodStart(FormLead $formLead)
  136.     {
  137.         $formInstance $this->getForm();
  138.         $date $formLead->getPeriodStartsAt();
  139.         $step $formInstance->getNotAllowStep();
  140.         $scale $formInstance->getNotAllowScale();
  141.         while (true) {
  142.             $startsAt $formLead->getPeriodStartsAt();
  143.             if ($date->format(self::DAY_FORMAT) > $this->getTodayFormat()) {
  144.                 $date DateTimeService::modifyDatetime($startsAt$step$scaleDateTimeService::OPERATION_SUB);
  145.                 break;
  146.             }
  147.             $date DateTimeService::modifyDatetime($startsAt$step$scaleDateTimeService::OPERATION_ADD);
  148.         }
  149.         return $date;
  150.     }
  151.     /**
  152.      * @return string
  153.      */
  154.     private function getTodayFormat(): string
  155.     {
  156.         return (new DateTime())->format(self::DAY_FORMAT);
  157.     }
  158. }