暫無描述

FormEntity.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace MailPoet\Entities;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Doctrine\EntityTraits\AutoincrementedIdTrait;
  5. use MailPoet\Doctrine\EntityTraits\CreatedAtTrait;
  6. use MailPoet\Doctrine\EntityTraits\DeletedAtTrait;
  7. use MailPoet\Doctrine\EntityTraits\UpdatedAtTrait;
  8. use MailPoetVendor\Doctrine\ORM\Mapping as ORM;
  9. /**
  10. * @ORM\Entity()
  11. * @ORM\Table(name="forms")
  12. */
  13. class FormEntity {
  14. use AutoincrementedIdTrait;
  15. use CreatedAtTrait;
  16. use UpdatedAtTrait;
  17. use DeletedAtTrait;
  18. const DISPLAY_TYPE_BELOW_POST = 'below_posts';
  19. const DISPLAY_TYPE_FIXED_BAR = 'fixed_bar';
  20. const DISPLAY_TYPE_POPUP = 'popup';
  21. const DISPLAY_TYPE_SLIDE_IN = 'slide_in';
  22. const DISPLAY_TYPE_OTHERS = 'others';
  23. const STATUS_ENABLED = 'enabled';
  24. const STATUS_DISABLED = 'disabled';
  25. const HTML_BLOCK_TYPE = 'html';
  26. const HEADING_BLOCK_TYPE = 'heading';
  27. const IMAGE_BLOCK_TYPE = 'image';
  28. const PARAGRAPH_BLOCK_TYPE = 'paragraph';
  29. const DIVIDER_BLOCK_TYPE = 'divider';
  30. const CHECKBOX_BLOCK_TYPE = 'checkbox';
  31. const RADIO_BLOCK_TYPE = 'radio';
  32. const SEGMENT_SELECTION_BLOCK_TYPE = 'segment';
  33. const DATE_BLOCK_TYPE = 'date';
  34. const SELECT_BLOCK_TYPE = 'select';
  35. const TEXT_BLOCK_TYPE = 'text';
  36. const TEXTAREA_BLOCK_TYPE = 'textarea';
  37. const SUBMIT_BLOCK_TYPE = 'submit';
  38. const COLUMNS_BLOCK_TYPE = 'columns';
  39. const COLUMN_BLOCK_TYPE = 'column';
  40. public const FORM_FIELD_TYPES = [
  41. self::CHECKBOX_BLOCK_TYPE,
  42. self::RADIO_BLOCK_TYPE,
  43. self::SEGMENT_SELECTION_BLOCK_TYPE,
  44. self::DATE_BLOCK_TYPE,
  45. self::SELECT_BLOCK_TYPE,
  46. self::TEXT_BLOCK_TYPE,
  47. self::TEXTAREA_BLOCK_TYPE,
  48. ];
  49. /**
  50. * @ORM\Column(type="string")
  51. * @var string
  52. */
  53. private $name;
  54. /**
  55. * @ORM\Column(type="serialized_array")
  56. * @var array|null
  57. */
  58. private $body;
  59. /**
  60. * @ORM\Column(type="string")
  61. * @var string
  62. */
  63. private $status;
  64. /**
  65. * @ORM\Column(type="serialized_array")
  66. * @var array|null
  67. */
  68. private $settings;
  69. /**
  70. * @ORM\Column(type="string", nullable=true)
  71. * @var string|null
  72. */
  73. private $styles;
  74. public function __construct(
  75. $name
  76. ) {
  77. $this->name = $name;
  78. $this->status = self::STATUS_ENABLED;
  79. }
  80. /**
  81. * @return string
  82. */
  83. public function getName() {
  84. return $this->name;
  85. }
  86. /**
  87. * @return array|null
  88. */
  89. public function getBody() {
  90. return $this->body;
  91. }
  92. /**
  93. * @return array|null
  94. */
  95. public function getSettings() {
  96. return $this->settings;
  97. }
  98. /**
  99. * @return string|null
  100. */
  101. public function getStyles() {
  102. return $this->styles;
  103. }
  104. /**
  105. * @param string $name
  106. */
  107. public function setName($name) {
  108. $this->name = $name;
  109. }
  110. /**
  111. * @param array|null $body
  112. */
  113. public function setBody($body) {
  114. $this->body = $body;
  115. }
  116. /**
  117. * @param array|null $settings
  118. */
  119. public function setSettings($settings) {
  120. $this->settings = $settings;
  121. }
  122. /**
  123. * @param string|null $styles
  124. */
  125. public function setStyles($styles) {
  126. $this->styles = $styles;
  127. }
  128. /**
  129. * @param string $status
  130. */
  131. public function setStatus(string $status) {
  132. $this->status = $status;
  133. }
  134. /**
  135. * @return string
  136. */
  137. public function getStatus(): string {
  138. return $this->status;
  139. }
  140. public function toArray(): array {
  141. return [
  142. 'id' => $this->getId(),
  143. 'name' => $this->getName(),
  144. 'body' => $this->getBody(),
  145. 'settings' => $this->getSettings(),
  146. 'styles' => $this->getStyles(),
  147. 'status' => $this->getStatus(),
  148. 'created_at' => $this->getCreatedAt(),
  149. 'updated_at' => $this->getUpdatedAt(),
  150. 'deleted_at' => $this->getDeletedAt(),
  151. ];
  152. }
  153. public function getBlocksByTypes(array $types, array $blocks = null): array {
  154. $found = [];
  155. if ($blocks === null) {
  156. $blocks = $this->getBody() ?? [];
  157. }
  158. foreach ($blocks as $block) {
  159. if (isset($block['type']) && in_array($block['type'], $types, true)) {
  160. $found[] = $block;
  161. }
  162. if (isset($block['body']) && is_array($block['body']) && !empty($block['body'])) {
  163. $found = array_merge($found, $this->getBlocksByTypes($types, $block['body']));
  164. }
  165. }
  166. return $found;
  167. }
  168. public function getSegmentBlocksSegmentIds(): array {
  169. $listSelectionBlocks = $this->getBlocksByTypes([FormEntity::SEGMENT_SELECTION_BLOCK_TYPE]);
  170. $listSelection = [];
  171. foreach ($listSelectionBlocks as $listSelectionBlock) {
  172. $listSelection = array_unique(
  173. array_merge(
  174. $listSelection, array_column($listSelectionBlock['params']['values'] ?? [], 'id')
  175. )
  176. );
  177. }
  178. return $listSelection;
  179. }
  180. public function getSettingsSegmentIds(): array {
  181. return $this->settings['segments'] ?? [];
  182. }
  183. }