From fedf621dd50bc62a05dd8c9d816ce2908f00af6b Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Wed, 14 Oct 2020 15:05:03 +0200 Subject: [PATCH] fix(section,question): don't force ordering on import questins and sections may be shuffled on import. Don't overwrite order property Signed-off-by: Thierry Bugier --- inc/exportabletrait.class.php | 3 +++ inc/form.class.php | 10 ---------- inc/question.class.php | 17 ++++++++++------- inc/section.class.php | 19 +++++++++++-------- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/inc/exportabletrait.class.php b/inc/exportabletrait.class.php index 350d56041..c16b13ed8 100644 --- a/inc/exportabletrait.class.php +++ b/inc/exportabletrait.class.php @@ -35,6 +35,9 @@ trait PluginFormcreatorExportableTrait { + + protected $useAutomaticOrdering = true; + /** * Insert the export of sub items in the export * diff --git a/inc/form.class.php b/inc/form.class.php index ae9bbc4d0..af53d7a80 100644 --- a/inc/form.class.php +++ b/inc/form.class.php @@ -1966,16 +1966,6 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con // add the form to the linker $linker->addObject($originalId, $item); - // sort sections - if (isset($input['_sections']) && is_array($input['_sections'])) { - usort($input['_sections'], function($a, $b) { - if ($a['order'] == $b['order']) { - return 0; - } - return ($a['order'] < $b['order']) ? -1 : 1; - }); - } - $subItems = [ '_profiles' => PluginFormcreatorForm_Profile::class, '_sections' => PluginFormcreatorSection::class, diff --git a/inc/question.class.php b/inc/question.class.php index 8bee32191..55f9e6dcc 100644 --- a/inc/question.class.php +++ b/inc/question.class.php @@ -379,14 +379,16 @@ public function prepareInputForAdd($input) { } $sectionFk = PluginFormcreatorSection::getForeignKeyField(); // Get next row + if ($this->useAutomaticOrdering) { $sectionFk = PluginFormcreatorSection::getForeignKeyField(); - $maxRow = PluginFormcreatorCommon::getMax($this, [ - $sectionFk => $input[$sectionFk] - ], 'row'); - if ($maxRow === null) { - $input['row'] = 0; - } else { - $input['row'] = $maxRow + 1; + $maxRow = PluginFormcreatorCommon::getMax($this, [ + $sectionFk => $input[$sectionFk] + ], 'row'); + if ($maxRow === null) { + $input['row'] = 0; + } else { + $input['row'] = $maxRow + 1; + } } // generate a unique id @@ -915,6 +917,7 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con ); $item->update($input); } else { + $item->useAutomaticOrdering = false; unset($input['id']); $itemId = $item->add($input); } diff --git a/inc/section.class.php b/inc/section.class.php index 942ce754c..8dcfdadcf 100644 --- a/inc/section.class.php +++ b/inc/section.class.php @@ -85,14 +85,16 @@ public function prepareInputForAdd($input) { } // Get next order - $formId = $input['plugin_formcreator_forms_id']; - $maxOrder = PluginFormcreatorCommon::getMax($this, [ - "plugin_formcreator_forms_id" => $formId - ], 'order'); - if ($maxOrder === null) { - $input['order'] = 1; - } else { - $input['order'] = $maxOrder + 1; + if ($this->useAutomaticOrdering) { + $formId = $input['plugin_formcreator_forms_id']; + $maxOrder = PluginFormcreatorCommon::getMax($this, [ + "plugin_formcreator_forms_id" => $formId + ], 'order'); + if ($maxOrder === null) { + $input['order'] = 1; + } else { + $input['order'] = $maxOrder + 1; + } } return $input; @@ -297,6 +299,7 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con $item->update($input); } else { unset($input['id']); + $item->useAutomaticOrdering = false; $itemId = $item->add($input); } if ($itemId === false) {