FIX Correct reorder of inconsistent sort values

This commit is contained in:
Daniel Hensby 2016-07-26 13:02:41 +01:00
parent 66c09cd60d
commit 1d2b20c87f
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E

View File

@ -413,9 +413,13 @@ class GridFieldOrderableRows extends RequestHandler implements
} }
protected function reorderItems($list, array $values, array $order) { protected function reorderItems($list, array $values, array $order) {
// Get a list of sort values that can be used. $sortField = $this->getSortField();
$pool = array_values($values); /** @var SS_List $map */
sort($pool); $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. // If not a ManyManyList and using versioning, detect it.
$isVersioned = false; $isVersioned = false;
@ -427,14 +431,15 @@ class GridFieldOrderableRows extends RequestHandler implements
// Loop through each item, and update the sort values which do not // Loop through each item, and update the sort values which do not
// match to order the objects. // match to order the objects.
if (!$isVersioned) { if (!$isVersioned) {
$sortTable = $this->getSortTable($list);
$additionalSQL = (!$list instanceof ManyManyList) ? ', "LastEdited" = NOW()' : ''; $additionalSQL = (!$list instanceof ManyManyList) ? ', "LastEdited" = NOW()' : '';
foreach(array_values($order) as $pos => $id) { foreach(array_values($order) as $pos => $id) {
if($values[$id] != $pool[$pos]) { if($map[$id] != $pos) {
DB::query(sprintf( DB::query(sprintf(
'UPDATE "%s" SET "%s" = %d%s WHERE %s', 'UPDATE "%s" SET "%s" = %d%s WHERE %s',
$this->getSortTable($list), $sortTable,
$this->getSortField(), $sortField,
$pool[$pos], $pos,
$additionalSQL, $additionalSQL,
$this->getSortTableClauseForIds($list, $id) $this->getSortTableClauseForIds($list, $id)
)); ));
@ -445,11 +450,10 @@ class GridFieldOrderableRows extends RequestHandler implements
// *_versions table is updated. This ensures re-ordering works // *_versions table is updated. This ensures re-ordering works
// similar to the SiteTree where you change the position, and then // similar to the SiteTree where you change the position, and then
// you go into the record and publish it. // you go into the record and publish it.
$sortField = $this->getSortField();
foreach(array_values($order) as $pos => $id) { foreach(array_values($order) as $pos => $id) {
if($values[$id] != $pool[$pos]) { if($map[$id] != $pos) {
$record = $class::get()->byID($id); $record = $class::get()->byID($id);
$record->$sortField = $pool[$pos]; $record->$sortField = $pos;
$record->write(); $record->write();
} }
} }