From 79972af8a219033ac511c7450cd3b8ece59ea009 Mon Sep 17 00:00:00 2001 From: Pieter Hollants Date: Sun, 18 Jan 2015 19:40:11 +0100 Subject: [PATCH] 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. --- code/GridFieldOrderableRows.php | 52 +++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/code/GridFieldOrderableRows.php b/code/GridFieldOrderableRows.php index 0202bbc..661f044 100755 --- a/code/GridFieldOrderableRows.php +++ b/code/GridFieldOrderableRows.php @@ -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)) {