vendor/sulu/form-bundle/Entity/Form.php line 20

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\FormBundle\Entity;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. /**
  14.  * Form entity.
  15.  */
  16. class Form
  17. {
  18.     public const RESOURCE_KEY 'forms';
  19.     /**
  20.      * @var null|int
  21.      */
  22.     private $id;
  23.     /**
  24.      * @var string
  25.      */
  26.     private $defaultLocale;
  27.     /**
  28.      * @var Collection<int, FormTranslation>
  29.      */
  30.     private $translations;
  31.     /**
  32.      * @var Collection<int, FormField>
  33.      */
  34.     private $fields;
  35.     public function __construct()
  36.     {
  37.         $this->translations = new ArrayCollection();
  38.         $this->fields = new ArrayCollection();
  39.     }
  40.     public function getId(): ?int
  41.     {
  42.         return $this->id;
  43.     }
  44.     public function getDefaultLocale(): string
  45.     {
  46.         return $this->defaultLocale;
  47.     }
  48.     public function setDefaultLocale(string $defaultLocale): self
  49.     {
  50.         $this->defaultLocale $defaultLocale;
  51.         return $this;
  52.     }
  53.     public function addTranslation(FormTranslation $translation): self
  54.     {
  55.         $this->translations[] = $translation;
  56.         return $this;
  57.     }
  58.     public function removeTranslation(FormTranslation $translation): self
  59.     {
  60.         $this->translations->removeElement($translation);
  61.         return $this;
  62.     }
  63.     /**
  64.      * Get translations.
  65.      *
  66.      * @return Collection<int, FormTranslation>
  67.      */
  68.     public function getTranslations()
  69.     {
  70.         return $this->translations;
  71.     }
  72.     public function getTranslation(string $localebool $create falsebool $fallback false): ?FormTranslation
  73.     {
  74.         foreach ($this->translations as $translation) {
  75.             if ($translation->getLocale() == $locale) {
  76.                 return $translation;
  77.             }
  78.         }
  79.         if ($create) {
  80.             $translation = new FormTranslation();
  81.             $translation->setLocale($locale);
  82.             $this->addTranslation($translation);
  83.             $translation->setForm($this);
  84.             return $translation;
  85.         }
  86.         if ($fallback) {
  87.             return $this->getTranslation($this->getDefaultLocale());
  88.         }
  89.         return null;
  90.     }
  91.     public function addField(FormField $field): self
  92.     {
  93.         $this->fields[] = $field;
  94.         return $this;
  95.     }
  96.     public function removeField(FormField $field): self
  97.     {
  98.         $this->fields->removeElement($field);
  99.         return $this;
  100.     }
  101.     /**
  102.      * @return Collection<int, FormField>
  103.      */
  104.     public function getFields()
  105.     {
  106.         return $this->fields;
  107.     }
  108.     /**
  109.      * @return FormField[]
  110.      */
  111.     public function getFieldsByType(string $type): array
  112.     {
  113.         $fields = [];
  114.         foreach ($this->fields as $field) {
  115.             if ($field->getType() === $type) {
  116.                 $fields[] = $field;
  117.             }
  118.         }
  119.         return $fields;
  120.     }
  121.     public function getField(?string $key): ?FormField
  122.     {
  123.         foreach ($this->fields as $field) {
  124.             if ($field->getKey() == $key) {
  125.                 return $field;
  126.             }
  127.         }
  128.         return null;
  129.     }
  130.     public function getFieldType(string $key): ?string
  131.     {
  132.         $field $this->getField($key);
  133.         if (!$field) {
  134.             return null;
  135.         }
  136.         return $field->getType();
  137.     }
  138.     /**
  139.      * Get fields not in array.
  140.      *
  141.      * @param string[] $keys
  142.      *
  143.      * @return FormField[]
  144.      */
  145.     public function getFieldsNotInArray(array $keys): array
  146.     {
  147.         $fields = [];
  148.         foreach ($this->fields as $field) {
  149.             if (!\in_array($field->getKey(), $keys)) {
  150.                 $fields[] = $field;
  151.             }
  152.         }
  153.         return $fields;
  154.     }
  155.     /**
  156.      * Return a localized array of the object.
  157.      *
  158.      * @return mixed[]
  159.      */
  160.     public function serializeForLocale(string $locale, ?Dynamic $dynamic null): array
  161.     {
  162.         $fields = [];
  163.         foreach ($this->fields as $field) {
  164.             $fieldTranslation $field->getTranslation($localefalsetrue);
  165.             $value null;
  166.             if ($dynamic) {
  167.                 $value $dynamic->getField($field->getKey());
  168.             }
  169.             $fields[$field->getOrder()] = [
  170.                 'key' => $field->getKey(),
  171.                 'type' => $field->getType(),
  172.                 'title' => $fieldTranslation->getTitle(),
  173.                 'options' => $fieldTranslation->getOptions(),
  174.                 'defaultValue' => $fieldTranslation->getDefaultValue(),
  175.                 'placeholder' => $fieldTranslation->getPlaceholder(),
  176.                 'shortTitle' => $fieldTranslation->getShortTitle(),
  177.                 'value' => $value,
  178.             ];
  179.             \ksort($fields);
  180.         }
  181.         $translation $this->getTranslation($localefalsetrue);
  182.         return [
  183.             'id' => $dynamic $dynamic->getId() : null,
  184.             'formId' => $this->getId(),
  185.             'title' => $translation->getTitle(),
  186.             'subject' => $translation->getSubject(),
  187.             'mailText' => $translation->getMailText(),
  188.             'submitLabel' => $translation->getSubmitLabel(),
  189.             'successText' => $translation->getSuccessText(),
  190.             'fromEmail' => $translation->getFromEmail(),
  191.             'fromName' => $translation->getFromName(),
  192.             'toEmail' => $translation->getToEmail(),
  193.             'toName' => $translation->getToName(),
  194.             'fields' => $fields,
  195.             'created' => $dynamic $dynamic->getCreated() : null,
  196.         ];
  197.     }
  198. }