Made adjustments to make the changes more flexible

Fixed issue causing SortableGridField to not work on ModelAdmin lists
This commit is contained in:
Ed 2013-10-19 14:34:45 -03:00
parent 6ca5bf9a9d
commit eb1e736805
3 changed files with 48 additions and 33 deletions

View File

@ -1,5 +0,0 @@
---
Name: gridfieldsortablerows
---
GridFieldSortableRows:
append_to_top: true

View File

@ -6,6 +6,7 @@
*/
class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator {
protected $sortColumn;
protected $append_to_top=false;
/**
* @param String $sortColumn Column that should be used to update the sort information
@ -109,6 +110,16 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
return $dataList->sort($this->sortColumn);
}
/**
* Sets if new records should be appended to the top or the bottom of the list
* @param bool $value Boolean true to append to the top false to append to the bottom
* @return GridFieldSortableRows Returns the current instance
*/
public function setAppendToTop($value) {
$this->append_to_top=$value;
return $this;
}
/**
* Detects and corrects items with a sort column value of 0, by appending them to the bottom of the list
* @param GridField $gridField Grid Field Reference
@ -189,48 +200,43 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
DB::getConn()->transactionStart();
}
$append_to_top = Config::inst()->get('GridFieldSortableRows', 'append_to_top');
$idCondition=null;
if($this->append_to_top && !($list instanceof RelationList)) {
$idCondition='"ID" IN(\''.implode("','", $list->getIDList()).'\')';
}
foreach($list as $obj) {
if($many_many) {
if ($append_to_top) {
// Upgrade all the records (including the last iserted from 0 to 1)
if($this->append_to_top) {
//Upgrade all the records (including the last iserted from 0 to 1)
DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = "' . $sortColumn .'"+1'
. ' WHERE "' . $parentField . '" = ' . $owner->ID);
} else {
// Append the last record to the bottom
}else {
//Append the last record to the bottom
DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn .'" = ' . ($max + $i)
. ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
}
} else {
if ($append_to_top) {
/*
* Append to top
*/
// Upgrade all the records (including the last iserted from 0 to 1)
DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = "' . $sortColumn .'"+1'
. ' WHERE "' . $list->foreignKey . '" = '. $owner->ID);
// LastEdited
DB::query('UPDATE "' . $baseDataClass
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
. ' WHERE "' . $list->foreignKey . '" = '. $owner->ID);
} else {
/*
* Append to bottom
*/
// Append the last record to the bottom
DB::query('UPDATE "' . $table
}else if($this->append_to_top) {
//Upgrade all the records (including the last iserted from 0 to 1)
DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = "' . $sortColumn .'"+1'
. ' WHERE '.($list instanceof RelationList ? '"' . $list->foreignKey . '" = '. $owner->ID:$idCondition));
//LastEdited
DB::query('UPDATE "' . $baseDataClass
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
. ' WHERE '.($list instanceof RelationList ? '"' . $list->foreignKey . '" = '. $owner->ID:$idCondition));
}else {
//Append the last record to the bottom
DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = ' . ($max + $i)
. ' WHERE "ID" = '. $obj->ID);
// LastEdited
DB::query('UPDATE "' . $baseDataClass
//LastEdited
DB::query('UPDATE "' . $baseDataClass
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
. ' WHERE "ID" = '. $obj->ID);
}
}
$i++;

View File

@ -33,6 +33,20 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest {
$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
$this->assertEquals(3, $this->list->last()->SortOrder, 'Auto sort should have run');
}
public function testAppendToTopAutoSort() {
if(Member::currentUser()) { Member::currentUser()->logOut(); }
$this->gridField->getConfig()->getComponentByType('GridFieldSortableRows')->setAppendToTop(true);
$this->assertEquals(0, $this->list->last()->SortOrder, 'Auto sort should not have run');
$stateID = 'testGridStateActionField';
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);
$this->assertEquals(3, $this->list->last()->SortOrder, 'Auto sort should have run');
}
}
class GridFieldAction_SortOrder_Player extends DataObject implements TestOnly {