diff --git a/code/GridFieldOrderableRows.php b/code/GridFieldOrderableRows.php index 0202bbc..3440a53 100755 --- a/code/GridFieldOrderableRows.php +++ b/code/GridFieldOrderableRows.php @@ -25,6 +25,22 @@ class GridFieldOrderableRows extends RequestHandler implements */ protected $sortField; + /** + * Extra sort fields to apply before the sort field. + * + * @see setExtraSortFields() + * @var string|array + */ + protected $extraSortFields = null; + + /** + * The number of the column containing the reorder handles + * + * @see setReorderColumnNumber() + * @var integer + */ + protected $reorderColumnNumber = 0; + /** * @param string $sortField */ @@ -50,6 +66,42 @@ 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; + } + + /** + * @return integer + */ + public function getReorderColumnNumber() { + return $this->reorderColumnNumber; + } + + /** + * Sets the number of the column containing the reorder handles. + * + * @param integer $colno + * @return GridFieldOrderableRows $this + */ + public function setReorderColumnNumber($colno) { + $this->reorderColumnNumber = $colno; + return $this; + } + /** * Gets the table which contains the sort field. * @@ -99,7 +151,7 @@ class GridFieldOrderableRows extends RequestHandler implements public function augmentColumns($grid, &$cols) { if(!in_array('Reorder', $cols) && $grid->getState()->GridFieldOrderableRows->enabled) { - array_unshift($cols, 'Reorder'); + array_splice($cols, $this->reorderColumnNumber, 0, 'Reorder'); } } @@ -128,7 +180,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 +213,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)) {