Skip to content

Commit f332fc6

Browse files
committed
fix(question): duplication may make several items having same position
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent dced8ee commit f332fc6

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

ajax/question_duplicate.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@
5050
exit;
5151
}
5252

53-
if (($newId = $question->duplicate(['progress' => false])) === false) {
53+
$newRow = 1 + PluginFormcreatorCommon::getMax(
54+
$question, [
55+
'plugin_formcreator_sections_id' => $question->fields['plugin_formcreator_sections_id']
56+
], 'row'
57+
);
58+
if (($newId = $question->duplicate(['progress' => false, 'fields' => ['row' => $newRow]])) === false) {
5459
http_response_code(500);
5560
exit;
5661
}
@@ -60,6 +65,7 @@
6065
http_response_code(500);
6166
exit;
6267
}
68+
6369
$json = [
6470
'y' => $question->fields['row'],
6571
'x' => $question->fields['col'],

inc/question.class.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,8 @@ private function checkBeforeSave($input) : array {
316316
return [];
317317
}
318318
// - field type is compatible with accessibility of the form
319-
$section = new PluginFormcreatorSection();
320-
$sectionFk = PluginFormcreatorSection::getForeignKeyField();
321-
if (isset($input[$sectionFk])) {
322-
$section->getFromDB($input[$sectionFk]);
323-
} else {
324-
$section->getFromDB($this->fields[$sectionFk]);
325-
}
326319
$form = new PluginFormcreatorForm();
327-
$form->getFromDBBySection($section);
320+
$form->getFromDBByQuestion($this);
328321
if ($form->isPublicAccess() && !$this->field->isAnonymousFormCompatible()) {
329322
Session::addMessageAfterRedirect(__('This type of question is not compatible with public forms.', 'formcreator'), false, ERROR);
330323
return [];
@@ -839,6 +832,17 @@ public function duplicate(array $options = []) {
839832

840833
$sectionFk = PluginFormcreatorSection::getForeignKeyField();
841834
$export = $this->export(true);
835+
836+
// Amend some data (used when duplicating a question from the form designer UI)
837+
if (isset($options['fields'])) {
838+
foreach ($options['fields'] as $key => $value) {
839+
if ($value === null) {
840+
unset($export[$key]);
841+
continue;
842+
}
843+
$export[$key] = $value;
844+
}
845+
}
842846
$newQuestionId = static::import($linker, $export, $this->fields[$sectionFk]);
843847

844848
if ($newQuestionId === false) {

0 commit comments

Comments
 (0)