silverstripe-framework/forms/gridfield/GridFieldSortableHeader.php
Damian Mooyman e6b877df27 Merge remote-tracking branch 'origin/3'
# Conflicts:
#	control/Director.php
#	control/HTTP.php
#	core/startup/ParameterConfirmationToken.php
#	docs/en/00_Getting_Started/01_Installation/05_Common_Problems.md
#	docs/en/00_Getting_Started/04_Directory_Structure.md
#	docs/en/00_Getting_Started/05_Coding_Conventions.md
#	docs/en/01_Tutorials/01_Building_A_Basic_Site.md
#	docs/en/01_Tutorials/02_Extending_A_Basic_Site.md
#	docs/en/01_Tutorials/03_Forms.md
#	docs/en/01_Tutorials/04_Site_Search.md
#	docs/en/01_Tutorials/05_Dataobject_Relationship_Management.md
#	docs/en/02_Developer_Guides/12_Search/01_Searchcontext.md
#	docs/en/02_Developer_Guides/13_i18n/index.md
#	docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/06_Javascript_Development.md
#	docs/en/03_Upgrading/index.md
#	docs/en/changelogs/index.md
#	docs/en/howto/customize-cms-menu.md
#	docs/en/howto/navigation-menu.md
#	docs/en/index.md
#	docs/en/installation/index.md
#	docs/en/installation/windows-manual-iis-6.md
#	docs/en/misc/contributing/code.md
#	docs/en/misc/contributing/issues.md
#	docs/en/misc/module-release-process.md
#	docs/en/reference/dataobject.md
#	docs/en/reference/execution-pipeline.md
#	docs/en/reference/grid-field.md
#	docs/en/reference/modeladmin.md
#	docs/en/reference/rssfeed.md
#	docs/en/reference/templates.md
#	docs/en/topics/commandline.md
#	docs/en/topics/debugging.md
#	docs/en/topics/email.md
#	docs/en/topics/forms.md
#	docs/en/topics/index.md
#	docs/en/topics/module-development.md
#	docs/en/topics/modules.md
#	docs/en/topics/page-type-templates.md
#	docs/en/topics/page-types.md
#	docs/en/topics/search.md
#	docs/en/topics/testing/index.md
#	docs/en/topics/testing/testing-guide-troubleshooting.md
#	docs/en/topics/theme-development.md
#	docs/en/tutorials/1-building-a-basic-site.md
#	docs/en/tutorials/2-extending-a-basic-site.md
#	docs/en/tutorials/3-forms.md
#	docs/en/tutorials/4-site-search.md
#	docs/en/tutorials/5-dataobject-relationship-management.md
#	docs/en/tutorials/building-a-basic-site.md
#	docs/en/tutorials/dataobject-relationship-management.md
#	docs/en/tutorials/extending-a-basic-site.md
#	docs/en/tutorials/forms.md
#	docs/en/tutorials/index.md
#	docs/en/tutorials/site-search.md
#	main.php
#	model/SQLQuery.php
#	security/ChangePasswordForm.php
#	security/MemberLoginForm.php
#	tests/control/ControllerTest.php
#	tests/core/startup/ParameterConfirmationTokenTest.php
#	tests/model/SQLQueryTest.php
#	tests/security/SecurityTest.php
#	tests/view/SSViewerTest.php
#	view/SSTemplateParser.php
#	view/SSTemplateParser.php.inc
#	view/SSViewer.php
2016-01-20 13:16:27 +13:00

214 lines
6.3 KiB
PHP

