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) {
|
|
|
|
$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 15:39:37 +02:00
|
|
|
if(!singleton($gridField->getModelClass())->canEdit() || !$gridField->getList() || $gridField->getList()->Count()>1){
|
2012-05-15 17:33:00 +02:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:04:40 +02:00
|
|
|
|
|
|
|
//Sort order toggle
|
|
|
|
$sortOrderToggle = Object::create(
|
|
|
|
'GridField_FormAction',
|
|
|
|
$gridField,
|
|
|
|
'sortablerows-toggle',
|
|
|
|
_t('GridFieldSortableRows.ALLOW_DRAG_DROP', '_Allow drag and drop re-ordering'),
|
|
|
|
'saveGridRowSort',
|
|
|
|
null
|
|
|
|
)->addExtraClass('sortablerows-toggle');
|
|
|
|
|
|
|
|
|
|
|
|
//Disable Pagenator
|
|
|
|
$disablePagenator = Object::create(
|
|
|
|
'GridField_FormAction',
|
|
|
|
$gridField,
|
|
|
|
'sortablerows-disablepagenator',
|
|
|
|
_t('GridFieldSortableRows.DISABLE_PAGINATOR', '_Disable Pagenator'),
|
|
|
|
'sortableRowsDisablePaginator',
|
|
|
|
null
|
|
|
|
)->addExtraClass('sortablerows-disablepagenator');
|
|
|
|
|
|
|
|
|
2012-05-17 03:37:08 +02:00
|
|
|
//Disable Pagenator
|
|
|
|
$sortToPage = Object::create(
|
|
|
|
'GridField_FormAction',
|
|
|
|
$gridField,
|
|
|
|
'sortablerows-sorttopage',
|
|
|
|
_t('GridFieldSortableRows.SORT_TO_PAGE', '_Sort To Page'),
|
|
|
|
'sortToPage',
|
|
|
|
null
|
|
|
|
)->addExtraClass('sortablerows-sorttopage');
|
|
|
|
|
|
|
|
|
2012-05-15 16:04:40 +02:00
|
|
|
$data = array('SortableToggle' => $sortOrderToggle,
|
|
|
|
'PagenatorToggle' => $disablePagenator,
|
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
|
|
|
|
Requirements::css('SortableGridField/css/GridFieldSortableRows.css');
|
|
|
|
Requirements::javascript('SortableGridField/javascript/GridFieldSortableRows.js');
|
|
|
|
|
|
|
|
|
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-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
|
|
|
|
2012-05-18 03:05:56 +02:00
|
|
|
|
|
|
|
//Detect and correct items with a sort column value of 0 (push to bottom)
|
2012-05-19 02:04:34 +02:00
|
|
|
$this->fixSortColumn($gridField, $dataList);
|
2012-05-18 03:05:56 +02:00
|
|
|
|
|
|
|
|
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) {
|
2012-05-18 03:05:56 +02:00
|
|
|
$list=clone $dataList;
|
2012-06-19 01:32:51 +02:00
|
|
|
$list->dataQuery()->limit(array());
|
2012-05-18 03:05:56 +02:00
|
|
|
$max = $list->Max($this->sortColumn);
|
2012-07-10 16:33:22 +02:00
|
|
|
if($list->where('"'.$this->sortColumn.'"=0')->Count()>0) {
|
2012-05-18 03:05:56 +02:00
|
|
|
//Start transaction if supported
|
|
|
|
if(DB::getConn()->supportsTransactions()) {
|
|
|
|
DB::getConn()->transactionStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$owner = $gridField->Form->getRecord();
|
|
|
|
$sortColumn = $this->sortColumn;
|
|
|
|
$i = 1;
|
|
|
|
$many_many = ($list instanceof ManyManyList);
|
|
|
|
if ($many_many) {
|
|
|
|
list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//@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)
|
|
|
|
. ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID);
|
|
|
|
}else {
|
|
|
|
$obj->$sortColumn = ($max + $i);
|
|
|
|
$obj->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
2012-05-17 03:37:08 +02:00
|
|
|
return array('saveGridRowSort', 'sortableRowsDisablePaginator', '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) {
|
2012-05-15 17:33:00 +02:00
|
|
|
if(!singleton($gridField->getModelClass())->canEdit()){
|
|
|
|
throw new ValidationException(_t('GridFieldSortableRows.EditPermissionsFailure', "No edit permissions"),0);
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:04:40 +02:00
|
|
|
if (empty($data['Items'])) {
|
|
|
|
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;
|
|
|
|
if($pageState->currentPage && $pageState->currentPage>1) {
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
//@TODO Need to optimize this to eliminate some of the resource load could use raw queries to be more efficient
|
2012-05-15 16:04:40 +02:00
|
|
|
$data['Items'] = explode(',', $data['Items']);
|
|
|
|
for($sort = 0;$sort<count($data['Items']);$sort++) {
|
|
|
|
$id = intval($data['Items'][$sort]);
|
|
|
|
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 {
|
|
|
|
$obj = $items->byID($data['Items'][$sort]);
|
2012-05-17 03:37:08 +02:00
|
|
|
$obj->$sortColumn = ($sort + 1) + $pageOffset;
|
2012-05-15 16:04:40 +02:00
|
|
|
$obj->write();
|
|
|
|
}
|
|
|
|
}
|
2012-05-15 17:33:00 +02:00
|
|
|
|
|
|
|
//End transaction if supported
|
|
|
|
if(DB::getConn()->supportsTransactions()) {
|
|
|
|
DB::getConn()->transactionEnd();
|
|
|
|
}
|
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') {
|
|
|
|
$sortPositions = $items->limit($paginator->getItemsPerPage() + 1, ($paginator->getItemsPerPage() * ($currentPage - 1)) - 1)->column($sortColumn);
|
2012-05-17 14:56:16 +02:00
|
|
|
} else if ($data['Target'] == 'nextpage') {
|
2012-05-18 15:50:09 +02:00
|
|
|
$sortPositions = $items->limit($paginator->getItemsPerPage() + 1, $paginator->getItemsPerPage() * ($currentPage - 1))->column($sortColumn);
|
2012-05-17 14:56:16 +02:00
|
|
|
} else {
|
|
|
|
user_error('Not implemented: '.$data['Target'], E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2012-05-17 14:56:16 +02:00
|
|
|
}
|
2012-05-09 19:14:34 +02:00
|
|
|
}
|
|
|
|
?>
|