silverstripe-framework/forms/gridfield/GridFieldSortableHeader.php
Stig Lindqvist 3c516b7b97 API CHANGE: Refactored GridField modifiers into GridField_ColumnProvider, GridField_HTMLProvider, GridField_ActionProvider, and GridField_DataModifier interfaces, all added as components in the config.
API CHANGE: Simplified state handling so that it's just a key store. Affectors are replaced with GridField_ActionProviders. API CHANGE: Removed GridField state manipulation actions instead opting for GridField_ActionProvider actions.
API CHANGE: Removed support for modifiers that add "body" rows, instead having core support for generating the body rows hardcoded into the GridField.
API CHANGE: Allow modification of columns across the whole GridField with the GridField_ColumnProvider interface.
API CHANGE: Renamed GridField_AlterAction to GridField_Action, and added actionName/args parameters, since it can be used for all actions (including batch actions and row actions)
API CHANGE: Removed GridFieldRow class.
2012-01-09 13:30:34 +13:00

82 lines
2.4 KiB
PHP

<?php
/**
* GridFieldSortableHeader adds column headers to a gridfield that can also sort the columns
*
* @see GridField
*
* @package sapphire
* @subpackage fields-relational
*/
class GridFieldSortableHeader implements GridField_HTMLProvider, GridField_DataManipulator, GridField_ActionProvider {
/**
* Returns the header row providing titles with sort buttons
*/
public function getHTMLFragments($gridField) {
Requirements::javascript(SAPPHIRE_DIR.'/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
Requirements::javascript(SAPPHIRE_DIR.'/javascript/GridField.js');
$forTemplate = new ArrayData(array());
$forTemplate->Fields = new ArrayList;
$state = $gridField->State->GridFieldSortableHeader;
$columns = $gridField->getColumns();
foreach($columns as $columnField) {
$metadata = $gridField->getColumnMetadata($columnField);
$title = $metadata['title'];
if($title && $gridField->getList()->canSortBy($columnField)) {
$dir = 'asc';
if($state->SortColumn == $columnField && $state->SortDirection == 'asc') {
$dir = 'desc';
}
$field = new GridField_Action($gridField, 'SetOrder'.$columnField, $title, "sort$dir", array('SortColumn' => $columnField));
$field->addExtraClass('ss-gridfield-sort');
if($state->SortColumn == $columnField){
$field->addExtraClass('ss-gridfield-sorted');
}
} else {
$field = new LiteralField($columnField, $title);
}
$forTemplate->Fields->push($field);
}
return array(
'header' => $forTemplate->renderWith('GridFieldSortableHeader_Row'),
);
}
/**
*
* @param GridField $gridField
* @return array
*/
public function getActions($gridField) {
return array('sortasc', 'sortdesc');
}
function handleAction(GridField $gridField, $actionName, $arguments, $data) {
$state = $gridField->State->GridFieldSortableHeader;
switch($actionName) {
case 'sortasc':
$state->SortColumn = $arguments['SortColumn'];
$state->SortDirection = 'asc';
break;
case 'sortdesc':
$state->SortColumn = $arguments['SortColumn'];
$state->SortDirection = 'desc';
break;
}
}
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
$state = $gridField->State->GridFieldSortableHeader;
if ($state->SortColumn == "") {
return $dataList;
}
return $dataList->sort($state->SortColumn, $state->SortDirection);
}
}