custom/plugins/MoorlFormBuilder/src/Core/Content/Form/FormEntity.php line 10

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFormBuilder\Core\Content\Form;
  3. use MoorlFormBuilder\Core\Content\FormElement\FormElement;
  4. use MoorlFormBuilder\Core\Content\FormElement\FormElementInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityCustomFieldsTrait;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityIdTrait;
  8. use Shopware\Core\Content\Product\ProductCollection;
  9. use Shopware\Core\Content\Media\MediaCollection;
  10. use ScssPhp\ScssPhp\Compiler;
  11. use ScssPhp\ScssPhp\Formatter\Crunched;
  12. use ScssPhp\ScssPhp\Formatter\Expanded;
  13. class FormEntity extends Entity
  14. {
  15.     use EntityIdTrait;
  16.     use EntityCustomFieldsTrait;
  17.     /**
  18.      * @var FormElementInterface[]
  19.      */
  20.     protected iterable $formElements;
  21.     protected ?string $salesChannelId null;
  22.     protected ?string $replyTo null;
  23.     protected ?string $summaryHTML null;
  24.     protected ?string $summaryPlain null;
  25.     protected ?array $payload null;
  26.     protected bool $initialized false;
  27.     protected ?array $submitText null// CHECK
  28.     protected bool $submitDisabled false// CHECK
  29.     protected ?array $successMessage null;
  30.     protected array $label;
  31.     protected int $maxFileSize 0;
  32.     protected ?string $redirectTo null;
  33.     protected ?string $redirectParams null;
  34.     protected ?string $relatedEntity null;
  35.     protected bool $active false;
  36.     protected bool $insertNewsletter false;
  37.     protected bool $insertHistory false;
  38.     protected bool $privacy true;
  39.     protected bool $locked false;
  40.     protected bool $bootstrapGrid true;
  41.     protected bool $useCaptcha true;
  42.     protected bool $sendMail false;
  43.     protected ?string $captcha null;
  44.     protected ?string $stylesheet null;
  45.     /**
  46.      * @depraced: v6.5
  47.      */
  48.     protected bool $useTrans false;
  49.     /**
  50.      * @depraced: v6.5
  51.      */
  52.     protected bool $sendCopy false;
  53.     protected bool $bootstrapWideSpacing false;
  54.     protected bool $useSassCompiler false;
  55.     protected bool $insertDatabase false;
  56.     protected ?string $name null;
  57.     protected ?string $action null;
  58.     protected string $type;
  59.     protected ?string $emailReceiver null;
  60.     protected ?string $customerEmailReceiver null;
  61.     protected ?string $mailTemplateId null;
  62.     protected ?string $customerMailTemplateId null;
  63.     protected ?string $mediaFolderId null;
  64.     protected ?string $mediaFolder null;
  65.     protected ?ProductCollection $products null;
  66.     protected ?MediaCollection $medias null;
  67.     protected ?array $data null;
  68.     protected ?array $userValues = [];
  69.     protected array $binAttachments = [];
  70.     protected ?array $redirectConditions = [];
  71.     protected string $sendCopyType;
  72.     /**
  73.      * @return array|null
  74.      */
  75.     public function getSuccessMessage(): ?array
  76.     {
  77.         return $this->successMessage;
  78.     }
  79.     /**
  80.      * @param array|null $successMessage
  81.      */
  82.     public function setSuccessMessage(?array $successMessage): void
  83.     {
  84.         $this->successMessage $successMessage;
  85.     }
  86.     /**
  87.      * @return FormElementInterface[]
  88.      */
  89.     public function getFormElements(): iterable
  90.     {
  91.         return $this->formElements;
  92.     }
  93.     /**
  94.      * @param FormElementInterface[] $formElements
  95.      */
  96.     public function setFormElements(iterable $formElements): void
  97.     {
  98.         $this->formElements $formElements;
  99.     }
  100.     /**
  101.      * @return string|null
  102.      */
  103.     public function getSalesChannelId(): ?string
  104.     {
  105.         return $this->salesChannelId;
  106.     }
  107.     /**
  108.      * @param string|null $salesChannelId
  109.      */
  110.     public function setSalesChannelId(?string $salesChannelId): void
  111.     {
  112.         $this->salesChannelId $salesChannelId;
  113.     }
  114.     /**
  115.      * @return string|null
  116.      */
  117.     public function getReplyTo(): ?string
  118.     {
  119.         return $this->replyTo;
  120.     }
  121.     /**
  122.      * @param string|null $replyTo
  123.      */
  124.     public function setReplyTo(?string $replyTo): void
  125.     {
  126.         $this->replyTo $replyTo;
  127.     }
  128.     /**
  129.      * @return string|null
  130.      */
  131.     public function getSummaryHTML(): ?string
  132.     {
  133.         return $this->summaryHTML;
  134.     }
  135.     /**
  136.      * @param string|null $summaryHTML
  137.      */
  138.     public function setSummaryHTML(?string $summaryHTML): void
  139.     {
  140.         $this->summaryHTML $summaryHTML;
  141.     }
  142.     /**
  143.      * @return string|null
  144.      */
  145.     public function getSummaryPlain(): ?string
  146.     {
  147.         return $this->summaryPlain;
  148.     }
  149.     /**
  150.      * @param string|null $summaryPlain
  151.      */
  152.     public function setSummaryPlain(?string $summaryPlain): void
  153.     {
  154.         $this->summaryPlain $summaryPlain;
  155.     }
  156.     /**
  157.      * @return array|null
  158.      */
  159.     public function getPayload(): ?array
  160.     {
  161.         return $this->payload;
  162.     }
  163.     /**
  164.      * @param array|null $payload
  165.      */
  166.     public function setPayload(?array $payload): void
  167.     {
  168.         $this->payload $payload;
  169.     }
  170.     /**
  171.      * @return bool
  172.      */
  173.     public function getInitialized(): bool
  174.     {
  175.         return $this->initialized;
  176.     }
  177.     /**
  178.      * @param bool $initialized
  179.      */
  180.     public function setInitialized(bool $initialized): void
  181.     {
  182.         $this->initialized $initialized;
  183.     }
  184.     /**
  185.      * @return array|null
  186.      */
  187.     public function getSubmitText(): ?array
  188.     {
  189.         return $this->submitText;
  190.     }
  191.     /**
  192.      * @param array|null $submitText
  193.      */
  194.     public function setSubmitText(?array $submitText): void
  195.     {
  196.         $this->submitText $submitText;
  197.     }
  198.     /**
  199.      * @return bool
  200.      */
  201.     public function getSubmitDisabled(): bool
  202.     {
  203.         return $this->submitDisabled;
  204.     }
  205.     /**
  206.      * @param bool $submitDisabled
  207.      */
  208.     public function setSubmitDisabled(bool $submitDisabled): void
  209.     {
  210.         $this->submitDisabled $submitDisabled;
  211.     }
  212.     /**
  213.      * @return array
  214.      */
  215.     public function getLabel(): array
  216.     {
  217.         return $this->label;
  218.     }
  219.     /**
  220.      * @param array $label
  221.      */
  222.     public function setLabel(array $label): void
  223.     {
  224.         $this->label $label;
  225.     }
  226.     /**
  227.      * @return int
  228.      */
  229.     public function getMaxFileSize(): int
  230.     {
  231.         return $this->maxFileSize;
  232.     }
  233.     /**
  234.      * @param int $maxFileSize
  235.      */
  236.     public function setMaxFileSize(int $maxFileSize): void
  237.     {
  238.         $this->maxFileSize $maxFileSize;
  239.     }
  240.     /**
  241.      * @return string|null
  242.      */
  243.     public function getRedirectTo(): ?string
  244.     {
  245.         return $this->redirectTo;
  246.     }
  247.     /**
  248.      * @param string|null $redirectTo
  249.      */
  250.     public function setRedirectTo(?string $redirectTo): void
  251.     {
  252.         $this->redirectTo $redirectTo;
  253.     }
  254.     /**
  255.      * @return string|null
  256.      */
  257.     public function getRedirectParams(): ?string
  258.     {
  259.         return $this->redirectParams;
  260.     }
  261.     /**
  262.      * @param string|null $redirectParams
  263.      */
  264.     public function setRedirectParams(?string $redirectParams): void
  265.     {
  266.         $this->redirectParams $redirectParams;
  267.     }
  268.     /**
  269.      * @return string|null
  270.      */
  271.     public function getRelatedEntity(): ?string
  272.     {
  273.         return $this->relatedEntity;
  274.     }
  275.     /**
  276.      * @param string|null $relatedEntity
  277.      */
  278.     public function setRelatedEntity(?string $relatedEntity): void
  279.     {
  280.         $this->relatedEntity $relatedEntity;
  281.     }
  282.     /**
  283.      * @return bool
  284.      */
  285.     public function getActive(): bool
  286.     {
  287.         return $this->active;
  288.     }
  289.     /**
  290.      * @param bool $active
  291.      */
  292.     public function setActive(bool $active): void
  293.     {
  294.         $this->active $active;
  295.     }
  296.     /**
  297.      * @return bool
  298.      */
  299.     public function getInsertNewsletter(): bool
  300.     {
  301.         return $this->insertNewsletter;
  302.     }
  303.     /**
  304.      * @param bool $insertNewsletter
  305.      */
  306.     public function setInsertNewsletter(bool $insertNewsletter): void
  307.     {
  308.         $this->insertNewsletter $insertNewsletter;
  309.     }
  310.     /**
  311.      * @return bool
  312.      */
  313.     public function getInsertHistory(): bool
  314.     {
  315.         return $this->insertHistory;
  316.     }
  317.     /**
  318.      * @param bool $insertHistory
  319.      */
  320.     public function setInsertHistory(bool $insertHistory): void
  321.     {
  322.         $this->insertHistory $insertHistory;
  323.     }
  324.     /**
  325.      * @return bool
  326.      */
  327.     public function getPrivacy(): bool
  328.     {
  329.         return $this->privacy;
  330.     }
  331.     /**
  332.      * @param bool $privacy
  333.      */
  334.     public function setPrivacy(bool $privacy): void
  335.     {
  336.         $this->privacy $privacy;
  337.     }
  338.     /**
  339.      * @return bool
  340.      */
  341.     public function getLocked(): bool
  342.     {
  343.         return $this->locked;
  344.     }
  345.     /**
  346.      * @param bool $locked
  347.      */
  348.     public function setLocked(bool $locked): void
  349.     {
  350.         $this->locked $locked;
  351.     }
  352.     /**
  353.      * @return bool
  354.      */
  355.     public function getBootstrapGrid(): bool
  356.     {
  357.         return $this->bootstrapGrid;
  358.     }
  359.     /**
  360.      * @param bool $bootstrapGrid
  361.      */
  362.     public function setBootstrapGrid(bool $bootstrapGrid): void
  363.     {
  364.         $this->bootstrapGrid $bootstrapGrid;
  365.     }
  366.     /**
  367.      * @return bool
  368.      */
  369.     public function getUseCaptcha(): bool
  370.     {
  371.         return $this->useCaptcha;
  372.     }
  373.     /**
  374.      * @param bool $useCaptcha
  375.      */
  376.     public function setUseCaptcha(bool $useCaptcha): void
  377.     {
  378.         $this->useCaptcha $useCaptcha;
  379.     }
  380.     /**
  381.      * @return bool
  382.      */
  383.     public function getSendMail(): bool
  384.     {
  385.         return $this->sendMail;
  386.     }
  387.     /**
  388.      * @param bool $sendMail
  389.      */
  390.     public function setSendMail(bool $sendMail): void
  391.     {
  392.         $this->sendMail $sendMail;
  393.     }
  394.     /**
  395.      * @return string|null
  396.      */
  397.     public function getCaptcha(): ?string
  398.     {
  399.         return $this->captcha;
  400.     }
  401.     /**
  402.      * @param string|null $captcha
  403.      */
  404.     public function setCaptcha(?string $captcha): void
  405.     {
  406.         $this->captcha $captcha;
  407.     }
  408.     /**
  409.      * @return bool
  410.      */
  411.     public function getUseTrans(): bool
  412.     {
  413.         return $this->useTrans;
  414.     }
  415.     /**
  416.      * @param bool $useTrans
  417.      */
  418.     public function setUseTrans(bool $useTrans): void
  419.     {
  420.         $this->useTrans $useTrans;
  421.     }
  422.     /**
  423.      * @return bool
  424.      */
  425.     public function getSendCopy(): bool
  426.     {
  427.         return $this->sendCopy;
  428.     }
  429.     /**
  430.      * @param bool $sendCopy
  431.      */
  432.     public function setSendCopy(bool $sendCopy): void
  433.     {
  434.         $this->sendCopy $sendCopy;
  435.     }
  436.     /**
  437.      * @return bool
  438.      */
  439.     public function getBootstrapWideSpacing(): bool
  440.     {
  441.         return $this->bootstrapWideSpacing;
  442.     }
  443.     /**
  444.      * @param bool $bootstrapWideSpacing
  445.      */
  446.     public function setBootstrapWideSpacing(bool $bootstrapWideSpacing): void
  447.     {
  448.         $this->bootstrapWideSpacing $bootstrapWideSpacing;
  449.     }
  450.     /**
  451.      * @return bool
  452.      */
  453.     public function getUseSassCompiler(): bool
  454.     {
  455.         return $this->useSassCompiler;
  456.     }
  457.     /**
  458.      * @param bool $useSassCompiler
  459.      */
  460.     public function setUseSassCompiler(bool $useSassCompiler): void
  461.     {
  462.         $this->useSassCompiler $useSassCompiler;
  463.     }
  464.     /**
  465.      * @return bool
  466.      */
  467.     public function getInsertDatabase(): bool
  468.     {
  469.         return $this->insertDatabase;
  470.     }
  471.     /**
  472.      * @param bool $insertDatabase
  473.      */
  474.     public function setInsertDatabase(bool $insertDatabase): void
  475.     {
  476.         $this->insertDatabase $insertDatabase;
  477.     }
  478.     /**
  479.      * @return string|null
  480.      */
  481.     public function getName(): ?string
  482.     {
  483.         return $this->name;
  484.     }
  485.     /**
  486.      * @param string|null $name
  487.      */
  488.     public function setName(?string $name): void
  489.     {
  490.         $this->name $name;
  491.     }
  492.     /**
  493.      * @return string|null
  494.      */
  495.     public function getAction(): ?string
  496.     {
  497.         return $this->action;
  498.     }
  499.     /**
  500.      * @param string|null $action
  501.      */
  502.     public function setAction(?string $action): void
  503.     {
  504.         $this->action $action;
  505.     }
  506.     /**
  507.      * @return string
  508.      */
  509.     public function getType(): string
  510.     {
  511.         return $this->type;
  512.     }
  513.     /**
  514.      * @param string $type
  515.      */
  516.     public function setType(string $type): void
  517.     {
  518.         $this->type $type;
  519.     }
  520.     /**
  521.      * @return string|null
  522.      */
  523.     public function getEmailReceiver(): ?string
  524.     {
  525.         return $this->emailReceiver;
  526.     }
  527.     /**
  528.      * @param string|null $emailReceiver
  529.      */
  530.     public function setEmailReceiver(?string $emailReceiver): void
  531.     {
  532.         $this->emailReceiver $emailReceiver;
  533.     }
  534.     /**
  535.      * @return string|null
  536.      */
  537.     public function getCustomerEmailReceiver(): ?string
  538.     {
  539.         return $this->customerEmailReceiver;
  540.     }
  541.     /**
  542.      * @param string|null $customerEmailReceiver
  543.      */
  544.     public function setCustomerEmailReceiver(?string $customerEmailReceiver): void
  545.     {
  546.         $this->customerEmailReceiver $customerEmailReceiver;
  547.     }
  548.     /**
  549.      * @return string|null
  550.      */
  551.     public function getMailTemplateId(): ?string
  552.     {
  553.         return $this->mailTemplateId;
  554.     }
  555.     /**
  556.      * @param string|null $mailTemplateId
  557.      */
  558.     public function setMailTemplateId(?string $mailTemplateId): void
  559.     {
  560.         $this->mailTemplateId $mailTemplateId;
  561.     }
  562.     /**
  563.      * @return string|null
  564.      */
  565.     public function getCustomerMailTemplateId(): ?string
  566.     {
  567.         return $this->customerMailTemplateId;
  568.     }
  569.     /**
  570.      * @param string|null $customerMailTemplateId
  571.      */
  572.     public function setCustomerMailTemplateId(?string $customerMailTemplateId): void
  573.     {
  574.         $this->customerMailTemplateId $customerMailTemplateId;
  575.     }
  576.     /**
  577.      * @return string|null
  578.      */
  579.     public function getMediaFolderId(): ?string
  580.     {
  581.         return $this->mediaFolderId;
  582.     }
  583.     /**
  584.      * @param string|null $mediaFolderId
  585.      */
  586.     public function setMediaFolderId(?string $mediaFolderId): void
  587.     {
  588.         $this->mediaFolderId $mediaFolderId;
  589.     }
  590.     /**
  591.      * @return string|null
  592.      */
  593.     public function getMediaFolder(): ?string
  594.     {
  595.         return $this->mediaFolder;
  596.     }
  597.     /**
  598.      * @param string|null $mediaFolder
  599.      */
  600.     public function setMediaFolder(?string $mediaFolder): void
  601.     {
  602.         $this->mediaFolder $mediaFolder;
  603.     }
  604.     /**
  605.      * @return ProductCollection|null
  606.      */
  607.     public function getProducts(): ?ProductCollection
  608.     {
  609.         return $this->products;
  610.     }
  611.     /**
  612.      * @param ProductCollection|null $products
  613.      */
  614.     public function setProducts(?ProductCollection $products): void
  615.     {
  616.         $this->products $products;
  617.     }
  618.     /**
  619.      * @return MediaCollection|null
  620.      */
  621.     public function getMedias(): ?MediaCollection
  622.     {
  623.         return $this->medias;
  624.     }
  625.     /**
  626.      * @param MediaCollection|null $medias
  627.      */
  628.     public function setMedias(?MediaCollection $medias): void
  629.     {
  630.         $this->medias $medias;
  631.     }
  632.     /**
  633.      * @return array|null
  634.      */
  635.     public function getUserValues(): ?array
  636.     {
  637.         return $this->userValues;
  638.     }
  639.     /**
  640.      * @param array|null $userValues
  641.      */
  642.     public function setUserValues(?array $userValues): void
  643.     {
  644.         $this->userValues $userValues;
  645.     }
  646.     /**
  647.      * @return array
  648.      */
  649.     public function getBinAttachments(): array
  650.     {
  651.         return $this->binAttachments;
  652.     }
  653.     /**
  654.      * @param array $binAttachments
  655.      */
  656.     public function setBinAttachments(array $binAttachments): void
  657.     {
  658.         $this->binAttachments $binAttachments;
  659.     }
  660.     /**
  661.      * @return array|null
  662.      */
  663.     public function getRedirectConditions(): ?array
  664.     {
  665.         return $this->redirectConditions;
  666.     }
  667.     /**
  668.      * @param array|null $redirectConditions
  669.      */
  670.     public function setRedirectConditions(?array $redirectConditions): void
  671.     {
  672.         $this->redirectConditions $redirectConditions;
  673.     }
  674.     /**
  675.      * @return string
  676.      */
  677.     public function getSendCopyType(): string
  678.     {
  679.         return $this->sendCopyType;
  680.     }
  681.     /**
  682.      * @param string $sendCopyType
  683.      */
  684.     public function setSendCopyType(string $sendCopyType): void
  685.     {
  686.         $this->sendCopyType $sendCopyType;
  687.     }
  688.     /* Custom methods below */
  689.     public function addBinAttachment(array $binAttachment): void
  690.     {
  691.         $this->binAttachments[] = $binAttachment;
  692.     }
  693.     public function getBootstrapRow(): string
  694.     {
  695.         if ($this->bootstrapWideSpacing) {
  696.             return "row";
  697.         }
  698.         return "row form-row";
  699.     }
  700.     public function getRepeaterDataValue(string $technicalName): ?string
  701.     {
  702.         if ($this->data && is_array($this->data)) {
  703.             foreach ($this->data as $formElement) {
  704.                 if ($formElement['name'] === $technicalName && is_array($formElement['value'])) {
  705.                     $i 0;
  706.                     $html "";
  707.                     foreach ($formElement['value'] as $repeater) {
  708.                         $i++;
  709.                         if (is_array($repeater)) {
  710.                             $html .= "\n#" $i "\n";
  711.                             foreach ($repeater as $repeaterKey => $repeaterValue) {
  712.                                 $html .= $this->getDataLabel($repeaterKey) . ": ";
  713.                                 $html .= $this->getDataValue($repeaterKey$repeaterValue);
  714.                                 $html .= "\n";
  715.                             }
  716.                         } else {
  717.                             $html .= "\n#" $i "\n";
  718.                             $html .= $repeater;
  719.                         }
  720.                     }
  721.                     return $html;
  722.                 }
  723.             }
  724.         }
  725.         return null;
  726.     }
  727.     public function getDataLabel(string $technicalName): ?string
  728.     {
  729.         if ($this->data && is_array($this->data)) {
  730.             foreach ($this->data as $formElement) {
  731.                 if ($formElement['name'] === $technicalName) {
  732.                     return $formElement['translated']['label'];
  733.                 }
  734.             }
  735.         }
  736.         return null;
  737.     }
  738.     public function getDataValue(string $technicalName$value null): ?string
  739.     {
  740.         if ($this->data && is_array($this->data)) {
  741.             foreach ($this->data as $formElement) {
  742.                 if ($formElement['name'] === $technicalName) {
  743.                     $value $value ?: $formElement['value'];
  744.                     if (in_array($formElement['type'], ['select','multiselect','radio-group','checkbox-group'])) {
  745.                         $txt = [];
  746.                         foreach ($formElement['options'] as $option) {
  747.                             if ($option['value'] == $value || (is_array($value) && in_array($option['value'], $value))) {
  748.                                 $txt[] = $option['translated']['label'];
  749.                             }
  750.                         }
  751.                         return implode(', '$txt);
  752.                     } else {
  753.                         return $value;
  754.                     }
  755.                 }
  756.             }
  757.         }
  758.         return null;
  759.     }
  760.     public function getFormElement(string $technicalName): FormElementInterface
  761.     {
  762.         foreach ($this->formElements as $formElement) {
  763.             if ($formElement->getName() === $technicalName) {
  764.                 return $formElement;
  765.             }
  766.         }
  767.     }
  768.     public function getStylesheet(): ?string
  769.     {
  770.         if ($this->useSassCompiler && $this->stylesheet) {
  771.             $compiler = new Compiler();
  772.             $compiler->setFormatter(Expanded::class);
  773.             return $compiler->compile(sprintf("#form-%s { %s }"$this->id$this->stylesheet));
  774.         }
  775.         return $this->stylesheet;
  776.     }
  777.     public function getData(): ?array
  778.     {
  779.         return $this->data;
  780.     }
  781.     public function setData(?array $data): void
  782.     {
  783.         $this->data $data;
  784.         $this->formElements = [];
  785.         foreach ($data as $formElement) {
  786.             $this->formElements[] = new FormElement($formElement);
  787.         }
  788.     }
  789.     public function addData(string $name, array $value): void
  790.     {
  791.         $this->data[$name] = $value;
  792.         $this->formElements[] = new FormElement($value);
  793.     }
  794.     public function enrichUserValues(?array $userValues null): void
  795.     {
  796.         if (!$userValues) {
  797.             return;
  798.         }
  799.         foreach ($this->data as &$dataValue) {
  800.             if (isset($userValues[$dataValue['name']])) {
  801.                 $dataValue['value'] = $userValues[$dataValue['name']];
  802.             }
  803.         }
  804.         foreach ($this->formElements as $formElement) {
  805.             if (isset($userValues[$formElement->getName()])) {
  806.                 $formElement->setValue($userValues[$formElement->getName()]);
  807.             }
  808.         }
  809.     }
  810. }