GridFieldOrderableRows: Support extra sort fields

This change allows for the specification of extra sort fields that are to be
applied before the user-defined sort field's order. This is useful in
scenarios where a GridField is used with one or more "fixed" columns which
eg. categorize the entries and whose order is important but the user should
still be allowed to modify the order within a category.

This change affects the PHP code only, therefore the Javascript code will
still let the user drag rows into foreign "categories". However, after
dropping it the GridField will reload and the extra sort order will be
enforced again.
This commit is contained in:
Pieter Hollants 2015-01-18 19:40:11 +01:00
parent 9ff5de80d4
commit 79972af8a2

View File

@ -25,6 +25,14 @@ class GridFieldOrderableRows extends RequestHandler implements
*/
protected $sortField;
/**
* Extra sort fields to apply before the sort field.
*
* @see setExtraSortFields()
* @var string|array
*/
protected $extraSortFields = null;
/**
* @param string $sortField
*/
@ -50,6 +58,24 @@ class GridFieldOrderableRows extends RequestHandler implements
return $this;
}
/**
* @return string|array
*/
public function getExtraSortFields() {
return $this->extraSortFields;
}
/**
* Sets extra sort fields to apply before the sort field.
*
* @param string|array $fields
* @return GridFieldOrderableRows $this
*/
public function setExtraSortFields($fields) {
$this->extraSortFields = $fields;
return $this;
}
/**
* Gets the table which contains the sort field.
*
@ -128,7 +154,18 @@ class GridFieldOrderableRows extends RequestHandler implements
$state->GridFieldOrderableRows->enabled = !$sorted;
if(!$sorted) {
return $list->sort($this->getSortField());
$sortterm = '';
if ($this->extraSortFields) {
if (is_array($this->extraSortFields)) {
foreach($this->extraSortFields as $col => $dir) {
$sortterm .= "$col $dir, ";
}
} else {
$sortterm = $this->extraSortFields.', ';
}
}
$sortterm .= $this->getSortField();
return $list->sort($sortterm);
} else {
return $list;
}
@ -150,7 +187,18 @@ class GridFieldOrderableRows extends RequestHandler implements
$this->httpError(400);
}
$items = $list->byIDs($ids)->sort($field);
$sortterm = '';
if ($this->extraSortFields) {
if (is_array($this->extraSortFields)) {
foreach($this->extraSortFields as $col => $dir) {
$sortterm .= "$col $dir, ";
}
} else {
$sortterm = $this->extraSortFields.', ';
}
}
$sortterm .= $field;
$items = $list->byIDs($ids)->sort($sortterm);
// Ensure that each provided ID corresponded to an actual object.
if(count($items) != count($ids)) {