2011-12-06 01:56:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* GridFieldSortableHeader adds column headers to a gridfield that can also sort the columns
|
|
|
|
*
|
|
|
|
* @see GridField
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-12-06 01:56:24 +01:00
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
|
|
|
class GridFieldSortableHeader implements GridField_HTMLProvider, GridField_DataManipulator, GridField_ActionProvider {
|
2012-03-09 02:32:16 +01:00
|
|
|
|
|
|
|
/**
|
2012-03-10 02:44:12 +01:00
|
|
|
* See {@link setThrowExceptionOnBadDataType()}
|
2012-03-09 02:32:16 +01:00
|
|
|
*/
|
|
|
|
protected $throwExceptionOnBadDataType = true;
|
2012-08-28 11:15:46 +02:00
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
public $fieldSorting = array();
|
2012-03-09 02:32:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine what happens when this component is used with a list that isn't {@link SS_Filterable}.
|
|
|
|
*
|
2012-05-16 02:18:23 +02:00
|
|
|
* - true: An exception is thrown
|
2012-03-09 02:32:16 +01:00
|
|
|
* - false: This component will be ignored - it won't make any changes to the GridField.
|
|
|
|
*
|
|
|
|
* By default, this is set to true so that it's clearer what's happening, but the predefined
|
|
|
|
* {@link GridFieldConfig} subclasses set this to false for flexibility.
|
|
|
|
*/
|
2012-03-10 02:44:12 +01:00
|
|
|
public function setThrowExceptionOnBadDataType($throwExceptionOnBadDataType) {
|
2012-03-09 02:32:16 +01:00
|
|
|
$this->throwExceptionOnBadDataType = $throwExceptionOnBadDataType;
|
|
|
|
}
|
2012-03-10 02:44:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See {@link setThrowExceptionOnBadDataType()}
|
|
|
|
*/
|
|
|
|
public function getThrowExceptionOnBadDataType() {
|
|
|
|
return $this->throwExceptionOnBadDataType;
|
|
|
|
}
|
2012-03-09 02:32:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that this dataList is of the right data type.
|
|
|
|
* Returns false if it's a bad data type, and if appropriate, throws an exception.
|
|
|
|
*/
|
|
|
|
protected function checkDataType($dataList) {
|
|
|
|
if($dataList instanceof SS_Sortable) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if($this->throwExceptionOnBadDataType) {
|
|
|
|
throw new LogicException(get_class($this) . " expects an SS_Sortable list to be passed to the GridField.");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 11:15:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify sortings with fieldname as the key, and actual fieldname to sort as value.
|
|
|
|
* Example: array("MyCustomTitle"=>"Title", "MyCustomBooleanField" => "ActualBooleanField")
|
|
|
|
*
|
|
|
|
* @param array $casting
|
|
|
|
*/
|
|
|
|
public function setFieldSorting($sorting) {
|
|
|
|
$this->fieldSorting = $sorting;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getFieldSorting() {
|
|
|
|
return $this->fieldSorting;
|
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the header row providing titles with sort buttons
|
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField) {
|
2012-03-09 02:32:16 +01:00
|
|
|
if(!$this->checkDataType($gridField->getList())) return;
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
$forTemplate = new ArrayData(array());
|
|
|
|
$forTemplate->Fields = new ArrayList;
|
|
|
|
|
|
|
|
$state = $gridField->State->GridFieldSortableHeader;
|
|
|
|
$columns = $gridField->getColumns();
|
2012-05-16 02:18:23 +02:00
|
|
|
$currentColumn = 0;
|
2011-12-06 01:56:24 +01:00
|
|
|
foreach($columns as $columnField) {
|
2012-05-16 02:18:23 +02:00
|
|
|
$currentColumn++;
|
2011-12-06 01:56:24 +01:00
|
|
|
$metadata = $gridField->getColumnMetadata($columnField);
|
|
|
|
$title = $metadata['title'];
|
2012-08-28 11:15:46 +02:00
|
|
|
if(isset($this->fieldSorting[$columnField]) && $this->fieldSorting[$columnField]) $columnField = $this->fieldSorting[$columnField];
|
2011-12-06 01:56:24 +01:00
|
|
|
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(
|
2012-03-06 12:51:57 +01:00
|
|
|
'GridField_FormAction', $gridField, 'SetOrder'.$columnField, $title,
|
2012-03-06 16:58:13 +01:00
|
|
|
"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-05-16 06:50:11 +02:00
|
|
|
if($currentColumn == count($columns) && $gridField->getConfig()->getComponentByType('GridFieldFilterHeader')){
|
2012-05-17 01:15:53 +02:00
|
|
|
$field = new LiteralField($columnField, '<button name="showFilter" class="ss-gridfield-button-filter trigger"></button>');
|
2012-05-16 02:18:23 +02:00
|
|
|
}else{
|
|
|
|
$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) {
|
2012-03-09 02:32:16 +01:00
|
|
|
if(!$this->checkDataType($gridField->getList())) return;
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
return array('sortasc', 'sortdesc');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
2012-03-09 02:32:16 +01:00
|
|
|
if(!$this->checkDataType($gridField->getList())) return;
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
$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) {
|
2012-03-09 02:32:16 +01:00
|
|
|
if(!$this->checkDataType($dataList)) return $dataList;
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
$state = $gridField->State->GridFieldSortableHeader;
|
|
|
|
if ($state->SortColumn == "") {
|
|
|
|
return $dataList;
|
|
|
|
}
|
|
|
|
return $dataList->sort($state->SortColumn, $state->SortDirection);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|