CustomFieldEntity.php
2 months ago
DynamicSegmentFilterData.php
2 months ago
DynamicSegmentFilterEntity.php
3 years ago
FeatureFlagEntity.php
3 years ago
FormEntity.php
1 week ago
LogEntity.php
3 years ago
NewsletterEntity.php
2 months ago
NewsletterLinkEntity.php
3 years ago
NewsletterOptionEntity.php
3 years ago
NewsletterOptionFieldEntity.php
1 month ago
NewsletterPostEntity.php
3 years ago
NewsletterSegmentEntity.php
3 years ago
NewsletterTemplateEntity.php
3 years ago
ScheduledTaskEntity.php
1 month ago
ScheduledTaskSubscriberEntity.php
1 year ago
SegmentEntity.php
2 months ago
SendingQueueEntity.php
2 months ago
SettingEntity.php
3 years ago
StatisticsBounceEntity.php
3 years ago
StatisticsClickEntity.php
3 years ago
StatisticsFormEntity.php
3 years ago
StatisticsNewsletterEntity.php
1 year ago
StatisticsOpenEntity.php
3 years ago
StatisticsUnsubscribeEntity.php
2 months ago
StatisticsWooCommercePurchaseEntity.php
2 years ago
StatsNotificationEntity.php
3 years ago
SubscriberCustomFieldEntity.php
3 years ago
SubscriberEntity.php
5 days ago
SubscriberIPEntity.php
3 years ago
SubscriberSegmentEntity.php
3 years ago
SubscriberTagEntity.php
4 years ago
TagEntity.php
3 years ago
UserAgentEntity.php
3 years ago
UserFlagEntity.php
3 years ago
WpPostEntity.php
2 years ago
index.php
3 years ago
FormEntity.php
223 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Entities; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\EntityTraits\AutoincrementedIdTrait; |
| 9 | use MailPoet\Doctrine\EntityTraits\CreatedAtTrait; |
| 10 | use MailPoet\Doctrine\EntityTraits\DeletedAtTrait; |
| 11 | use MailPoet\Doctrine\EntityTraits\UpdatedAtTrait; |
| 12 | use MailPoetVendor\Doctrine\ORM\Mapping as ORM; |
| 13 | |
| 14 | /** |
| 15 | * @ORM\Entity() |
| 16 | * @ORM\Table(name="forms") |
| 17 | */ |
| 18 | class FormEntity { |
| 19 | use AutoincrementedIdTrait; |
| 20 | use CreatedAtTrait; |
| 21 | use UpdatedAtTrait; |
| 22 | use DeletedAtTrait; |
| 23 | |
| 24 | const DISPLAY_TYPE_BELOW_POST = 'below_posts'; |
| 25 | const DISPLAY_TYPE_FIXED_BAR = 'fixed_bar'; |
| 26 | const DISPLAY_TYPE_POPUP = 'popup'; |
| 27 | const DISPLAY_TYPE_SLIDE_IN = 'slide_in'; |
| 28 | const DISPLAY_TYPE_OTHERS = 'others'; |
| 29 | |
| 30 | const STATUS_ENABLED = 'enabled'; |
| 31 | const STATUS_DISABLED = 'disabled'; |
| 32 | |
| 33 | const HTML_BLOCK_TYPE = 'html'; |
| 34 | const HEADING_BLOCK_TYPE = 'heading'; |
| 35 | const IMAGE_BLOCK_TYPE = 'image'; |
| 36 | const PARAGRAPH_BLOCK_TYPE = 'paragraph'; |
| 37 | const DIVIDER_BLOCK_TYPE = 'divider'; |
| 38 | const CHECKBOX_BLOCK_TYPE = 'checkbox'; |
| 39 | const RADIO_BLOCK_TYPE = 'radio'; |
| 40 | const SEGMENT_SELECTION_BLOCK_TYPE = 'segment'; |
| 41 | const DATE_BLOCK_TYPE = 'date'; |
| 42 | const SELECT_BLOCK_TYPE = 'select'; |
| 43 | const TEXT_BLOCK_TYPE = 'text'; |
| 44 | const TEXTAREA_BLOCK_TYPE = 'textarea'; |
| 45 | const SUBMIT_BLOCK_TYPE = 'submit'; |
| 46 | const CLOSE_BLOCK_TYPE = 'close'; |
| 47 | const COLUMNS_BLOCK_TYPE = 'columns'; |
| 48 | const COLUMN_BLOCK_TYPE = 'column'; |
| 49 | |
| 50 | public const FORM_DISPLAY_TYPES = [ |
| 51 | self::DISPLAY_TYPE_BELOW_POST, |
| 52 | self::DISPLAY_TYPE_FIXED_BAR, |
| 53 | self::DISPLAY_TYPE_POPUP, |
| 54 | self::DISPLAY_TYPE_SLIDE_IN, |
| 55 | self::DISPLAY_TYPE_OTHERS, |
| 56 | ]; |
| 57 | |
| 58 | public const FORM_FIELD_TYPES = [ |
| 59 | self::CHECKBOX_BLOCK_TYPE, |
| 60 | self::RADIO_BLOCK_TYPE, |
| 61 | self::SEGMENT_SELECTION_BLOCK_TYPE, |
| 62 | self::DATE_BLOCK_TYPE, |
| 63 | self::SELECT_BLOCK_TYPE, |
| 64 | self::TEXT_BLOCK_TYPE, |
| 65 | self::TEXTAREA_BLOCK_TYPE, |
| 66 | ]; |
| 67 | |
| 68 | /** |
| 69 | * @ORM\Column(type="string") |
| 70 | * @var string |
| 71 | */ |
| 72 | private $name; |
| 73 | |
| 74 | /** |
| 75 | * @ORM\Column(type="serialized_array") |
| 76 | * @var array|null |
| 77 | */ |
| 78 | private $body; |
| 79 | |
| 80 | /** |
| 81 | * @ORM\Column(type="string") |
| 82 | * @var string |
| 83 | */ |
| 84 | private $status; |
| 85 | |
| 86 | /** |
| 87 | * @ORM\Column(type="serialized_array") |
| 88 | * @var array|null |
| 89 | */ |
| 90 | private $settings; |
| 91 | |
| 92 | /** |
| 93 | * @ORM\Column(type="string", nullable=true) |
| 94 | * @var string|null |
| 95 | */ |
| 96 | private $styles; |
| 97 | |
| 98 | public function __construct( |
| 99 | $name |
| 100 | ) { |
| 101 | $this->name = $name; |
| 102 | $this->status = self::STATUS_ENABLED; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @return string |
| 107 | */ |
| 108 | public function getName() { |
| 109 | return $this->name; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @return array|null |
| 114 | */ |
| 115 | public function getBody() { |
| 116 | return $this->body; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @return array|null |
| 121 | */ |
| 122 | public function getSettings() { |
| 123 | return $this->settings; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @return string|null |
| 128 | */ |
| 129 | public function getStyles() { |
| 130 | return $this->styles; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @param string $name |
| 135 | */ |
| 136 | public function setName($name) { |
| 137 | $this->name = $name; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param array|null $body |
| 142 | */ |
| 143 | public function setBody($body) { |
| 144 | $this->body = $body; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @param array|null $settings |
| 149 | */ |
| 150 | public function setSettings($settings) { |
| 151 | $this->settings = $settings; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @param string|null $styles |
| 156 | */ |
| 157 | public function setStyles($styles) { |
| 158 | $this->styles = $styles; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @param string $status |
| 163 | */ |
| 164 | public function setStatus(string $status) { |
| 165 | $this->status = $status; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @return string |
| 170 | */ |
| 171 | public function getStatus(): string { |
| 172 | return $this->status; |
| 173 | } |
| 174 | |
| 175 | public function toArray(): array { |
| 176 | return [ |
| 177 | 'id' => $this->getId(), |
| 178 | 'name' => $this->getName(), |
| 179 | 'body' => $this->getBody(), |
| 180 | 'settings' => $this->getSettings(), |
| 181 | 'styles' => $this->getStyles(), |
| 182 | 'status' => $this->getStatus(), |
| 183 | 'created_at' => $this->getCreatedAt(), |
| 184 | 'updated_at' => $this->getUpdatedAt(), |
| 185 | 'deleted_at' => $this->getDeletedAt(), |
| 186 | ]; |
| 187 | } |
| 188 | |
| 189 | public function getBlocksByTypes(array $types, ?array $blocks = null): array { |
| 190 | $found = []; |
| 191 | if ($blocks === null) { |
| 192 | $blocks = $this->getBody() ?? []; |
| 193 | } |
| 194 | foreach ($blocks as $block) { |
| 195 | if (isset($block['type']) && in_array($block['type'], $types, true)) { |
| 196 | $found[] = $block; |
| 197 | } |
| 198 | if (isset($block['body']) && is_array($block['body']) && !empty($block['body'])) { |
| 199 | $found = array_merge($found, $this->getBlocksByTypes($types, $block['body'])); |
| 200 | } |
| 201 | } |
| 202 | return $found; |
| 203 | } |
| 204 | |
| 205 | public function getSegmentBlocksSegmentIds(): array { |
| 206 | $listSelectionBlocks = $this->getBlocksByTypes([FormEntity::SEGMENT_SELECTION_BLOCK_TYPE]); |
| 207 | $listSelection = []; |
| 208 | foreach ($listSelectionBlocks as $listSelectionBlock) { |
| 209 | $listSelection = array_unique( |
| 210 | array_merge( |
| 211 | $listSelection, |
| 212 | array_column($listSelectionBlock['params']['values'] ?? [], 'id') |
| 213 | ) |
| 214 | ); |
| 215 | } |
| 216 | return $listSelection; |
| 217 | } |
| 218 | |
| 219 | public function getSettingsSegmentIds(): array { |
| 220 | return $this->settings['segments'] ?? []; |
| 221 | } |
| 222 | } |
| 223 |