SortBox: Add support for sort defaults provided by the controller action
This commit is contained in:
parent
c57d1212eb
commit
e7399c312f
|
@ -100,16 +100,17 @@ class Controller extends ModuleActionController
|
||||||
*
|
*
|
||||||
* The widget is set on the `sortBox' view property only if the current view has not been requested as compact
|
* The widget is set on the `sortBox' view property only if the current view has not been requested as compact
|
||||||
*
|
*
|
||||||
* @param array $columns An array containing the sort columns, with the
|
* @param array $columns An array containing the sort columns, with the
|
||||||
* submit value as the key and the label as the value
|
* submit value as the key and the label as the value
|
||||||
* @param Sortable $query Query to apply the user chosen sort rules on
|
* @param Sortable $query Query to apply the user chosen sort rules on
|
||||||
|
* @param array $defaults An array containing default sort directions for specific columns
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
protected function setupSortControl(array $columns, Sortable $query = null)
|
protected function setupSortControl(array $columns, Sortable $query = null, array $defaults = null)
|
||||||
{
|
{
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
$sortBox = SortBox::create('sortbox-' . $request->getActionName(), $columns);
|
$sortBox = SortBox::create('sortbox-' . $request->getActionName(), $columns, $defaults);
|
||||||
$sortBox->setRequest($request);
|
$sortBox->setRequest($request);
|
||||||
|
|
||||||
if ($query) {
|
if ($query) {
|
||||||
|
|
|
@ -33,6 +33,15 @@ class SortBox extends AbstractWidget
|
||||||
*/
|
*/
|
||||||
protected $sortFields;
|
protected $sortFields;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An array containing default sort directions for specific columns
|
||||||
|
*
|
||||||
|
* The first entry will be used as default sort column.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $sortDefaults;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name used to uniquely identfy the forms being created
|
* The name used to uniquely identfy the forms being created
|
||||||
*
|
*
|
||||||
|
@ -59,11 +68,13 @@ class SortBox extends AbstractWidget
|
||||||
*
|
*
|
||||||
* @param string $name The name for the SortBox
|
* @param string $name The name for the SortBox
|
||||||
* @param array $sortFields An array containing the columns and their labels to be displayed in the SortBox
|
* @param array $sortFields An array containing the columns and their labels to be displayed in the SortBox
|
||||||
|
* @param array $sortDefaults An array containing default sort directions for specific columns
|
||||||
*/
|
*/
|
||||||
public function __construct($name, array $sortFields)
|
public function __construct($name, array $sortFields, array $sortDefaults = null)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->sortFields = $sortFields;
|
$this->sortFields = $sortFields;
|
||||||
|
$this->sortDefaults = $sortDefaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -71,12 +82,13 @@ class SortBox extends AbstractWidget
|
||||||
*
|
*
|
||||||
* @param string $name The name for the SortBox
|
* @param string $name The name for the SortBox
|
||||||
* @param array $sortFields An array containing the columns and their labels to be displayed in the SortBox
|
* @param array $sortFields An array containing the columns and their labels to be displayed in the SortBox
|
||||||
|
* @param array $sortDefaults An array containing default sort directions for specific columns
|
||||||
*
|
*
|
||||||
* @return SortBox
|
* @return SortBox
|
||||||
*/
|
*/
|
||||||
public static function create($name, array $sortFields)
|
public static function create($name, array $sortFields, array $sortDefaults = null)
|
||||||
{
|
{
|
||||||
return new static($name, $sortFields);
|
return new static($name, $sortFields, $sortDefaults);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -105,30 +117,6 @@ class SortBox extends AbstractWidget
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply the sort rules from the given or current request on the query
|
|
||||||
*
|
|
||||||
* @param Request $request
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function handleRequest(Request $request = null)
|
|
||||||
{
|
|
||||||
if ($this->query !== null) {
|
|
||||||
if ($request === null) {
|
|
||||||
$request = Icinga::app()->getRequest();
|
|
||||||
}
|
|
||||||
if (! ($sort = $request->getParam('sort'))) {
|
|
||||||
list($sort, $dir) = $this->getSortDefaults();
|
|
||||||
} else {
|
|
||||||
list($_, $dir) = $this->getSortDefaults($sort);
|
|
||||||
}
|
|
||||||
$this->query->order($sort, $request->getParam('dir', $dir));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the default sort rule for the query
|
* Return the default sort rule for the query
|
||||||
*
|
*
|
||||||
|
@ -139,7 +127,14 @@ class SortBox extends AbstractWidget
|
||||||
protected function getSortDefaults($column = null)
|
protected function getSortDefaults($column = null)
|
||||||
{
|
{
|
||||||
$direction = null;
|
$direction = null;
|
||||||
if ($this->query !== null && $this->query instanceof SortRules) {
|
if (! empty($this->sortDefaults) && ($column === null || isset($this->sortDefaults[$column]))) {
|
||||||
|
if ($column === null) {
|
||||||
|
reset($this->sortDefaults);
|
||||||
|
$column = key($this->sortDefaults);
|
||||||
|
}
|
||||||
|
|
||||||
|
$direction = $this->sortDefaults[$column];
|
||||||
|
} elseif ($this->query !== null && $this->query instanceof SortRules) {
|
||||||
$sortRules = $this->query->getSortRules();
|
$sortRules = $this->query->getSortRules();
|
||||||
if ($column === null) {
|
if ($column === null) {
|
||||||
$column = key($sortRules);
|
$column = key($sortRules);
|
||||||
|
@ -152,9 +147,36 @@ class SortBox extends AbstractWidget
|
||||||
reset($this->sortFields);
|
reset($this->sortFields);
|
||||||
$column = key($this->sortFields);
|
$column = key($this->sortFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
return array($column, $direction);
|
return array($column, $direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply the sort rules from the given or current request on the query
|
||||||
|
*
|
||||||
|
* @param Request $request
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function handleRequest(Request $request = null)
|
||||||
|
{
|
||||||
|
if ($this->query !== null) {
|
||||||
|
if ($request === null) {
|
||||||
|
$request = Icinga::app()->getRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! ($sort = $request->getParam('sort'))) {
|
||||||
|
list($sort, $dir) = $this->getSortDefaults();
|
||||||
|
} else {
|
||||||
|
list($_, $dir) = $this->getSortDefaults($sort);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->query->order($sort, $request->getParam('dir', $dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render this SortBox as HTML
|
* Render this SortBox as HTML
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue