Data views: Implement `Sortable' interface

Because the `ListController' should only operate on data views,
data views should be a proxy to their underlying queries.

The methods `DataView::fromRequest()' and `DataView::sort()' are from now on deprecated
because they are relaced by `$backend->select()->from($viewName)' and `DataView::order()' respectively.
This commit is contained in:
Eric Lippmann 2014-05-07 10:19:00 +02:00
parent ecbd9b8616
commit 5b895ab038
1 changed files with 41 additions and 13 deletions

View File

@ -32,24 +32,16 @@ namespace Icinga\Module\Monitoring\DataView;
use Icinga\Data\BaseQuery; use Icinga\Data\BaseQuery;
use Icinga\Data\Browsable; use Icinga\Data\Browsable;
use Icinga\Data\PivotTable; use Icinga\Data\PivotTable;
use Icinga\Data\Sortable;
use Icinga\Filter\Filterable; use Icinga\Filter\Filterable;
use Icinga\Module\Monitoring\Filter\UrlViewFilter; use Icinga\Module\Monitoring\Filter\UrlViewFilter;
use Icinga\Web\Request; use Icinga\Web\Request;
/** /**
* A read-only view of an underlying Query * A read-only view of an underlying query
*/ */
abstract class DataView implements Browsable, Filterable abstract class DataView implements Browsable, Filterable, Sortable
{ {
/**
* Sort in ascending order, default
*/
const SORT_ASC = BaseQuery::SORT_ASC;
/**
* Sort in reverse order
*/
const SORT_DESC = BaseQuery::SORT_DESC;
/** /**
* The query used to populate the view * The query used to populate the view
* *
@ -97,6 +89,7 @@ abstract class DataView implements Browsable, Filterable
* @param array $columns * @param array $columns
* *
* @return static * @return static
* @deprecated Use $backend->select()->from($viewName) instead
*/ */
public static function fromRequest($request, array $columns = null) public static function fromRequest($request, array $columns = null)
{ {
@ -186,11 +179,13 @@ abstract class DataView implements Browsable, Filterable
/** /**
* Sort the rows, according to the specified sort column and order * Sort the rows, according to the specified sort column and order
* *
* @param string $column Sort column * @param string $column Sort column
* @param int $order Sort order, one of the SORT_ constants * @param int $order Sort order, one of the SORT_ constants
* *
* @return self
* @see DataView::SORT_ASC * @see DataView::SORT_ASC
* @see DataView::SORT_DESC * @see DataView::SORT_DESC
* @deprecated Use DataView::order() instead
*/ */
public function sort($column = null, $order = null) public function sort($column = null, $order = null)
{ {
@ -235,6 +230,39 @@ abstract class DataView implements Browsable, Filterable
return null; return null;
} }
/**
* Sort result set either by the given column (and direction) or the sort defaults
*
* @param string $column
* @param string $direction
*
* @return self
*/
public function order($column = null, $direction = null)
{
return $this->sort($column, $direction !== null ? strtoupper($direction) : null);
}
/**
* Whether an order is set
*
* @return bool
*/
public function hasOrder()
{
return $this->query->hasOrder();
}
/**
* Get the order if any
*
* @return array|null
*/
public function getOrder()
{
return $this->query->getOrder();
}
public function getMappedField($field) public function getMappedField($field)
{ {
return $this->query->getMappedField($field); return $this->query->getMappedField($field);