From 1d2b20c87f792ca6b55bd1aaa01efda92a98b3f4 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Tue, 26 Jul 2016 13:02:41 +0100 Subject: [PATCH] FIX Correct reorder of inconsistent sort values --- code/GridFieldOrderableRows.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/code/GridFieldOrderableRows.php b/code/GridFieldOrderableRows.php index 862c2ae8..1fd50483 100755 --- a/code/GridFieldOrderableRows.php +++ b/code/GridFieldOrderableRows.php @@ -413,9 +413,13 @@ protected function executeReorder(GridField $grid, $ids) { } protected function reorderItems($list, array $values, array $order) { - // Get a list of sort values that can be used. - $pool = array_values($values); - sort($pool); + $sortField = $this->getSortField(); + /** @var SS_List $map */ + $map = $list->map('ID', $sortField); + //fix for versions of SS that return inconsistent types for `map` function + if ($map instanceof SS_Map) { + $map = $map->toArray(); + } // If not a ManyManyList and using versioning, detect it. $isVersioned = false; @@ -427,14 +431,15 @@ protected function reorderItems($list, array $values, array $order) { // Loop through each item, and update the sort values which do not // match to order the objects. if (!$isVersioned) { + $sortTable = $this->getSortTable($list); $additionalSQL = (!$list instanceof ManyManyList) ? ', "LastEdited" = NOW()' : ''; foreach(array_values($order) as $pos => $id) { - if($values[$id] != $pool[$pos]) { + if($map[$id] != $pos) { DB::query(sprintf( 'UPDATE "%s" SET "%s" = %d%s WHERE %s', - $this->getSortTable($list), - $this->getSortField(), - $pool[$pos], + $sortTable, + $sortField, + $pos, $additionalSQL, $this->getSortTableClauseForIds($list, $id) )); @@ -445,11 +450,10 @@ protected function reorderItems($list, array $values, array $order) { // *_versions table is updated. This ensures re-ordering works // similar to the SiteTree where you change the position, and then // you go into the record and publish it. - $sortField = $this->getSortField(); foreach(array_values($order) as $pos => $id) { - if($values[$id] != $pool[$pos]) { + if($map[$id] != $pos) { $record = $class::get()->byID($id); - $record->$sortField = $pool[$pos]; + $record->$sortField = $pos; $record->write(); } }