diff --git a/_config/gridfieldsortablerows.yml b/_config/gridfieldsortablerows.yml deleted file mode 100644 index 11e19f4..0000000 --- a/_config/gridfieldsortablerows.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -Name: gridfieldsortablerows ---- -GridFieldSortableRows: - append_to_top: true \ No newline at end of file diff --git a/code/forms/GridFieldSortableRows.php b/code/forms/GridFieldSortableRows.php index f09bfbf..aaa883b 100644 --- a/code/forms/GridFieldSortableRows.php +++ b/code/forms/GridFieldSortableRows.php @@ -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++; diff --git a/tests/forms/GridFieldSortableRowsAutoSortTest.php b/tests/forms/GridFieldSortableRowsAutoSortTest.php index 0c98407..c373a73 100644 --- a/tests/forms/GridFieldSortableRowsAutoSortTest.php +++ b/tests/forms/GridFieldSortableRowsAutoSortTest.php @@ -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 {