2011-12-06 01:56:24 +01:00
|
|
|
<?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) {
|
|
|
|
$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';
|
|
|
|
}
|
|
|
|
|
2012-03-06 16:58:13 +01:00
|
|
|
$field = Object::create(
|
|
|
|
'GridField_Action', $gridField, 'SetOrder'.$columnField, $title,
|
|
|
|
"sort$dir", array('SortColumn' => $columnField)
|
|
|
|
)->addExtraClass('ss-gridfield-sort');
|
2011-12-06 01:56:24 +01:00
|
|
|
|
|
|
|
if($state->SortColumn == $columnField){
|
|
|
|
$field->addExtraClass('ss-gridfield-sorted');
|
2012-02-29 02:34:06 +01:00
|
|
|
|
|
|
|
if($state->SortDirection == 'asc')
|
|
|
|
$field->addExtraClass('ss-gridfield-sorted-asc');
|
|
|
|
else
|
|
|
|
$field->addExtraClass('ss-gridfield-sorted-desc');
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-03-01 23:06:24 +01:00
|
|
|
$field = new LiteralField($columnField, '<span class="non-sortable">' . $title . '</span>');
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|