mirror of
https://github.com/UndefinedOffset/SortableGridField.git
synced 2024-10-22 17:05:38 +02:00
Made adjustments to make the changes more flexible
Fixed issue causing SortableGridField to not work on ModelAdmin lists
This commit is contained in:
parent
6ca5bf9a9d
commit
eb1e736805
@ -1,5 +0,0 @@
|
||||
---
|
||||
Name: gridfieldsortablerows
|
||||
---
|
||||
GridFieldSortableRows:
|
||||
append_to_top: true
|
@ -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,11 +200,14 @@ 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) {
|
||||
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'
|
||||
@ -204,24 +218,17 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
||||
. '" SET "' . $sortColumn .'" = ' . ($max + $i)
|
||||
. ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
|
||||
}
|
||||
} else {
|
||||
|
||||
if ($append_to_top) {
|
||||
/*
|
||||
* Append to top
|
||||
*/
|
||||
}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->foreignKey . '" = '. $owner->ID);
|
||||
. ' 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->foreignKey . '" = '. $owner->ID);
|
||||
. ' WHERE '.($list instanceof RelationList ? '"' . $list->foreignKey . '" = '. $owner->ID:$idCondition));
|
||||
}else {
|
||||
/*
|
||||
* Append to bottom
|
||||
*/
|
||||
//Append the last record to the bottom
|
||||
DB::query('UPDATE "' . $table
|
||||
. '" SET "' . $sortColumn . '" = ' . ($max + $i)
|
||||
@ -231,7 +238,6 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
|
||||
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
|
||||
. ' WHERE "ID" = '. $obj->ID);
|
||||
}
|
||||
}
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user