icingaweb2/library/Icinga/Data/PivotTable.php

171 lines
3.9 KiB
PHP
Raw Normal View History

<?php
2014-03-12 14:41:17 +01:00
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Data;
2014-03-12 14:41:17 +01:00
use \stdClass;
use Icinga\Data\BaseQuery;
class PivotTable
{
/**
* The query to fetch as pivot table
*
* @var BaseQuery
*/
protected $query;
/**
* The column to use for the x axis
*
* @var string
*/
2014-03-12 14:41:17 +01:00
protected $xAxisColumn;
/**
* The column to use for the y axis
*
* @var string
*/
2014-03-12 14:41:17 +01:00
protected $yAxisColumn;
protected $limit;
protected $offset;
protected $verticalLimit;
protected $horizontalLimit;
2014-03-12 14:41:17 +01:00
/**
* Create a new pivot table
*
* @param BaseQuery $query The query to fetch as pivot table
* @param string $xAxisColumn The column to use for the x axis
* @param string $yAxisColumn The column to use for the y axis
*/
public function __construct(BaseQuery $query, $xAxisColumn, $yAxisColumn)
{
$this->query = $query;
2014-03-12 14:41:17 +01:00
$this->xAxisColumn = $xAxisColumn;
$this->yAxisColumn = $yAxisColumn;
}
public function limit($limit = null, $offset = null)
{
$this->limit = $limit;
$this->offset = $offset;
return $this;
}
public function getLimit()
{
if ($this->limit === null) {
return 20;
}
return $this->limit;
}
public function getOffset()
{
if ($this->limit === null) {
return 20;
}
return $this->offset;
}
public function verticalLimit($limit = null, $offset = null)
{
// TODO: Trigger limit by calling $this->limit()?
if ($limit === null) {
$limit = $this->getLimit();
}
if ($offset === null) {
$offset = $this->getOffset();
}
$this->verticalLimit = $limit;
$this->verticalOffset = $offset;
return $this;
}
public function paginateVertical($limit = null, $offset = null)
{
$this->verticalLimit($limit, $offset);
return Paginator($this);
}
public function getVerticalLimit()
{
if ($this->verticalLimit === null) {
return 20;
}
return $this->verticalLimit;
}
public function getVerticalOffset()
{
if ($this->verticalLimit === null) {
return 20;
}
return $this->verticalOffset;
}
/**
2014-03-12 14:41:17 +01:00
* Fetch the values to label the x axis with
*/
2014-03-12 14:41:17 +01:00
protected function fetchXAxis()
{
$query = clone $this->query;
$query->setColumns(array($this->xAxisColumn));
2014-03-12 14:41:17 +01:00
return $query->fetchColumn();
}
/**
* Fetch the values to label the y axis with
*/
protected function fetchYAxis()
{
$query = clone $this->query;
$query->setColumns(array($this->yAxisColumn));
2014-03-12 14:41:17 +01:00
return $query->fetchColumn();
}
/**
* Return the pivot table as array
*
* @return array
*/
public function toArray()
{
$xAxis = $this->fetchXAxis();
$yAxis = $this->fetchYAxis();
$this->query->where($this->xAxisColumn, $xAxis)->where($this->yAxisColumn, $yAxis);
if (!$this->query->hasOrder()) {
$this->query->order($this->xAxisColumn)->order($this->yAxisColumn);
}
2014-03-12 14:41:17 +01:00
$emptyrow = new stdClass();
foreach ($this->query->getColumns() as $col) {
$emptyrow->{$col} = null;
}
2014-03-12 14:41:17 +01:00
$pivot = array();
2014-03-12 14:41:17 +01:00
foreach ($xAxis as $x) {
foreach ($yAxis as $y) {
$row = clone($emptyrow);
2014-03-12 14:41:17 +01:00
$row->{$this->xAxisColumn} = $x;
$row->{$this->yAxisColumn} = $y;
$pivot[$y][$x] = $row;
}
}
foreach ($this->query->fetchAll() as $row) {
2014-03-12 14:41:17 +01:00
$pivot[$row->{$this->yAxisColumn}][$row->{$this->xAxisColumn}] = $row;
}
2014-03-12 14:41:17 +01:00
return $pivot;
}
}