diff --git a/inc/field.class.php b/inc/field.class.php index 8beb9362..48233d32 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -153,6 +153,30 @@ public static function installBaseData(Migration $migration, $version) $migration->addConfig(['stable_search_options' => 'yes'], 'plugin:fields'); } + // Normalize rankings to be contiguous (0-based) per container + if (Config::getConfigurationValue('plugin:fields', 'contiguous_rankings') !== 'yes') { + $containers = $DB->request([ + 'SELECT' => 'plugin_fields_containers_id', + 'DISTINCT' => true, + 'FROM' => $table, + ]); + foreach ($containers as $row) { + $cid = $row['plugin_fields_containers_id']; + $fields = $DB->request([ + 'SELECT' => 'id', + 'FROM' => $table, + 'WHERE' => ['plugin_fields_containers_id' => $cid], + 'ORDER' => 'ranking ASC', + ]); + $rank = 0; + foreach ($fields as $field) { + $DB->update($table, ['ranking' => $rank], ['id' => $field['id']]); + $rank++; + } + } + $migration->addConfig(['contiguous_rankings' => 'yes'], 'plugin:fields'); + } + return true; } @@ -653,7 +677,7 @@ public function showSummary($container) foreach ($iterator as $data) { if ($this->getFromDB($data['id'])) { - echo ""; + echo ""; echo ''; $label = empty($this->fields['label']) ? NOT_AVAILABLE : $this->fields['label']; diff --git a/public/js/drag-field-row.js b/public/js/drag-field-row.js index 167a2477..6b0b262b 100644 --- a/public/js/drag-field-row.js +++ b/public/js/drag-field-row.js @@ -41,16 +41,16 @@ redipsInit = function () { rd.event.rowDroppedBefore = function (sourceTable, sourceRowIndex) { var pos = rd.getPosition(); - var old_index = sourceRowIndex; - var new_index = pos[1]; - var container = document.getElementById('plugin_fields_containers_id').value; + var old_ranking = sourceTable.rows[sourceRowIndex].dataset.ranking; + var new_ranking = sourceTable.rows[pos[1]].dataset.ranking; + var container = document.getElementById('plugin_fields_containers_id').value; jQuery.ajax({ type: "POST", url: "../ajax/reorder.php", data: { - old_order: old_index, - new_order: new_index, + old_order: old_ranking, + new_order: new_ranking, container_id: container } })