2011-12-06 01:56:24 +01:00
|
|
|
<?php
|
2013-05-20 12:18:07 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms\GridField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\LiteralField;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\ORM\Sortable;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\SSViewer;
|
|
|
|
use LogicException;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* GridFieldSortableHeader adds column headers to a {@link GridField} that can
|
2013-05-20 12:18:07 +02:00
|
|
|
* also sort the columns.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-06 01:56:24 +01:00
|
|
|
* @see GridField
|
|
|
|
*/
|
|
|
|
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()}
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @var bool
|
2012-03-09 02:32:16 +01:00
|
|
|
*/
|
|
|
|
protected $throwExceptionOnBadDataType = true;
|
2012-08-28 11:15:46 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
2013-05-20 12:18:07 +02:00
|
|
|
*/
|
2012-08-28 11:15:46 +02:00
|
|
|
public $fieldSorting = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-09 02:32:16 +01:00
|
|
|
* 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.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param bool $throwExceptionOnBadDataType
|
|
|
|
* @return $this
|
2012-03-09 02:32:16 +01:00
|
|
|
*/
|
2012-03-10 02:44:12 +01:00
|
|
|
public function setThrowExceptionOnBadDataType($throwExceptionOnBadDataType) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->throwExceptionOnBadDataType = $throwExceptionOnBadDataType;
|
2016-08-19 00:51:35 +02:00
|
|
|
return $this;
|
2012-03-09 02:32:16 +01:00
|
|
|
}
|
2012-03-10 02:44:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See {@link setThrowExceptionOnBadDataType()}
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2012-03-10 02:44:12 +01:00
|
|
|
*/
|
|
|
|
public function getThrowExceptionOnBadDataType() {
|
|
|
|
return $this->throwExceptionOnBadDataType;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param SS_List $dataList
|
|
|
|
* @return bool
|
2012-03-09 02:32:16 +01:00
|
|
|
*/
|
|
|
|
protected function checkDataType($dataList) {
|
2016-09-09 08:43:05 +02:00
|
|
|
if($dataList instanceof Sortable) {
|
2012-03-09 02:32:16 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if($this->throwExceptionOnBadDataType) {
|
2012-09-26 23:34:00 +02:00
|
|
|
throw new LogicException(
|
|
|
|
get_class($this) . " expects an SS_Sortable list to be passed to the GridField.");
|
2012-03-09 02:32:16 +01:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param array $sorting
|
|
|
|
* @return $this
|
2012-08-28 11:15:46 +02:00
|
|
|
*/
|
|
|
|
public function setFieldSorting($sorting) {
|
|
|
|
$this->fieldSorting = $sorting;
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-08-28 11:15:46 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getFieldSorting() {
|
|
|
|
return $this->fieldSorting;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Returns the header row providing titles with sort buttons
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$list = $gridField->getList();
|
|
|
|
if(!$this->checkDataType($list)) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-09-09 08:43:05 +02:00
|
|
|
/** @var Sortable $list */
|
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;
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2016-10-06 06:31:38 +02:00
|
|
|
$schema = DataObject::getSchema();
|
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);
|
2014-02-19 22:17:52 +01:00
|
|
|
$fieldName = str_replace('.', '-', $columnField);
|
2011-12-06 01:56:24 +01:00
|
|
|
$title = $metadata['title'];
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($this->fieldSorting[$columnField]) && $this->fieldSorting[$columnField]) {
|
|
|
|
$columnField = $this->fieldSorting[$columnField];
|
|
|
|
}
|
2013-10-23 22:59:06 +02:00
|
|
|
|
|
|
|
$allowSort = ($title && $list->canSortBy($columnField));
|
|
|
|
|
2013-11-25 04:38:46 +01:00
|
|
|
if(!$allowSort && strpos($columnField, '.') !== false) {
|
2013-10-23 22:59:06 +02:00
|
|
|
// we have a relation column with dot notation
|
2013-11-25 04:38:46 +01:00
|
|
|
// @see DataObject::relField for approximation
|
2013-10-23 22:59:06 +02:00
|
|
|
$parts = explode('.', $columnField);
|
|
|
|
$tmpItem = singleton($list->dataClass());
|
|
|
|
for($idx = 0; $idx < sizeof($parts); $idx++) {
|
|
|
|
$methodName = $parts[$idx];
|
2013-11-25 04:38:46 +01:00
|
|
|
if($tmpItem instanceof SS_List) {
|
2013-10-23 22:59:06 +02:00
|
|
|
// It's impossible to sort on a HasManyList/ManyManyList
|
2013-11-25 04:38:46 +01:00
|
|
|
break;
|
2015-08-03 17:49:18 +02:00
|
|
|
} elseif(method_exists($tmpItem, 'hasMethod') && $tmpItem->hasMethod($methodName)) {
|
2013-11-25 04:38:46 +01:00
|
|
|
// The part is a relation name, so get the object/list from it
|
2013-10-23 22:59:06 +02:00
|
|
|
$tmpItem = $tmpItem->$methodName();
|
2016-10-06 06:31:38 +02:00
|
|
|
} elseif ($tmpItem instanceof DataObject && $schema->fieldSpec($tmpItem, $methodName, ['dbOnly'])) {
|
2015-09-30 17:25:17 +02:00
|
|
|
// Else, if we've found a database field at the end of the chain, we can sort on it.
|
2013-11-25 04:38:46 +01:00
|
|
|
// If a method is applied further to this field (E.g. 'Cost.Currency') then don't try to sort.
|
|
|
|
$allowSort = $idx === sizeof($parts) - 1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// If neither method nor field, then unable to sort
|
|
|
|
break;
|
2013-10-23 22:59:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($allowSort) {
|
2011-12-06 01:56:24 +01:00
|
|
|
$dir = 'asc';
|
2013-10-10 23:08:59 +02:00
|
|
|
if($state->SortColumn(null) == $columnField && $state->SortDirection('asc') == 'asc') {
|
2011-12-06 01:56:24 +01:00
|
|
|
$dir = 'desc';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$field = GridField_FormAction::create(
|
|
|
|
$gridField,
|
|
|
|
'SetOrder'.$fieldName,
|
|
|
|
$title,
|
|
|
|
"sort$dir",
|
|
|
|
array('SortColumn' => $columnField)
|
2016-07-01 03:37:29 +02:00
|
|
|
)->addExtraClass('grid-field__sort');
|
2011-12-06 01:56:24 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($state->SortColumn(null) == $columnField){
|
2011-12-06 01:56:24 +01:00
|
|
|
$field->addExtraClass('ss-gridfield-sorted');
|
2012-02-29 02:34:06 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($state->SortDirection('asc') == 'asc') {
|
2012-02-29 02:34:06 +01:00
|
|
|
$field->addExtraClass('ss-gridfield-sorted-asc');
|
2016-08-19 00:51:35 +02:00
|
|
|
} else {
|
2012-02-29 02:34:06 +01:00
|
|
|
$field->addExtraClass('ss-gridfield-sorted-desc');
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
if($currentColumn == count($columns)
|
2016-08-19 00:51:35 +02:00
|
|
|
&& $gridField->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldFilterHeader')){
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2014-02-19 22:17:52 +01:00
|
|
|
$field = new LiteralField($fieldName,
|
2016-07-01 03:37:29 +02:00
|
|
|
'<button type="button" name="showFilter" class="btn font-icon-search btn--no-text btn--icon-large grid-field__filter-open"></button>');
|
2012-09-26 23:34:00 +02:00
|
|
|
} else {
|
2014-02-19 22:17:52 +01:00
|
|
|
$field = new LiteralField($fieldName, '<span class="non-sortable">' . $title . '</span>');
|
2012-05-16 02:18:23 +02:00
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
$forTemplate->Fields->push($field);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
$template = SSViewer::get_templates_by_class($this, '_Row', __CLASS__);
|
2011-12-06 01:56:24 +01:00
|
|
|
return array(
|
2016-08-23 04:32:26 +02:00
|
|
|
'header' => $forTemplate->renderWith($template),
|
2011-12-06 01:56:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getActions($gridField) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->checkDataType($gridField->getList())) {
|
|
|
|
return [];
|
|
|
|
}
|
2012-03-09 02:32:16 +01:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
return array('sortasc', 'sortdesc');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-23 22:59:06 +02:00
|
|
|
/**
|
2016-01-06 00:34:58 +01:00
|
|
|
* Returns the manipulated (sorted) DataList. Field names will simply add an
|
2014-08-18 05:39:42 +02:00
|
|
|
* 'ORDER BY' clause, relation names will add appropriate joins to the
|
|
|
|
* {@link DataQuery} first.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param GridField $gridField
|
|
|
|
* @param SS_List $dataList
|
2013-10-23 22:59:06 +02:00
|
|
|
* @return SS_List
|
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->checkDataType($dataList)) {
|
|
|
|
return $dataList;
|
|
|
|
}
|
2012-03-09 02:32:16 +01:00
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
/** @var Sortable $dataList */
|
2011-12-06 01:56:24 +01:00
|
|
|
$state = $gridField->State->GridFieldSortableHeader;
|
2013-12-06 15:44:24 +01:00
|
|
|
if ($state->SortColumn == "") {
|
2011-12-06 01:56:24 +01:00
|
|
|
return $dataList;
|
|
|
|
}
|
2013-10-23 22:59:06 +02:00
|
|
|
|
2015-09-17 00:19:03 +02:00
|
|
|
return $dataList->sort($state->SortColumn, $state->SortDirection('asc'));
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|