From f7b35b04f48e70ffda152f956070d1e67aee59cd Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 28 Jul 2015 12:03:50 +0200 Subject: [PATCH] SortBox: Properly apply sort rules and show the user the default refs #6644 --- library/Icinga/Web/Widget/SortBox.php | 55 +++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/library/Icinga/Web/Widget/SortBox.php b/library/Icinga/Web/Widget/SortBox.php index fa80b6c3e..9d0945394 100644 --- a/library/Icinga/Web/Widget/SortBox.php +++ b/library/Icinga/Web/Widget/SortBox.php @@ -3,10 +3,11 @@ namespace Icinga\Web\Widget; +use Icinga\Application\Icinga; +use Icinga\Data\Sortable; +use Icinga\Data\SortRules; use Icinga\Web\Form; use Icinga\Web\Request; -use Icinga\Data\Sortable; -use Icinga\Application\Icinga; /** * SortBox widget @@ -113,13 +114,41 @@ class SortBox extends AbstractWidget if ($request === null) { $request = Icinga::app()->getFrontController()->getRequest(); } - if ($sort = $request->getParam('sort')) { + + if (($sort = $request->getParam('sort'))) { $this->query->order($sort, $request->getParam('dir')); + } elseif (($dir = $request->getParam('dir'))) { + $this->query->order(null, $dir); } } + return $this; } + /** + * Return the default sort rule for the query + * + * @param string $column An optional column + * + * @return array An array of two values: $column, $direction + */ + protected function getSortDefaults($column = null) + { + $direction = null; + if ($this->query !== null && $this->query instanceof SortRules) { + $sortRules = $this->query->getSortRules(); + if ($column === null) { + $column = key($sortRules); + } + + if ($column !== null && isset($sortRules[$column]['order'])) { + $direction = strtoupper($sortRules[$column]['order']) === Sortable::SORT_DESC ? 'desc' : 'asc'; + } + } + + return array($column, $direction); + } + /** * Render this SortBox as HTML * @@ -166,17 +195,29 @@ class SortBox extends AbstractWidget ) ); + $column = null; if ($this->request) { $url = $this->request->getUrl(); if ($url->hasParam('sort')) { - $columnForm->populate(array('sort' => $url->getParam('sort'))); - } + $column = $url->getParam('sort'); - if ($url->hasParam('dir')) { - $orderForm->populate(array('dir' => $url->getParam('dir'))); + if ($url->hasParam('dir')) { + $direction = $url->getParam('dir'); + } else { + list($_, $direction) = $this->getSortDefaults($column); + } + } elseif ($url->hasParam('dir')) { + $direction = $url->getParam('dir'); + list($column, $_) = $this->getSortDefaults(); } } + if ($column === null) { + list($column, $direction) = $this->getSortDefaults(); + } + + $columnForm->populate(array('sort' => $column)); + $orderForm->populate(array('dir' => $direction)); return '
' . $columnForm . $orderForm . '
'; } }