<?php
/**
* GridFieldSortableHeader adds column headers to a {@link GridField} that can
* also sort the columns.
*
* @see GridField
*
* @package forms
* @subpackage fields-gridfield
*/
class GridFieldSortableHeader implements GridField_HTMLProvider, GridField_DataManipulator, GridField_ActionProvider {
/**
* See {@link setThrowExceptionOnBadDataType()}
*/
protected $throwExceptionOnBadDataType = true;
/**
* @var array
*/
public $fieldSorting = array();
/**
* Determine what happens when this component is used with a list that isn't {@link SS_Filterable}.
*
* - true: An exception is thrown
* - 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.
*/
public function setThrowExceptionOnBadDataType($throwExceptionOnBadDataType) {
$this->throwExceptionOnBadDataType = $throwExceptionOnBadDataType;
}
/**
* See {@link setThrowExceptionOnBadDataType()}
*/
public function getThrowExceptionOnBadDataType() {
return $this->throwExceptionOnBadDataType;
}
/**
* 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;
}
}
/**
* 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;
}
/**
* Returns the header row providing titles with sort buttons
*/
public function getHTMLFragments($gridField) {
if(!$this->checkDataType($gridField->getList())) return;
$forTemplate = new ArrayData(array());
$forTemplate->Fields = new ArrayList;
$state = $gridField->State->GridFieldSortableHeader;
$columns = $gridField->getColumns();
$currentColumn = 0;
$list = $gridField->getList();
foreach($columns as $columnField) {
$currentColumn++;
$metadata = $gridField->getColumnMetadata($columnField);
$fieldName = str_replace('.', '-', $columnField);
$title = $metadata['title'];
if(isset($this->fieldSorting[$columnField]) && $this->fieldSorting[$columnField]) {
$columnField = $this->fieldSorting[$columnField];
}
$allowSort = ($title && $list->canSortBy($columnField));
if(!$allowSort && strpos($columnField, '.') !== false) {
// we have a relation column with dot notation
// @see DataObject::relField for approximation
$parts = explode('.', $columnField);
$tmpItem = singleton($list->dataClass());
for($idx = 0; $idx < sizeof($parts); $idx++) {
$methodName = $parts[$idx];
if($tmpItem instanceof SS_List) {
// It's impossible to sort on a HasManyList/ManyManyList
break;
} elseif(method_exists($tmpItem, 'hasMethod') && $tmpItem->hasMethod($methodName)) {
// The part is a relation name, so get the object/list from it
$tmpItem = $tmpItem->$methodName();
} elseif($tmpItem instanceof DataObject && $tmpItem->hasDatabaseField($methodName)) {
// Else, if we've found a database field at the end of the chain, we can sort on it.
// 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;
}
}
}
if($allowSort) {
$dir = 'asc';
if($state->SortColumn(null) == $columnField && $state->SortDirection('asc') == 'asc') {
$dir = 'desc';
}
$field = Object::create(
'GridField_FormAction', $gridField, 'SetOrder'.$fieldName, $title,
"sort$dir", array('SortColumn' => $columnField)
)->addExtraClass('ss-gridfield-sort');
if($state->SortColumn(null) == $columnField){
$field->addExtraClass('ss-gridfield-sorted');
if($state->SortDirection('asc') == 'asc')
$field->addExtraClass('ss-gridfield-sorted-asc');
else
$field->addExtraClass('ss-gridfield-sorted-desc');
}
} else {
if($currentColumn == count($columns)
&& $gridField->getConfig()->getComponentByType('GridFieldFilterHeader')){
$field = new LiteralField($fieldName,
'<button type="button" name="showFilter" class="ss-gridfield-button-filter trigger"></button>');
} else {
$field = new LiteralField($fieldName, '<span class="non-sortable">' . $title . '</span>');
}
}
$forTemplate->Fields->push($field);
}
return array(
'header' => $forTemplate->renderWith('GridFieldSortableHeader_Row'),
);
}
/**
*
* @param GridField $gridField
* @return array
*/
public function getActions($gridField) {
if(!$this->checkDataType($gridField->getList())) return;
return array('sortasc', 'sortdesc');
}
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
if(!$this->checkDataType($gridField->getList())) return;
$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;
}
}
/**
* Returns the manipulated (sorted) DataList. Field names will simply add an
* 'ORDER BY' clause, relation names will add appropriate joins to the
* {@link DataQuery} first.
*
* @param GridField
* @param SS_List
* @return SS_List
*/
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
if(!$this->checkDataType($dataList)) return $dataList;
$state = $gridField->State->GridFieldSortableHeader;
if ($state->SortColumn == "") {
return $dataList;
}
return $dataList->sort($state->SortColumn, $state->SortDirection('asc'));
}
}