From e3895cc5efeb1ab76bfefb48fb3e8803ba8b38e6 Mon Sep 17 00:00:00 2001 From: UndefinedOffset Date: Tue, 22 Oct 2013 10:36:08 -0300 Subject: [PATCH] Fixed issue #48 where duplicate sort indexes would occur when appending to top instead of the bottom --- code/forms/GridFieldSortableRows.php | 18 +++++++++++++----- .../GridFieldSortableRowsAutoSortTest.php | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/code/forms/GridFieldSortableRows.php b/code/forms/GridFieldSortableRows.php index aaa883b..9f4aabc 100644 --- a/code/forms/GridFieldSortableRows.php +++ b/code/forms/GridFieldSortableRows.php @@ -205,13 +205,19 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP $idCondition='"ID" IN(\''.implode("','", $list->getIDList()).'\')'; } + if($this->append_to_top) { + $topIncremented=array(); + } + foreach($list as $obj) { if($many_many) { if($this->append_to_top) { - //Upgrade all the records (including the last iserted from 0 to 1) + //Upgrade all the records (including the last inserted from 0 to 1) DB::query('UPDATE "' . $table . '" SET "' . $sortColumn . '" = "' . $sortColumn .'"+1' - . ' WHERE "' . $parentField . '" = ' . $owner->ID); + . ' WHERE "' . $parentField . '" = ' . $owner->ID . (!empty($topIncremented) ? ' AND "' . $componentField . '" NOT IN(\''.implode('\',\'', $topIncremented).'\')':'')); + + $topIncremented[]=$obj->ID; }else { //Append the last record to the bottom DB::query('UPDATE "' . $table @@ -219,15 +225,17 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP . ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID); } }else if($this->append_to_top) { - //Upgrade all the records (including the last iserted from 0 to 1) + //Upgrade all the records (including the last inserted from 0 to 1) DB::query('UPDATE "' . $table . '" SET "' . $sortColumn . '" = "' . $sortColumn .'"+1' - . ' WHERE '.($list instanceof RelationList ? '"' . $list->foreignKey . '" = '. $owner->ID:$idCondition)); + . ' WHERE '.($list instanceof RelationList ? '"' . $list->foreignKey . '" = '. $owner->ID:$idCondition) . (!empty($topIncremented) ? ' AND "ID" NOT IN(\''.implode('\',\'', $topIncremented).'\')':'')); //LastEdited DB::query('UPDATE "' . $baseDataClass . '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\'' - . ' WHERE '.($list instanceof RelationList ? '"' . $list->foreignKey . '" = '. $owner->ID:$idCondition)); + . ' WHERE '.($list instanceof RelationList ? '"' . $list->foreignKey . '" = '. $owner->ID:$idCondition) . (!empty($topIncremented) ? ' AND "ID" NOT IN(\''.implode('\',\'', $topIncremented).'\')':'')); + + $topIncremented[]=$obj->ID; }else { //Append the last record to the bottom DB::query('UPDATE "' . $table diff --git a/tests/forms/GridFieldSortableRowsAutoSortTest.php b/tests/forms/GridFieldSortableRowsAutoSortTest.php index c373a73..ecf06d7 100644 --- a/tests/forms/GridFieldSortableRowsAutoSortTest.php +++ b/tests/forms/GridFieldSortableRowsAutoSortTest.php @@ -31,7 +31,15 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest { Session::set($stateID, array('grid'=>'', 'actionName'=>'sortableRowsToggle', 'args'=>array('GridFieldSortableRows'=>array('sortableToggle'=>true)))); $request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true)); $this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request); + + //Insure sort ran $this->assertEquals(3, $this->list->last()->SortOrder, 'Auto sort should have run'); + + + //Check for duplicates (there shouldn't be any) + $count=$this->list->Count(); + $indexes=count(array_unique($this->list->column('SortOrder'))); + $this->assertEquals(0, $count-$indexes, 'Duplicate indexes detected'); } public function testAppendToTopAutoSort() { @@ -45,7 +53,15 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest { Session::set($stateID, array('grid'=>'', 'actionName'=>'sortableRowsToggle', 'args'=>array('GridFieldSortableRows'=>array('sortableToggle'=>true)))); $request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true)); $this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request); + + //Insure sort ran $this->assertEquals(3, $this->list->last()->SortOrder, 'Auto sort should have run'); + + + //Check for duplicates (there shouldn't be any) + $count=$this->list->Count(); + $indexes=count(array_unique($this->list->column('SortOrder'))); + $this->assertEquals(0, $count-$indexes, 'Duplicate indexes detected'); } }