From f2f1e12b8ea615651939ce2e8f98834556a01e41 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 18 Aug 2015 14:17:07 +0200 Subject: [PATCH] Let PivotTable implement Sortable refs #9333 --- library/Icinga/Data/PivotTable.php | 76 +++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 21 deletions(-) diff --git a/library/Icinga/Data/PivotTable.php b/library/Icinga/Data/PivotTable.php index 3a2941bfb..b3b8c7a79 100644 --- a/library/Icinga/Data/PivotTable.php +++ b/library/Icinga/Data/PivotTable.php @@ -8,7 +8,7 @@ use Icinga\Application\Icinga; use Icinga\Web\Paginator\Adapter\QueryAdapter; use Zend_Paginator; -class PivotTable +class PivotTable implements Sortable { /** * The query to fetch as pivot table @@ -17,20 +17,6 @@ class PivotTable */ protected $baseQuery; - /** - * The query to fetch the x axis labels - * - * @var SimpleQuery - */ - protected $xAxisQuery; - - /** - * The query to fetch the y axis labels - * - * @var SimpleQuery - */ - protected $yAxisQuery; - /** * The column that contains the labels for the x axis * @@ -59,6 +45,27 @@ class PivotTable */ protected $yAxisFilter; + /** + * The query to fetch the x axis labels + * + * @var SimpleQuery + */ + protected $xAxisQuery; + + /** + * The query to fetch the y axis labels + * + * @var SimpleQuery + */ + protected $yAxisQuery; + + /** + * Column for sorting the result set + * + * @var array + */ + protected $order = array(); + /** * Create a new pivot table * @@ -73,6 +80,31 @@ class PivotTable $this->yAxisColumn = $yAxisColumn; } + /** + * {@inheritdoc} + */ + public function getOrder() + { + return $this->order; + } + + /** + * {@inheritdoc} + */ + public function hasOrder() + { + return ! empty($this->order); + } + + /** + * {@inheritdoc} + */ + public function order($field, $direction = null) + { + $this->order[$field] = $direction; + return $this; + } + /** * Set the filter to apply on the query for the x axis * @@ -137,9 +169,10 @@ class PivotTable $this->xAxisQuery->addFilter($this->xAxisFilter); } - if (! $this->xAxisQuery->hasOrder($this->xAxisColumn)) { - $this->xAxisQuery->order($this->xAxisColumn, 'asc'); - } + $this->xAxisQuery->order( + $this->xAxisColumn, + isset($this->order[$this->xAxisColumn]) ? $this->order[$this->xAxisColumn] : self::SORT_ASC + ); } return $this->xAxisQuery; @@ -161,9 +194,10 @@ class PivotTable $this->yAxisQuery->addFilter($this->yAxisFilter); } - if (! $this->yAxisQuery->hasOrder($this->yAxisColumn)) { - $this->yAxisQuery->order($this->yAxisColumn, 'asc'); - } + $this->yAxisQuery->order( + $this->yAxisColumn, + isset($this->order[$this->yAxisColumn]) ? $this->order[$this->yAxisColumn] : self::SORT_ASC + ); } return $this->yAxisQuery;