From 04d3f368c24fc5e3ac2fbb5b46b8e2b2c8ccb9a0 Mon Sep 17 00:00:00 2001 From: Jake Bentvelzen Date: Tue, 8 Mar 2016 15:42:57 +1100 Subject: [PATCH] Added functionality so that GridFieldOrderableRows updates *_versions table for Versioned data objects. --- code/GridFieldOrderableRows.php | 40 +++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/code/GridFieldOrderableRows.php b/code/GridFieldOrderableRows.php index 78310ba..ee7bef9 100755 --- a/code/GridFieldOrderableRows.php +++ b/code/GridFieldOrderableRows.php @@ -339,17 +339,39 @@ class GridFieldOrderableRows extends RequestHandler implements $pool = array_values($values); sort($pool); + // If not a ManyManyList and using versioning, detect it. + $isVersioned = false; + $class = $list->dataClass(); + if ($class == $this->getSortTable($list)) { + $isVersioned = $class::has_extension('Versioned'); + } + // Loop through each item, and update the sort values which do not // match to order the objects. - foreach(array_values($order) as $pos => $id) { - if($values[$id] != $pool[$pos]) { - DB::query(sprintf( - 'UPDATE "%s" SET "%s" = %d WHERE %s', - $this->getSortTable($list), - $this->getSortField(), - $pool[$pos], - $this->getSortTableClauseForIds($list, $id) - )); + if (!$isVersioned) { + foreach(array_values($order) as $pos => $id) { + if($values[$id] != $pool[$pos]) { + DB::query(sprintf( + 'UPDATE "%s" SET "%s" = %d WHERE %s', + $this->getSortTable($list), + $this->getSortField(), + $pool[$pos], + $this->getSortTableClauseForIds($list, $id) + )); + } + } + } else { + // For versioned objects, modify them with the ORM so that the + // *_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]) { + $record = $class::get()->byID($id); + $record->$sortField = $pool[$pos]; + $record->write(); + } } }