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 ;
2015-04-07 00:09:06 +02:00
protected $disable_selection = true ;
2013-10-19 19:34:45 +02:00
protected $append_to_top = false ;
2015-04-07 00:09:06 +02:00
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
2015-04-07 00:09:06 +02:00
* @ param bool $disableSelection
2012-05-10 16:27:01 +02:00
*/
2015-04-07 00:09:06 +02:00
public function __construct ( $sortColumn , $disableSelection = true ) {
2012-05-15 16:04:40 +02:00
$this -> sortColumn = $sortColumn ;
2015-04-07 00:09:06 +02:00
$this -> disable_selection = $disableSelection ;
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 ,
2015-09-07 01:45:00 +02:00
'Checked' => ( $state -> sortableToggle == true ? ' checked = "checked"' : '' ),
'List' => $dataList );
2012-05-15 16:04:40 +02:00
$forTemplate = new ArrayData ( $data );
//Inject Requirements
2015-04-13 23:17:21 +02:00
$custom = Config :: inst () -> get ( 'GridFieldSortableRows' , 'Base' );
$base = $custom ? : SORTABLE_GRIDFIELD_BASE ;
Requirements :: css ( $base . '/css/GridFieldSortableRows.css' );
Requirements :: javascript ( $base . '/javascript/GridFieldSortableRows.js' );
2012-05-15 16:04:40 +02:00
2015-04-07 00:09:06 +02:00
$args = array ( 'Colspan' => count ( $gridField -> getColumns ()), 'ID' => $gridField -> ID (), 'DisableSelection' => $this -> disable_selection );
2012-05-15 16:04:40 +02:00
2015-09-07 01:45:00 +02:00
$fragments = array ( 'header' => $forTemplate -> renderWith ( 'GridFieldSortableRows' , $args ));
if ( $gridField -> getConfig () -> getComponentByType ( 'GridFieldPaginator' )) {
$fragments [ 'after' ] = $forTemplate -> renderWith ( 'GridFieldSortableRows_paginator' );
}
return $fragments ;
2012-05-15 16:04:40 +02:00
}
/**
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 )) {
2014-10-03 16:01:27 +02:00
return $dataList -> sort ( $this -> sortColumn );
2012-05-10 16:42:52 +02:00
}
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 );
}
2013-10-19 19:34:45 +02:00
/**
* 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 ;
}
2015-04-07 00:09:06 +02:00
/**
* @ param bool $value Boolean true to disable selection of table contents false to enable selection
* @ return GridFieldSortableRows Returns the current instance
*/
public function setDisableSelection ( $value ){
$this -> disable_selection = $value ;
return $this ;
}
2013-10-19 19:34:45 +02:00
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 );
2014-08-06 17:34:25 +02:00
$list = $list -> filter ( $this -> sortColumn , 0 ) -> sort ( " Created,ID " );
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 ]))) {
2014-10-16 15:25:23 +02:00
user_error ( 'Sort column ' . $this -> sortColumn . ' must be an Int, column is of type ' . $extraFields [ $this -> sortColumn ], E_USER_ERROR );
2012-10-12 14:36:45 +02:00
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
}
2013-10-19 19:34:45 +02:00
$idCondition = null ;
if ( $this -> append_to_top && ! ( $list instanceof RelationList )) {
2015-05-07 17:44:55 +02:00
$idCondition = '"ID" IN(\'' . implode ( " ',' " , $dataList -> getIDList ()) . '\')' ;
2013-10-19 19:34:45 +02:00
}
2012-10-12 14:36:45 +02:00
2013-10-22 15:36:08 +02:00
if ( $this -> append_to_top ) {
$topIncremented = array ();
}
2012-05-18 03:05:56 +02:00
foreach ( $list as $obj ) {
if ( $many_many ) {
2013-10-19 19:34:45 +02:00
if ( $this -> append_to_top ) {
2013-10-22 15:36:08 +02:00
//Upgrade all the records (including the last inserted from 0 to 1)
2013-09-04 15:05:28 +02:00
DB :: query ( 'UPDATE "' . $table
. '" SET "' . $sortColumn . '" = "' . $sortColumn . '"+1'
2013-10-22 15:36:08 +02:00
. ' WHERE "' . $parentField . '" = ' . $owner -> ID . ( ! empty ( $topIncremented ) ? ' AND "' . $componentField . '" NOT IN(\'' . implode ( '\',\'' , $topIncremented ) . '\')' : '' ));
$topIncremented [] = $obj -> ID ;
2013-10-19 19:34:45 +02:00
} else {
//Append the last record to the bottom
2013-09-04 15:05:28 +02:00
DB :: query ( 'UPDATE "' . $table
. '" SET "' . $sortColumn . '" = ' . ( $max + $i )
. ' WHERE "' . $componentField . '" = ' . $obj -> ID . ' AND "' . $parentField . '" = ' . $owner -> ID );
}
2013-10-19 19:34:45 +02:00
} else if ( $this -> append_to_top ) {
2013-10-22 15:36:08 +02:00
//Upgrade all the records (including the last inserted from 0 to 1)
2013-10-19 19:34:45 +02:00
DB :: query ( 'UPDATE "' . $table
. '" SET "' . $sortColumn . '" = "' . $sortColumn . '"+1'
2013-10-22 15:36:08 +02:00
. ' WHERE ' . ( $list instanceof RelationList ? '"' . $list -> foreignKey . '" = ' . $owner -> ID : $idCondition ) . ( ! empty ( $topIncremented ) ? ' AND "ID" NOT IN(\'' . implode ( '\',\'' , $topIncremented ) . '\')' : '' ));
2013-10-19 19:34:45 +02:00
//LastEdited
DB :: query ( 'UPDATE "' . $baseDataClass
. '" SET "LastEdited" = \'' . date ( 'Y-m-d H:i:s' ) . '\''
2013-10-22 15:36:08 +02:00
. ' WHERE ' . ( $list instanceof RelationList ? '"' . $list -> foreignKey . '" = ' . $owner -> ID : $idCondition ) . ( ! empty ( $topIncremented ) ? ' AND "ID" NOT IN(\'' . implode ( '\',\'' , $topIncremented ) . '\')' : '' ));
$topIncremented [] = $obj -> ID ;
2013-10-19 19:34:45 +02:00
} else {
//Append the last record to the bottom
DB :: query ( 'UPDATE "' . $table
2013-09-04 15:05:28 +02:00
. '" SET "' . $sortColumn . '" = ' . ( $max + $i )
. ' WHERE "ID" = ' . $obj -> ID );
2013-10-19 19:34:45 +02:00
//LastEdited
DB :: query ( 'UPDATE "' . $baseDataClass
2013-09-04 15:05:28 +02:00
. '" SET "LastEdited" = \'' . date ( 'Y-m-d H:i:s' ) . '\''
. ' 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 ;
2015-09-07 01:45:00 +02:00
$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 ());
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
?>