Remove getFields method from FormEntity

[MAILPOET-3032]
This commit is contained in:
Jan Lysý
2021-04-08 12:39:12 +02:00
committed by Veljko V
parent 5af29f8aac
commit 02e1ce7e4d
5 changed files with 33 additions and 48 deletions

View File

@ -43,6 +43,16 @@ class FormEntity {
const COLUMNS_BLOCK_TYPE = 'columns';
const COLUMN_BLOCK_TYPE = 'column';
public const FORM_FIELD_TYPES = [
self::CHECKBOX_BLOCK_TYPE,
self::RADIO_BLOCK_TYPE,
self::SEGMENT_SELECTION_BLOCK_TYPE,
self::DATE_BLOCK_TYPE,
self::SELECT_BLOCK_TYPE,
self::TEXT_BLOCK_TYPE,
self::TEXTAREA_BLOCK_TYPE,
];
/**
* @ORM\Column(type="string")
* @var string
@ -162,24 +172,24 @@ class FormEntity {
];
}
public function getBlocksByType(string $type, array $blocks = null): array {
public function getBlocksByTypes(array $types, array $blocks = null): array {
$found = [];
if ($blocks === null) {
$blocks = $this->getBody() ?? [];
}
foreach ($blocks as $block) {
if ($block['type'] === $type) {
if (isset($block['type']) && in_array($block['type'], $types, true)) {
$found[] = $block;
}
if (isset($block['body']) && is_array($block['body']) && !empty($block['body'])) {
$found = array_merge($found, $this->getBlocksByType($type, $block['body']));
$found = array_merge($found, $this->getBlocksByTypes($types, $block['body']));
}
}
return $found;
}
public function getSegmentBlocksSegmentIds(): array {
$listSelectionBlocks = $this->getBlocksByType(FormEntity::SEGMENT_SELECTION_BLOCK_TYPE);
$listSelectionBlocks = $this->getBlocksByTypes([FormEntity::SEGMENT_SELECTION_BLOCK_TYPE]);
$listSelection = [];
foreach ($listSelectionBlocks as $listSelectionBlock) {
$listSelection = array_unique(
@ -194,37 +204,4 @@ class FormEntity {
public function getSettingsSegmentIds(): array {
return $this->settings['segments'] ?? [];
}
public function getFields(array $body = null): array {
$body = $body ?? $this->getBody();
if (empty($body)) {
return [];
}
$skippedTypes = ['html', 'divider', 'submit'];
$nestedTypes = ['column', 'columns'];
$fields = [];
foreach ($body as $field) {
if (!empty($field['type']) && in_array($field['type'], $nestedTypes) && !empty($field['body'])) {
$nestedFields = $this->getFields($field['body']);
if ($nestedFields) {
$fields = array_merge($fields, $nestedFields);
}
continue;
}
if (empty($field['id']) || empty($field['type']) || in_array($field['type'], $skippedTypes)) {
continue;
}
if ((int)$field['id'] > 0) {
$fields[] = 'cf_' . $field['id'];
} else {
$fields[] = $field['id'];
}
}
return $fields;
}
}