2012-05-09 19:14:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-05-15 16:04:40 +02:00
|
|
|
* This component provides a checkbox which when checked enables drag-and-drop re-ordering of elements displayed in a {@link GridField}
|
|
|
|
*
|
2012-05-09 19:14:34 +02:00
|
|
|
* @package forms
|
|
|
|
*/
|
2012-05-10 16:27:01 +02:00
|
|
|
class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator {
|
2012-05-15 16:04:40 +02:00
|
|
|
protected $sortColumn;
|
|
|
|
|
2012-05-10 16:27:01 +02:00
|
|
|
/**
|
2012-05-15 16:04:40 +02:00
|
|
|
* @param String $sortColumn Column that should be used to update the sort information
|
2012-05-10 16:27:01 +02:00
|
|
|
*/
|
|
|
|
public function __construct($sortColumn) {
|
2012-05-15 16:04:40 +02:00
|
|
|
$this->sortColumn = $sortColumn;
|
2012-05-10 16:27:01 +02:00
|
|
|
}
|
2012-05-15 16:04:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a map where the keys are fragment names and the values are pieces of HTML to add to these fragments.
|
|
|
|
* @param GridField $gridField Grid Field Reference
|
|
|
|
* @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) {
|
2013-02-12 03:19:25 +01:00
|
|
|
$dataList = $gridField->getList();
|
|
|
|
|
2013-02-09 00:47:34 +01:00
|
|
|
if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:04:40 +02:00
|
|
|
$state = $gridField->State->GridFieldSortableRows;
|
|
|
|
if(!is_bool($state->sortableToggle)) {
|
|
|
|
$state->sortableToggle = false;
|
|
|
|
}
|
|
|
|
|
2012-05-15 17:33:00 +02:00
|
|
|
//Ensure user can edit
|
2012-07-18 22:16:56 +02:00
|
|
|
if(!singleton($gridField->getModelClass())->canEdit()){
|
2012-05-15 17:33:00 +02:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:04:40 +02:00
|
|
|
|
|
|
|
//Sort order toggle
|
2013-07-19 01:54:33 +02:00
|
|
|
$sortOrderToggle = GridField_FormAction::create(
|
2012-05-15 16:04:40 +02:00
|
|
|
$gridField,
|
|
|
|
'sortablerows-toggle',
|
2013-07-19 01:54:33 +02:00
|
|
|
'sorttoggle',
|
|
|
|
'sortableRowsToggle',
|
2012-05-15 16:04:40 +02:00
|
|
|
null
|
|
|
|
)->addExtraClass('sortablerows-toggle');
|
|
|
|
|
|
|
|
|
2013-07-19 01:54:33 +02:00
|
|
|
$sortOrderSave = GridField_FormAction::create(
|
2012-05-15 16:04:40 +02:00
|
|
|
$gridField,
|
2013-07-19 01:54:33 +02:00
|
|
|
'sortablerows-savesort',
|
|
|
|
'savesort',
|
|
|
|
'saveGridRowSort',
|
2012-05-15 16:04:40 +02:00
|
|
|
null
|
2013-07-19 01:54:33 +02:00
|
|
|
)->addExtraClass('sortablerows-savesort');
|
2012-05-15 16:04:40 +02:00
|
|
|
|
|
|
|
|
2013-07-19 01:54:33 +02:00
|
|
|
//Sort to Page Action
|
|
|
|
$sortToPage = GridField_FormAction::create(
|
2012-05-17 03:37:08 +02:00
|
|
|
$gridField,
|
|
|
|
'sortablerows-sorttopage',
|
2013-07-19 01:54:33 +02:00
|
|
|
'sorttopage',
|
2012-05-17 03:37:08 +02:00
|
|
|
'sortToPage',
|
|
|
|
null
|
|
|
|
)->addExtraClass('sortablerows-sorttopage');
|
|
|
|
|
|
|
|
|
2012-05-15 16:04:40 +02:00
|
|
|
$data = array('SortableToggle' => $sortOrderToggle,
|
2013-07-19 01:54:33 +02:00
|
|
|
'SortOrderSave' => $sortOrderSave,
|
2012-05-17 03:37:08 +02:00
|
|
|
'SortToPage' => $sortToPage,
|
2012-05-15 16:04:40 +02:00
|
|
|
'Checked' => ($state->sortableToggle == true ? ' checked = "checked"':''));
|
|
|
|
|
|
|
|
$forTemplate = new ArrayData($data);
|
|
|
|
|
|
|
|
|
|
|
|
//Inject Requirements
|
2012-10-01 02:49:44 +02:00
|
|
|
Requirements::css(SORTABLE_GRIDFIELD_BASE . '/css/GridFieldSortableRows.css');
|
|
|
|
Requirements::javascript(SORTABLE_GRIDFIELD_BASE . '/javascript/GridFieldSortableRows.js');
|
2012-05-15 16:04:40 +02:00
|
|
|
|
|
|
|
|
2012-06-01 00:50:33 +02:00
|
|
|
$args = array('Colspan' => count($gridField->getColumns()), 'ID' => $gridField->ID());
|
2012-05-15 16:04:40 +02:00
|
|
|
|
|
|
|
return array('header' => $forTemplate->renderWith('GridFieldSortableRows', $args));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-10 16:27:01 +02:00
|
|
|
* Manipulate the datalist as needed by this grid modifier.
|
2012-05-15 16:04:40 +02:00
|
|
|
* @param GridField $gridField Grid Field Reference
|
|
|
|
* @param SS_List $dataList Data List to adjust
|
|
|
|
* @return DataList Modified Data List
|
2012-05-10 16:27:01 +02:00
|
|
|
*/
|
2012-05-15 16:04:40 +02:00
|
|
|
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
|
2012-08-10 17:50:14 +02:00
|
|
|
//Detect and correct items with a sort column value of 0 (push to bottom)
|
|
|
|
$this->fixSortColumn($gridField, $dataList);
|
2012-08-10 17:52:39 +02:00
|
|
|
|
|
|
|
|
2012-05-17 21:43:29 +02:00
|
|
|
$headerState = $gridField->State->GridFieldSortableHeader;
|
2012-05-18 03:05:56 +02:00
|
|
|
$state = $gridField->State->GridFieldSortableRows;
|
2012-05-17 21:43:29 +02:00
|
|
|
if ((!is_bool($state->sortableToggle) || $state->sortableToggle==false) && $headerState && !empty($headerState->SortColumn)) {
|
2012-05-10 16:42:52 +02:00
|
|
|
return $dataList;
|
|
|
|
}
|
2012-05-18 03:05:56 +02:00
|
|
|
|
|
|
|
if ($state->sortableToggle == true) {
|
2012-05-17 21:43:29 +02:00
|
|
|
$gridField->getConfig()->removeComponentsByType('GridFieldFilterHeader');
|
|
|
|
$gridField->getConfig()->removeComponentsByType('GridFieldSortableHeader');
|
|
|
|
}
|
2012-05-15 16:04:40 +02:00
|
|
|
|
|
|
|
return $dataList->sort($this->sortColumn);
|
|
|
|
}
|
|
|
|
|
2012-05-18 03:05:56 +02:00
|
|
|
/**
|
|
|
|
* Detects and corrects items with a sort column value of 0, by appending them to the bottom of the list
|
2012-05-19 02:04:34 +02:00
|
|
|
* @param GridField $gridField Grid Field Reference
|
2012-05-18 03:05:56 +02:00
|
|
|
* @param SS_List $dataList Data List of items to be checked
|
|
|
|
*/
|
2012-05-19 02:04:34 +02:00
|
|
|
protected function fixSortColumn($gridField, SS_List $dataList) {
|
2013-02-09 00:47:34 +01:00
|
|
|
if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-18 03:05:56 +02:00
|
|
|
$list=clone $dataList;
|
2012-12-28 19:35:06 +01:00
|
|
|
$list=$list->alterDataQuery(function($query, SS_List $tmplist) {
|
2012-12-30 20:11:33 +01:00
|
|
|
$query->limit(array());
|
|
|
|
return $query;
|
|
|
|
});
|
2012-10-12 14:36:45 +02:00
|
|
|
|
|
|
|
$many_many = ($list instanceof ManyManyList);
|
|
|
|
if (!$many_many) {
|
|
|
|
$sng=singleton($gridField->getModelClass());
|
|
|
|
$fieldType=$sng->db($this->sortColumn);
|
|
|
|
if(!$fieldType || !($fieldType=='Int' || is_subclass_of('Int', $fieldType))) {
|
2013-03-08 19:15:22 +01:00
|
|
|
if(is_array($fieldType)) {
|
|
|
|
user_error('Sort column '.$this->sortColumn.' could not be found in '.$gridField->getModelClass().'\'s ancestry', E_USER_ERROR);
|
|
|
|
}else {
|
|
|
|
user_error('Sort column '.$this->sortColumn.' must be an Int, column is of type '.$fieldType, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2012-10-12 14:36:45 +02:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-18 03:05:56 +02:00
|
|
|
$max = $list->Max($this->sortColumn);
|
2013-02-07 00:44:43 +01:00
|
|
|
$list=$list->where('"'.$this->sortColumn.'"=0');
|
2013-02-07 00:38:20 +01:00
|
|
|
if($list->Count()>0) {
|
2012-05-18 03:05:56 +02:00
|
|
|
$owner = $gridField->Form->getRecord();
|
|
|
|
$sortColumn = $this->sortColumn;
|
|
|
|
$i = 1;
|
|
|
|
|
2012-10-12 14:36:45 +02:00
|
|
|
if ($many_many) {
|
|
|
|
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName());
|
|
|
|
$extraFields=$owner->many_many_extraFields($gridField->getName());
|
|
|
|
|
|
|
|
if(!$extraFields || !array_key_exists($this->sortColumn, $extraFields) || !($extraFields[$this->sortColumn]=='Int' || is_subclass_of('Int', $extraFields[$this->sortColumn]))) {
|
|
|
|
user_error('Sort column '.$this->sortColumn.' must be an Int, column is of type '.$fieldType, E_USER_ERROR);
|
|
|
|
exit;
|
|
|
|
}
|
2013-01-24 01:11:22 +01:00
|
|
|
}else {
|
|
|
|
//Find table containing the sort column
|
|
|
|
$table=false;
|
|
|
|
$class=$gridField->getModelClass();
|
|
|
|
|
2013-01-31 19:03:33 +01:00
|
|
|
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
|
|
|
if(!empty($db) && array_key_exists($sortColumn, $db)) {
|
2013-01-24 01:11:22 +01:00
|
|
|
$table=$class;
|
|
|
|
}else {
|
|
|
|
$classes=ClassInfo::ancestry($class, true);
|
|
|
|
foreach($classes as $class) {
|
2013-01-31 19:03:33 +01:00
|
|
|
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
|
|
|
if(!empty($db) && array_key_exists($sortColumn, $db)) {
|
2013-01-24 01:11:22 +01:00
|
|
|
$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;
|
|
|
|
}
|
2013-05-26 01:42:35 +02:00
|
|
|
|
|
|
|
$baseDataClass=ClassInfo::baseDataClass($gridField->getModelClass());
|
2013-01-24 01:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Start transaction if supported
|
|
|
|
if(DB::getConn()->supportsTransactions()) {
|
|
|
|
DB::getConn()->transactionStart();
|
2012-10-12 14:36:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-18 03:05:56 +02:00
|
|
|
foreach($list as $obj) {
|
|
|
|
if($many_many) {
|
|
|
|
DB::query('UPDATE "' . $table
|
2013-01-24 01:11:22 +01:00
|
|
|
. '" SET "' . $sortColumn .'" = ' . ($max + $i)
|
2012-05-18 03:05:56 +02:00
|
|
|
. ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
|
|
|
|
}else {
|
2013-01-24 01:11:22 +01:00
|
|
|
DB::query('UPDATE "' . $table
|
2013-05-25 18:35:55 +02:00
|
|
|
. '" SET "' . $sortColumn . '" = ' . ($max + $i)
|
|
|
|
. ' WHERE "ID" = '. $obj->ID);
|
|
|
|
|
2013-05-26 01:42:35 +02:00
|
|
|
DB::query('UPDATE "' . $baseDataClass
|
2013-05-25 18:35:55 +02:00
|
|
|
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
|
2013-01-24 01:11:22 +01:00
|
|
|
. ' WHERE "ID" = '. $obj->ID);
|
2012-05-18 03:05:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//End transaction if supported
|
|
|
|
if(DB::getConn()->supportsTransactions()) {
|
|
|
|
DB::getConn()->transactionEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:04:40 +02:00
|
|
|
/**
|
|
|
|
* Return a list of the actions handled by this action provider.
|
|
|
|
* @param GridField $gridField Grid Field Reference
|
|
|
|
* @return Array Array with action identifier strings.
|
|
|
|
*/
|
|
|
|
public function getActions($gridField) {
|
2013-07-19 01:54:33 +02:00
|
|
|
return array('saveGridRowSort', 'sortableRowsToggle', 'sortToPage');
|
2012-05-15 16:04:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an action on the given grid field.
|
|
|
|
* @param GridField $gridField Grid Field Reference
|
|
|
|
* @param String $actionName Action identifier, see {@link getActions()}.
|
|
|
|
* @param Array $arguments Arguments relevant for this
|
|
|
|
* @param Array $data All form data
|
|
|
|
*/
|
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
|
|
|
$state = $gridField->State->GridFieldSortableRows;
|
|
|
|
if (!is_bool($state->sortableToggle)) {
|
|
|
|
$state->sortableToggle = false;
|
|
|
|
} else if ($state->sortableToggle == true) {
|
|
|
|
$gridField->getConfig()->removeComponentsByType('GridFieldFilterHeader');
|
|
|
|
$gridField->getConfig()->removeComponentsByType('GridFieldSortableHeader');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($actionName == 'savegridrowsort') {
|
|
|
|
return $this->saveGridRowSort($gridField, $data);
|
2012-05-17 03:37:08 +02:00
|
|
|
} else if ($actionName == 'sorttopage') {
|
2012-05-17 14:56:16 +02:00
|
|
|
return $this->sortToPage($gridField, $data);
|
|
|
|
}
|
2012-05-15 16:04:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles saving of the row sort order
|
|
|
|
* @param GridField $gridField Grid Field Reference
|
|
|
|
* @param Array $data Data submitted in the request
|
|
|
|
*/
|
2012-05-17 03:37:08 +02:00
|
|
|
protected function saveGridRowSort(GridField $gridField, $data) {
|
2013-02-12 03:19:25 +01:00
|
|
|
$dataList = $gridField->getList();
|
|
|
|
|
2013-02-09 00:47:34 +01:00
|
|
|
if(class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) {
|
|
|
|
user_error('Cannot sort an UnsavedRelationList', E_USER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-15 17:33:00 +02:00
|
|
|
if(!singleton($gridField->getModelClass())->canEdit()){
|
|
|
|
throw new ValidationException(_t('GridFieldSortableRows.EditPermissionsFailure', "No edit permissions"),0);
|
|
|
|
}
|
|
|
|
|
2012-09-10 14:40:54 +02:00
|
|
|
if (empty($data['ItemIDs'])) {
|
2012-05-15 16:04:40 +02:00
|
|
|
user_error('No items to sort', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = $gridField->getModelClass();
|
|
|
|
$owner = $gridField->Form->getRecord();
|
2012-05-17 22:04:53 +02:00
|
|
|
$items = clone $gridField->getList();
|
2012-05-15 16:04:40 +02:00
|
|
|
$many_many = ($items instanceof ManyManyList);
|
|
|
|
$sortColumn = $this->sortColumn;
|
2012-05-17 14:56:16 +02:00
|
|
|
$pageOffset = 0;
|
|
|
|
|
|
|
|
if ($paginator = $gridField->getConfig()->getComponentsByType('GridFieldPaginator')->First()) {
|
|
|
|
$pageState = $gridField->State->GridFieldPaginator;
|
2012-08-19 19:43:20 +02:00
|
|
|
|
|
|
|
if($pageState->currentPage && is_int($pageState->currentPage) && $pageState->currentPage>1) {
|
2012-05-17 14:56:16 +02:00
|
|
|
$pageOffset = $paginator->getItemsPerPage() * ($pageState->currentPage - 1);
|
|
|
|
}
|
|
|
|
}
|
2012-05-15 16:04:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
if ($many_many) {
|
|
|
|
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName());
|
2013-01-24 01:11:22 +01:00
|
|
|
}else {
|
|
|
|
//Find table containing the sort column
|
|
|
|
$table=false;
|
2013-01-31 19:03:33 +01:00
|
|
|
$class=$gridField->getModelClass();
|
|
|
|
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
|
|
|
if(!empty($db) && array_key_exists($sortColumn, $db)) {
|
2013-01-24 01:11:22 +01:00
|
|
|
$table=$class;
|
|
|
|
}else {
|
|
|
|
$classes=ClassInfo::ancestry($class, true);
|
|
|
|
foreach($classes as $class) {
|
2013-01-31 19:03:33 +01:00
|
|
|
$db = Config::inst()->get($class, "db", CONFIG::UNINHERITED);
|
|
|
|
if(!empty($db) && array_key_exists($sortColumn, $db)) {
|
2013-01-24 01:11:22 +01:00
|
|
|
$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;
|
|
|
|
}
|
2013-05-26 01:42:35 +02:00
|
|
|
|
|
|
|
$baseDataClass=ClassInfo::baseDataClass($gridField->getModelClass());
|
2012-05-15 16:04:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-04 17:15:37 +02:00
|
|
|
//Event to notify the Controller or owner DataObject before list sort
|
|
|
|
if($owner && $owner instanceof DataObject && method_exists($owner, 'onBeforeGridFieldRowSort')) {
|
|
|
|
$owner->onBeforeGridFieldRowSort(clone $items);
|
|
|
|
}else if(Controller::has_curr() && Controller::curr() instanceof ModelAdmin && method_exists(Controller::curr(), 'onBeforeGridFieldRowSort')) {
|
|
|
|
Controller::curr()->onBeforeGridFieldRowSort(clone $items);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-15 17:33:00 +02:00
|
|
|
//Start transaction if supported
|
|
|
|
if(DB::getConn()->supportsTransactions()) {
|
|
|
|
DB::getConn()->transactionStart();
|
|
|
|
}
|
|
|
|
|
2012-05-17 14:56:16 +02:00
|
|
|
|
2013-08-04 17:15:37 +02:00
|
|
|
//Perform sorting
|
2012-09-10 05:38:00 +02:00
|
|
|
$ids = explode(',', $data['ItemIDs']);
|
|
|
|
for($sort = 0;$sort<count($ids);$sort++) {
|
|
|
|
$id = intval($ids[$sort]);
|
2012-05-15 16:04:40 +02:00
|
|
|
if ($many_many) {
|
|
|
|
DB::query('UPDATE "' . $table
|
2012-05-17 03:37:08 +02:00
|
|
|
. '" SET "' . $sortColumn.'" = ' . (($sort + 1) + $pageOffset)
|
2012-05-15 16:04:40 +02:00
|
|
|
. ' WHERE "' . $componentField . '" = ' . $id . ' AND "' . $parentField . '" = ' . $owner->ID);
|
|
|
|
} else {
|
2013-01-24 01:11:22 +01:00
|
|
|
DB::query('UPDATE "' . $table
|
2013-05-25 18:35:55 +02:00
|
|
|
. '" SET "' . $sortColumn . '" = ' . (($sort + 1) + $pageOffset)
|
|
|
|
. ' WHERE "ID" = '. $id);
|
|
|
|
|
2013-05-26 01:42:35 +02:00
|
|
|
DB::query('UPDATE "' . $baseDataClass
|
2013-05-25 18:35:55 +02:00
|
|
|
. '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\''
|
2013-01-24 01:11:22 +01:00
|
|
|
. ' WHERE "ID" = '. $id);
|
2012-05-15 16:04:40 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-15 17:33:00 +02:00
|
|
|
|
2013-08-04 17:15:37 +02:00
|
|
|
|
2012-05-15 17:33:00 +02:00
|
|
|
//End transaction if supported
|
|
|
|
if(DB::getConn()->supportsTransactions()) {
|
|
|
|
DB::getConn()->transactionEnd();
|
|
|
|
}
|
2013-08-04 17:15:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
//Event to notify the Controller or owner DataObject after list sort
|
|
|
|
if($owner && $owner instanceof DataObject && method_exists($owner, 'onAfterGridFieldRowSort')) {
|
|
|
|
$owner->onAfterGridFieldRowSort(clone $items);
|
|
|
|
}else if(Controller::has_curr() && Controller::curr() instanceof ModelAdmin && method_exists(Controller::curr(), 'onAfterGridFieldRowSort')) {
|
|
|
|
Controller::curr()->onAfterGridFieldRowSort(clone $items);
|
|
|
|
}
|
2012-05-15 16:04:40 +02:00
|
|
|
}
|
2012-05-17 03:37:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles sorting across pages
|
|
|
|
* @param GridField $gridField Grid Field Reference
|
|
|
|
* @param Array $data Data submitted in the request
|
|
|
|
*/
|
|
|
|
protected function sortToPage(GridField $gridField, $data) {
|
2012-05-17 14:56:16 +02:00
|
|
|
if (!$paginator = $gridField->getConfig()->getComponentsByType('GridFieldPaginator')->First()) {
|
|
|
|
user_error('Paginator not detected', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($data['ItemID'])) {
|
2012-05-17 03:37:08 +02:00
|
|
|
user_error('No item to sort', E_USER_ERROR);
|
|
|
|
}
|
2012-05-17 14:56:16 +02:00
|
|
|
|
|
|
|
if (empty($data['Target'])) {
|
2012-05-17 03:37:08 +02:00
|
|
|
user_error('No target page', E_USER_ERROR);
|
|
|
|
}
|
2012-05-17 14:56:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
$className = $gridField->getModelClass();
|
2012-05-17 03:37:08 +02:00
|
|
|
$owner = $gridField->Form->getRecord();
|
2012-05-17 22:04:53 +02:00
|
|
|
$items = clone $gridField->getList();
|
2012-05-17 03:37:08 +02:00
|
|
|
$many_many = ($items instanceof ManyManyList);
|
|
|
|
$sortColumn = $this->sortColumn;
|
2012-05-17 14:56:16 +02:00
|
|
|
$targetItem = $items->byID(intval($data['ItemID']));
|
|
|
|
|
|
|
|
if (!$targetItem) {
|
|
|
|
user_error('Target item not found', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
$currentPage = 1;
|
|
|
|
|
|
|
|
|
|
|
|
$pageState = $gridField->State->GridFieldPaginator;
|
|
|
|
if($pageState->currentPage && $pageState->currentPage>1) {
|
|
|
|
$currentPage = $pageState->currentPage;
|
|
|
|
}
|
|
|
|
|
2012-05-17 03:37:08 +02:00
|
|
|
|
|
|
|
if ($many_many) {
|
|
|
|
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName());
|
|
|
|
}
|
2012-05-17 14:56:16 +02:00
|
|
|
|
|
|
|
|
2012-05-18 15:50:09 +02:00
|
|
|
if ($data['Target'] == 'previouspage') {
|
2013-07-10 19:20:16 +02:00
|
|
|
$items = $items->limit($paginator->getItemsPerPage() + 1, ($paginator->getItemsPerPage() * ($currentPage - 1)) - 1);
|
2012-05-17 14:56:16 +02:00
|
|
|
} else if ($data['Target'] == 'nextpage') {
|
2013-07-10 19:20:16 +02:00
|
|
|
$items = $items->limit($paginator->getItemsPerPage() + 1, $paginator->getItemsPerPage() * ($currentPage - 1));
|
2012-05-17 14:56:16 +02:00
|
|
|
} else {
|
|
|
|
user_error('Not implemented: '.$data['Target'], E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-07-10 19:20:16 +02:00
|
|
|
$sortPositions = $items->column($sortColumn);
|
|
|
|
|
2012-05-17 14:56:16 +02:00
|
|
|
|
2013-08-04 17:15:37 +02:00
|
|
|
//Event to notify the Controller or owner DataObject before list sort
|
|
|
|
if($owner && $owner instanceof DataObject && method_exists($owner, 'onBeforeGridFieldPageSort')) {
|
|
|
|
$owner->onBeforeGridFieldPageSort(clone $items);
|
|
|
|
}else if(Controller::has_curr() && Controller::curr() instanceof ModelAdmin && method_exists(Controller::curr(), 'onBeforeGridFieldPageSort')) {
|
|
|
|
Controller::curr()->onBeforeGridFieldPageSort(clone $items);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-18 15:50:09 +02:00
|
|
|
//Start transaction if supported
|
|
|
|
if(DB::getConn()->supportsTransactions()) {
|
|
|
|
DB::getConn()->transactionStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
if($data['Target']=='previouspage') {
|
2012-05-17 14:56:16 +02:00
|
|
|
if ($many_many) {
|
|
|
|
DB::query('UPDATE "' . $table
|
2012-05-18 15:50:09 +02:00
|
|
|
. '" SET "' . $sortColumn.'" = ' . $sortPositions[0]
|
|
|
|
. ' WHERE "' . $componentField . '" = ' . $targetItem->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
|
2012-05-17 14:56:16 +02:00
|
|
|
} else {
|
2012-05-18 15:50:09 +02:00
|
|
|
$targetItem->$sortColumn = $sortPositions[0];
|
|
|
|
$targetItem->write();
|
2012-05-17 14:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-18 15:50:09 +02:00
|
|
|
$i = 1;
|
|
|
|
foreach ($items as $obj) {
|
|
|
|
if ($obj->ID == $targetItem->ID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($many_many) {
|
|
|
|
DB::query('UPDATE "' . $table
|
|
|
|
. '" SET "' . $sortColumn.'" = ' . $sortPositions[$i]
|
|
|
|
. ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
|
|
|
|
} else {
|
|
|
|
$obj->$sortColumn = $sortPositions[$i];
|
|
|
|
$obj->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
} else {
|
2012-05-17 14:56:16 +02:00
|
|
|
if ($many_many) {
|
|
|
|
DB::query('UPDATE "' . $table
|
2012-05-18 15:50:09 +02:00
|
|
|
. '" SET "' . $sortColumn.'" = ' . $sortPositions[count($sortPositions) - 1]
|
2012-05-17 14:56:16 +02:00
|
|
|
. ' WHERE "' . $componentField . '" = ' . $targetItem->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
|
|
|
|
} else {
|
2012-05-18 15:50:09 +02:00
|
|
|
$targetItem->$sortColumn = $sortPositions[count($sortPositions) - 1];
|
2012-05-17 14:56:16 +02:00
|
|
|
$targetItem->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-18 15:50:09 +02:00
|
|
|
$i = 0;
|
|
|
|
foreach ($items as $obj) {
|
|
|
|
if ($obj->ID == $targetItem->ID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($many_many) {
|
|
|
|
DB::query('UPDATE "' . $table
|
|
|
|
. '" SET "' . $sortColumn.'" = ' . $sortPositions[$i]
|
|
|
|
. ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
|
|
|
|
} else {
|
|
|
|
$obj->$sortColumn = $sortPositions[$i];
|
|
|
|
$obj->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
$i++;
|
2012-05-17 14:56:16 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-18 15:50:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
//End transaction if supported
|
|
|
|
if(DB::getConn()->supportsTransactions()) {
|
|
|
|
DB::getConn()->transactionEnd();
|
|
|
|
}
|
2013-08-04 17:15:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
//Event to notify the Controller or owner DataObject after list sort
|
|
|
|
if($owner && $owner instanceof DataObject && method_exists($owner, 'onAfterGridFieldPageSort')) {
|
|
|
|
$owner->onAfterGridFieldPageSort(clone $items);
|
|
|
|
}else if(Controller::has_curr() && Controller::curr() instanceof ModelAdmin && method_exists(Controller::curr(), 'onAfterGridFieldPageSort')) {
|
|
|
|
Controller::curr()->onAfterGridFieldPageSort(clone $items);
|
|
|
|
}
|
2012-05-17 14:56:16 +02:00
|
|
|
}
|
2012-05-09 19:14:34 +02:00
|
|
|
}
|
2013-01-27 15:03:24 +01:00
|
|
|
?>
|