Added event methods that allow detection of the sort action being started or completed resolves #39

This commit is contained in:
Ed 2013-08-04 12:15:37 -03:00
parent f21e08f73e
commit cd20fdf697
2 changed files with 37 additions and 0 deletions

View File

@ -36,6 +36,9 @@ To move an item to another page drag the row over the respective page button and
* [has_many relationship] (https://github.com/UndefinedOffset/SortableGridField/blob/master/docs/HasManyExample.md)
* [many_many relationship] (https://github.com/UndefinedOffset/SortableGridField/blob/master/docs/ManyManyExample.md)
#### Events
GridFieldSortableRows provides 4 "events" onBeforeGridFieldRowSort(), onAfterGridFieldRowSort(), onBeforeGridFieldPageSort() and onAfterGridFieldPageSort(). These "events" are passed a clone of the DataList used in GridFieldSortableRows, in the case of page sorting this list has a limit that shows you the current page plus/minus one object. For GridFieldSortableRows that are on ModelAdmin decendents these events are called on the ModelAdmin if they do not have a owner DataObject, if you are using GridFieldSortableRows on a GridField for a DataObject's relationship the events are called on that DataObject.
## Migrating from SilverStripe 2.4 and Data Object Manager's SortableDataObject
SortableGridField is not the same as SortableDataObject, since it is only a component of GridField it does not have the ability to catch the object when it is saved for the first time. So SortableGridField uses 1 as the first sort index because 0 is the default for an integer field/column in the database. For migrations from 2.4 with SortableDataObject you need to setup your DataObject based on the instructions above however you must name your sort column "SortOrder" to maintain your sort indexes defined by SortableDataObject. Then you need to run the following query on the table containing your sort field, for many_many relationships this will be something like {RelationshipClass}_{RelationshipName}. This query will maintain your sort order from SortableDataObject but increment the index by 1 giving it a starting number of 1.

View File

@ -315,12 +315,21 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
}
//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);
}
//Start transaction if supported
if(DB::getConn()->supportsTransactions()) {
DB::getConn()->transactionStart();
}
//Perform sorting
$ids = explode(',', $data['ItemIDs']);
for($sort = 0;$sort<count($ids);$sort++) {
$id = intval($ids[$sort]);
@ -339,10 +348,19 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
}
}
//End transaction if supported
if(DB::getConn()->supportsTransactions()) {
DB::getConn()->transactionEnd();
}
//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);
}
}
/**
@ -400,6 +418,14 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
$sortPositions = $items->column($sortColumn);
//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);
}
//Start transaction if supported
if(DB::getConn()->supportsTransactions()) {
DB::getConn()->transactionStart();
@ -470,6 +496,14 @@ class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionP
if(DB::getConn()->supportsTransactions()) {
DB::getConn()->transactionEnd();
}
//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);
}
}
}
?>