diff --git a/lib/API/JSON/v1/Forms.php b/lib/API/JSON/v1/Forms.php index c8467b35d9..07acc90d3a 100644 --- a/lib/API/JSON/v1/Forms.php +++ b/lib/API/JSON/v1/Forms.php @@ -209,27 +209,20 @@ class Forms extends APIEndpoint { // check if the user gets to pick his own lists // or if it's selected by the admin - $hasSegmentSelection = false; + $formEntity = new FormEntity($name); + $formEntity->setBody($body); + $listSelectionBlocks = $formEntity->getBlocksByType(FormEntity::SEGMENT_SELECTION_BLOCK_TYPE); $listSelection = []; - foreach ($body as $i => $block) { - if ($block['type'] === 'segment') { - $hasSegmentSelection = true; - if (!empty($block['params']['values'])) { - $listSelection = array_filter( - array_map(function($segment) { - return (isset($segment['id']) - ? $segment['id'] - : null - ); - }, $block['params']['values']) - ); - } - break; - } + foreach ($listSelectionBlocks as $listSelectionBlock) { + $listSelection = array_unique( + array_merge( + $listSelection, array_column($listSelectionBlock['params']['values'] ?? [], 'id') + ) + ); } // check list selection - if ($hasSegmentSelection === true) { + if (count($listSelectionBlocks)) { $settings['segments_selected_by'] = 'user'; $settings['segments'] = $listSelection; } else { diff --git a/tests/integration/API/JSON/v1/FormsTest.php b/tests/integration/API/JSON/v1/FormsTest.php index d19bb6c0da..82916fc797 100644 --- a/tests/integration/API/JSON/v1/FormsTest.php +++ b/tests/integration/API/JSON/v1/FormsTest.php @@ -133,6 +133,36 @@ class FormsTest extends \MailPoetTest { expect($response->data['settings']['segments'])->equals([1, 3]); } + public function testItCanExtractListsFromNestedListSelectionBlock() { + $response = $this->endpoint->create(); + expect($response->status)->equals(APIResponse::STATUS_OK); + + $form = Form::findOne($response->data['id'])->asArray(); + $form['body'][] = [ + 'type' => 'segment', + 'params' => [ + 'values' => [['id' => 2], ['id' => 4]], + ], + ]; + + $form['body'] = [ + [ + 'type' => 'columns', + 'body' => [ + [ + 'type' => 'column', + 'body' => $form['body'], + ], + ], + ], + ]; + + $response = $this->endpoint->saveEditor($form); + expect($response->status)->equals(APIResponse::STATUS_OK); + expect($response->data['settings']['segments_selected_by'])->equals('user'); + expect($response->data['settings']['segments'])->equals([2, 4]); + } + public function testItCanRestoreAForm() { $this->form1->trash();