Skip to content

Commit 4db8455

Browse files
committed
fix(form,question): duplicate fail on form without section
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent 3a6968f commit 4db8455

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

inc/form.class.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,11 +1986,15 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
19861986
}
19871987
$importedItems[] = $importedItem;
19881988
}
1989-
// Delete all other restrictions
1989+
// Delete all other sections
1990+
$deleteCriteria = [];
1991+
if (count($importedItems) > 0) {
1992+
$deleteCriteria = ['NOT' => ['id' => $importedItems]];
1993+
}
19901994
$FormProfile = new PluginFormcreatorSection();
19911995
$FormProfile->deleteByCriteria([
19921996
$formFk => $itemId,
1993-
['NOT' => ['id' => $importedItems]]
1997+
$deleteCriteria,
19941998
]);
19951999
}
19962000

inc/section.class.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
262262
// add the section to the linker
263263
$linker->addObject($originalId, $item);
264264

265-
// Import the questions
265+
// Import each question
266+
$importedItems = [];
266267
if (isset($input['_questions'])) {
267268
// sort questions by order
268269
usort($input['_questions'], function ($a, $b) {
@@ -273,9 +274,24 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
273274
});
274275

275276
foreach ($input['_questions'] as $question) {
276-
PluginFormcreatorQuestion::import($linker, $question, $itemId);
277+
$importedItem = PluginFormcreatorQuestion::import($linker, $question, $itemId);
278+
if ($importedItem === false) {
279+
// Falied to import a question
280+
return false;
281+
}
282+
$importedItems[] = $importedItem;
277283
}
278284
}
285+
// Delete all other questions
286+
$deleteCriteria = [];
287+
if (count($importedItems) > 0) {
288+
$deleteCriteria = ['NOT' => ['id' => $importedItems]];
289+
}
290+
$FormProfile = new PluginFormcreatorSection();
291+
$FormProfile->deleteByCriteria([
292+
$formFk => $itemId,
293+
$deleteCriteria,
294+
]);
279295

280296
// Import conditions
281297
if (isset($input['_conditions'])) {

0 commit comments

Comments
 (0)