Refactor getting segments from form body to form entity

[MAILPOET-3297]
This commit is contained in:
Rostislav Wolny
2020-12-28 11:15:40 +01:00
committed by Veljko V
parent 36a7cc0b25
commit af574f7e1b
3 changed files with 34 additions and 10 deletions

View File

@ -211,18 +211,10 @@ class Forms extends APIEndpoint {
// or if it's selected by the admin // or if it's selected by the admin
$formEntity = new FormEntity($name); $formEntity = new FormEntity($name);
$formEntity->setBody($body); $formEntity->setBody($body);
$listSelectionBlocks = $formEntity->getBlocksByType(FormEntity::SEGMENT_SELECTION_BLOCK_TYPE); $listSelection = $formEntity->getSegmentBlocksSegmentIds();
$listSelection = [];
foreach ($listSelectionBlocks as $listSelectionBlock) {
$listSelection = array_unique(
array_merge(
$listSelection, array_column($listSelectionBlock['params']['values'] ?? [], 'id')
)
);
}
// check list selection // check list selection
if (count($listSelectionBlocks)) { if (count($listSelection)) {
$settings['segments_selected_by'] = 'user'; $settings['segments_selected_by'] = 'user';
$settings['segments'] = $listSelection; $settings['segments'] = $listSelection;
} else { } else {

View File

@ -177,4 +177,17 @@ class FormEntity {
} }
return $found; return $found;
} }
public function getSegmentBlocksSegmentIds(): array {
$listSelectionBlocks = $this->getBlocksByType(FormEntity::SEGMENT_SELECTION_BLOCK_TYPE);
$listSelection = [];
foreach ($listSelectionBlocks as $listSelectionBlock) {
$listSelection = array_unique(
array_merge(
$listSelection, array_column($listSelectionBlock['params']['values'] ?? [], 'id')
)
);
}
return $listSelection;
}
} }

View File

@ -68,4 +68,23 @@ class FormEntityTest extends \MailPoetUnitTest {
expect($columns)->count(1); expect($columns)->count(1);
expect($columns[0]['body'])->count(2); expect($columns[0]['body'])->count(2);
} }
public function testGetSegmentSelectionSegmentIds() {
$formEntity = new FormEntity('Test' );
$formEntity->setBody($this->body);
$segmentIds = $formEntity->getSegmentBlocksSegmentIds();
expect($segmentIds)->isEmpty();
// Add segment selection block to second columns
$body = $this->body;
$body[0]['body'][1]['body'][] = [
'type' => 'segment',
'params' => [
'values' => [['id' => 1], ['id' => 3]],
],
];
$formEntity->setBody($body);
$segmentIds = $formEntity->getSegmentBlocksSegmentIds();
expect($segmentIds)->equals([1, 3]);
}
} }