Merge remote-tracking branch 'UndefinedOffset/master'

This commit is contained in:
Tomáš Bílek 2013-02-13 00:00:51 +01:00
commit 723382b752
3 changed files with 89 additions and 22 deletions

View File

@ -7,7 +7,7 @@ Adds drag and drop functionality to SilverStripe 3's GridField
* SilverStripe 3.x
## Installation
* Download the module from here https://github.com/UndefinedOffset/SortableGridField/downloads
* Download the module from here https://github.com/UndefinedOffset/SortableGridField/archive/master.zip
* Extract the downloaded archive into your site root so that the destination folder is called SortableGridField, opening the extracted folder should contain _config.php in the root along with other files/folders
* Run dev/build?flush=all to regenerate the manifest
* Upon entering the cms and using GridFieldSortableRows component for the first time you make need to add ?flush=all to the end of the address to force the templates to regenerate

View File

@ -20,6 +20,12 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
* @return Array Map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
*/
public function getHTMLFragments($gridField) {
$dataList = $gridField->getList();
if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
return array();
}
$state = $gridField->State->GridFieldSortableRows;
if(!is_bool($state->sortableToggle)) {
$state->sortableToggle = false;
@ -104,7 +110,6 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
$gridField->getConfig()->removeComponentsByType('GridFieldSortableHeader');
}
return $dataList->sort($this->sortColumn);
}
@ -114,6 +119,10 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
* @param SS_List $dataList Data List of items to be checked
*/
protected function fixSortColumn($gridField, SS_List $dataList) {
if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
return;
}
$list=clone $dataList;
$list=$list->alterDataQuery(function($query, SS_List $tmplist) {
$query->limit(array());
@ -132,13 +141,8 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
$max = $list->Max($this->sortColumn);
if($list->where('"'.$this->sortColumn.'"=0')->Count()>0) {
//Start transaction if supported
if(DB::getConn()->supportsTransactions()) {
DB::getConn()->transactionStart();
}
$list=$list->where('"'.$this->sortColumn.'"=0');
if($list->Count()>0) {
$owner = $gridField->Form->getRecord();
$sortColumn = $this->sortColumn;
$i = 1;
@ -151,18 +155,47 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
user_error('Sort column '.$this->sortColumn.' must be an Int, column is of type '.$fieldType, E_USER_ERROR);
exit;
}
}else {
//Find table containing the sort column
$table=false;
$class=$gridField->getModelClass();
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
if(!empty($db) && array_key_exists($sortColumn, $db)) {
$table=$class;
}else {
$classes=ClassInfo::ancestry($class, true);
foreach($classes as $class) {
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
if(!empty($db) && array_key_exists($sortColumn, $db)) {
$table=$class;
break;
}
}
}
if($table===false) {
user_error('Sort column '.$this->sortColumn.' could not be found in '.$gridField->getModelClass().'\'s ancestry', E_USER_ERROR);
exit;
}
}
//Start transaction if supported
if(DB::getConn()->supportsTransactions()) {
DB::getConn()->transactionStart();
}
//@TODO Need to optimize this to eliminate some of the resource load could use raw queries to be more efficient
foreach($list as $obj) {
if($many_many) {
DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn.'" = ' . ($max + $i)
. '" SET "' . $sortColumn .'" = ' . ($max + $i)
. ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
}else {
$obj->$sortColumn = ($max + $i);
$obj->write();
DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = ' . ($max + $i)
. ' WHERE "ID" = '. $obj->ID);
}
$i++;
@ -215,6 +248,13 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
* @param Array $data Data submitted in the request
*/
protected function saveGridRowSort(GridField $gridField, $data) {
$dataList = $gridField->getList();
if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
user_error('Cannot sort an UnsavedRelationList', E_USER_ERROR);
return;
}
if(!singleton($gridField->getModelClass())->canEdit()){
throw new ValidationException(_t('GridFieldSortableRows.EditPermissionsFailure', "No edit permissions"),0);
}
@ -241,6 +281,28 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
if ($many_many) {
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName());
}else {
//Find table containing the sort column
$table=false;
$class=$gridField->getModelClass();
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
if(!empty($db) && array_key_exists($sortColumn, $db)) {
$table=$class;
}else {
$classes=ClassInfo::ancestry($class, true);
foreach($classes as $class) {
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
if(!empty($db) && array_key_exists($sortColumn, $db)) {
$table=$class;
break;
}
}
}
if($table===false) {
user_error('Sort column '.$this->sortColumn.' could not be found in '.$gridField->getModelClass().'\'s ancestry', E_USER_ERROR);
exit;
}
}
@ -250,7 +312,6 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
}
//@TODO Need to optimize this to eliminate some of the resource load could use raw queries to be more efficient
$ids = explode(',', $data['ItemIDs']);
for($sort = 0;$sort<count($ids);$sort++) {
$id = intval($ids[$sort]);
@ -259,9 +320,9 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
. '" SET "' . $sortColumn.'" = ' . (($sort + 1) + $pageOffset)
. ' WHERE "' . $componentField . '" = ' . $id . ' AND "' . $parentField . '" = ' . $owner->ID);
} else {
$obj = $items->byID($ids[$sort]);
$obj->$sortColumn = ($sort + 1) + $pageOffset;
$obj->write();
DB::query('UPDATE "' . $table
. '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset)
. ' WHERE "ID" = '. $id);
}
}
@ -396,4 +457,4 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
}
}
}
?>
?>

View File

@ -27,21 +27,27 @@ class GridFieldSortableRowsTest extends SapphireTest {
public function testSortActionWithoutCorrectPermission() {
if(Member::currentUser()) { Member::currentUser()->logOut(); }
$this->setExpectedException('ValidationException');
$team1 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team1');
$team2 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team2');
$team3 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team3');
$stateID = 'testGridStateActionField';
Session::set($stateID, array('grid'=>'', 'actionName'=>'saveGridRowSort', 'args'=>array('GridFieldSortableRows'=>array('sortableToggle'=>true))));
$request = new SS_HTTPRequest('POST', 'url', array('Items'=>'1,3,2'), array('action_gridFieldAlterAction?StateID='.$stateID=>true));
$request = new SS_HTTPRequest('POST', 'url', array('ItemIDs'=>"$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID='.$stateID=>true));
$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
$this->assertEquals(3, $this->list->last()->ID, 'User should\'t be able to sort records without correct permissions.');
$this->assertEquals($team3->ID, $this->list->last()->ID, 'User should\'t be able to sort records without correct permissions.');
}
public function testSortActionWithAdminPermission() {
$team1 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team1');
$team2 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team2');
$team3 = $this->objFromFixture('GridFieldAction_SortOrder_Team', 'team3');
$this->logInWithPermission('ADMIN');
$stateID = 'testGridStateActionField';
Session::set($stateID, array('grid'=>'', 'actionName'=>'saveGridRowSort', 'args'=>array('GridFieldSortableRows'=>array('sortableToggle'=>true))));
$request = new SS_HTTPRequest('POST', 'url', array('Items'=>'1,3,2'), array('action_gridFieldAlterAction?StateID='.$stateID=>true));
$request = new SS_HTTPRequest('POST', 'url', array('ItemIDs'=>"$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID='.$stateID=>true));
$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
$this->assertEquals(2, $this->list->last()->ID, 'User should be able to sort records with ADMIN permission.');
$this->assertEquals($team2->ID, $this->list->last()->ID, 'User should be able to sort records with ADMIN permission.');
}
}