Fixed issue #48 where duplicate sort indexes would occur when appending to top instead of the bottom

This commit is contained in:
UndefinedOffset 2013-10-22 10:36:08 -03:00
parent 15ead2ae94
commit e3895cc5ef
2 changed files with 29 additions and 5 deletions

View File

@ -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

View File

@ -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');
}
}