diff --git a/inc/section.class.php b/inc/section.class.php index 1d64678a5..131d287d1 100644 --- a/inc/section.class.php +++ b/inc/section.class.php @@ -270,6 +270,8 @@ public function duplicate(array $options = []) { public function moveUp() { global $DB; + $this->fixOrder(); + $order = $this->fields['order']; $formId = $this->fields['plugin_formcreator_forms_id']; $otherItem = new static(); @@ -313,6 +315,8 @@ public function moveUp() { public function moveDown() { global $DB; + $this->fixOrder(); + $order = $this->fields['order']; $formId = $this->fields['plugin_formcreator_forms_id']; $otherItem = new static(); @@ -349,6 +353,33 @@ public function moveDown() { return $success; } + public function fixOrder(): void { + global $DB; + + $formId = $this->fields['plugin_formcreator_forms_id']; + + $iterator = $DB->request([ + 'FROM' => static::getTable(), + 'WHERE' => [ + 'AND' => [ + 'plugin_formcreator_forms_id' => $formId, + ] + ], + 'ORDER' => ['order ASC'] + ]); + $order = 1; + foreach ($iterator as $row) { + if ($row['order'] !== $order) { + $DB->update(static::getTable(), [ + 'order' => $order + ], [ + 'id' => $row['id'] + ]); + } + $order++; + } + } + public static function import(PluginFormcreatorLinker $linker, $input = [], $containerId = 0) { global $DB;