2013-08-21 00:18:31 +02:00
|
|
|
<?php
|
2014-03-12 14:41:17 +01:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-08-21 00:18:31 +02:00
|
|
|
|
|
|
|
namespace Icinga\Data;
|
|
|
|
|
2014-03-12 14:41:17 +01:00
|
|
|
use \stdClass;
|
|
|
|
use Icinga\Data\BaseQuery;
|
|
|
|
|
2013-08-21 00:18:31 +02:00
|
|
|
class PivotTable
|
|
|
|
{
|
2014-03-12 15:20:33 +01:00
|
|
|
/**
|
|
|
|
* The query to fetch as pivot table
|
|
|
|
*
|
|
|
|
* @var BaseQuery
|
|
|
|
*/
|
2013-08-21 00:18:31 +02:00
|
|
|
protected $query;
|
|
|
|
|
2014-03-12 15:20:33 +01:00
|
|
|
/**
|
|
|
|
* The column to use for the x axis
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-03-12 14:41:17 +01:00
|
|
|
protected $xAxisColumn;
|
2013-08-21 00:18:31 +02:00
|
|
|
|
2014-03-12 15:20:33 +01:00
|
|
|
/**
|
|
|
|
* The column to use for the y axis
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-03-12 14:41:17 +01:00
|
|
|
protected $yAxisColumn;
|
2013-08-21 00:18:31 +02:00
|
|
|
|
|
|
|
protected $limit;
|
2014-03-06 13:08:11 +01:00
|
|
|
|
2013-08-21 00:18:31 +02:00
|
|
|
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)
|
2013-08-21 00:18:31 +02:00
|
|
|
{
|
|
|
|
$this->query = $query;
|
2014-03-12 14:41:17 +01:00
|
|
|
$this->xAxisColumn = $xAxisColumn;
|
|
|
|
$this->yAxisColumn = $yAxisColumn;
|
2013-08-21 00:18:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2013-08-21 00:18:31 +02:00
|
|
|
*/
|
2014-03-12 14:41:17 +01:00
|
|
|
protected function fetchXAxis()
|
2013-08-21 00:18:31 +02:00
|
|
|
{
|
2014-03-12 15:20:33 +01:00
|
|
|
$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()
|
|
|
|
{
|
2014-03-12 15:20:33 +01:00
|
|
|
$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);
|
2013-08-21 00:18:31 +02:00
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:17 +01:00
|
|
|
$emptyrow = new stdClass();
|
|
|
|
foreach ($this->query->getColumns() as $col) {
|
|
|
|
$emptyrow->{$col} = null;
|
2013-08-21 00:18:31 +02:00
|
|
|
}
|
2014-03-12 14:41:17 +01:00
|
|
|
|
2013-08-21 00:18:31 +02:00
|
|
|
$pivot = array();
|
2014-03-12 14:41:17 +01:00
|
|
|
foreach ($xAxis as $x) {
|
|
|
|
foreach ($yAxis as $y) {
|
2013-08-21 00:18:31 +02:00
|
|
|
$row = clone($emptyrow);
|
2014-03-12 14:41:17 +01:00
|
|
|
$row->{$this->xAxisColumn} = $x;
|
|
|
|
$row->{$this->yAxisColumn} = $y;
|
2013-08-21 00:18:31 +02:00
|
|
|
$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;
|
2013-08-21 00:18:31 +02:00
|
|
|
}
|
2014-03-12 14:41:17 +01:00
|
|
|
|
2013-08-21 00:18:31 +02:00
|
|
|
return $pivot;
|
|
|
|
}
|
|
|
|
}
|