lib: Allow to set axis header columns in the pivot table

PivotTable::toArray() now returns the pivot data and the pivot header.

refs #9538
This commit is contained in:
Eric Lippmann 2015-08-25 14:46:23 +02:00
parent cc7eab6746
commit 403f7016ca

View File

@ -60,6 +60,20 @@ class PivotTable
*/ */
protected $yAxisFilter; protected $yAxisFilter;
/**
* X-axis header
*
* @var string|null
*/
protected $xAxisHeader;
/**
* Y-axis header
*
* @var string|null
*/
protected $yAxisHeader;
/** /**
* Create a new pivot table * Create a new pivot table
* *
@ -100,6 +114,56 @@ class PivotTable
return $this; return $this;
} }
/**
* Get the x-axis header
*
* Defaults to {@link $xAxisColumn} in case no x-axis header has been set using {@link setXAxisHeader()}
*
* @return string
*/
public function getXAxisHeader()
{
return $this->xAxisHeader !== null ? $this->xAxisHeader : $this->xAxisColumn;
}
/**
* Set the x-axis header
*
* @param string $xAxisHeader
*
* @return $this
*/
public function setXAxisHeader($xAxisHeader)
{
$this->xAxisHeader = (string) $xAxisHeader;
return $this;
}
/**
* Get the y-axis header
*
* Defaults to {@link $yAxisColumn} in case no x-axis header has been set using {@link setYAxisHeader()}
*
* @return string
*/
public function getYAxisHeader()
{
return $this->yAxisHeader !== null ? $this->yAxisHeader : $this->yAxisColumn;
}
/**
* Set the y-axis header
*
* @param string $yAxisHeader
*
* @return $this
*/
public function setYAxisHeader($yAxisHeader)
{
$this->yAxisHeader = (string) $yAxisHeader;
return $this;
}
/** /**
* Return the value for the given request parameter * Return the value for the given request parameter
* *
@ -132,7 +196,7 @@ class PivotTable
if ($this->xAxisQuery === null) { if ($this->xAxisQuery === null) {
$this->xAxisQuery = clone $this->baseQuery; $this->xAxisQuery = clone $this->baseQuery;
$this->xAxisQuery->group($this->xAxisColumn); $this->xAxisQuery->group($this->xAxisColumn);
$this->xAxisQuery->columns(array($this->xAxisColumn)); $this->xAxisQuery->columns(array($this->xAxisColumn, $this->getXAxisHeader()));
$this->xAxisQuery->setUseSubqueryCount(); $this->xAxisQuery->setUseSubqueryCount();
if ($this->xAxisFilter !== null) { if ($this->xAxisFilter !== null) {
@ -157,7 +221,7 @@ class PivotTable
if ($this->yAxisQuery === null) { if ($this->yAxisQuery === null) {
$this->yAxisQuery = clone $this->baseQuery; $this->yAxisQuery = clone $this->baseQuery;
$this->yAxisQuery->group($this->yAxisColumn); $this->yAxisQuery->group($this->yAxisColumn);
$this->yAxisQuery->columns(array($this->yAxisColumn)); $this->yAxisQuery->columns(array($this->yAxisColumn, $this->getYAxisHeader()));
$this->yAxisQuery->setUseSubqueryCount(); $this->yAxisQuery->setUseSubqueryCount();
if ($this->yAxisFilter !== null) { if ($this->yAxisFilter !== null) {
@ -168,7 +232,6 @@ class PivotTable
$this->yAxisQuery->order($this->yAxisColumn, 'asc'); $this->yAxisQuery->order($this->yAxisColumn, 'asc');
} }
} }
return $this->yAxisQuery; return $this->yAxisQuery;
} }
@ -245,33 +308,39 @@ class PivotTable
($this->xAxisFilter === null && $this->yAxisFilter === null) ($this->xAxisFilter === null && $this->yAxisFilter === null)
|| ($this->xAxisFilter !== null && $this->yAxisFilter !== null) || ($this->xAxisFilter !== null && $this->yAxisFilter !== null)
) { ) {
$xAxis = $this->queryXAxis()->fetchColumn(); $xAxis = $this->queryXAxis()->fetchPairs();
$yAxis = $this->queryYAxis()->fetchColumn(); $yAxis = $this->queryYAxis()->fetchPairs();
} else { } else {
if ($this->xAxisFilter !== null) { if ($this->xAxisFilter !== null) {
$xAxis = $this->queryXAxis()->fetchColumn(); $xAxis = $this->queryXAxis()->fetchPairs();
$yAxis = $this->queryYAxis()->where($this->xAxisColumn, $xAxis)->fetchColumn(); $yAxis = $this->queryYAxis()->where($this->xAxisColumn, $xAxis)->fetchPairs();
} else { // $this->yAxisFilter !== null } else { // $this->yAxisFilter !== null
$yAxis = $this->queryYAxis()->fetchColumn(); $yAxis = $this->queryYAxis()->fetchPairs();
$xAxis = $this->queryXAxis()->where($this->yAxisColumn, $yAxis)->fetchColumn(); $xAxis = $this->queryXAxis()->where($this->yAxisColumn, $yAxis)->fetchPairs();
} }
} }
$pivotData = array();
$pivotHeader = array(
'cols' => $xAxis,
'rows' => $yAxis
);
if (! empty($xAxis) && ! empty($yAxis)) {
$xAxisKeys = array_keys($xAxis);
$yAxisKeys = array_keys($yAxis);
$this->baseQuery
->where($this->xAxisColumn, $xAxisKeys)
->where($this->yAxisColumn, $yAxisKeys);
$pivot = array(); foreach ($yAxisKeys as $yAxisKey) {
if (!empty($xAxis) && !empty($yAxis)) { foreach ($xAxisKeys as $xAxisKey) {
$this->baseQuery->where($this->xAxisColumn, $xAxis)->where($this->yAxisColumn, $yAxis); $pivotData[$yAxisKey][$xAxisKey] = null;
foreach ($yAxis as $yLabel) {
foreach ($xAxis as $xLabel) {
$pivot[$yLabel][$xLabel] = null;
} }
} }
foreach ($this->baseQuery as $row) { foreach ($this->baseQuery as $row) {
$pivot[$row->{$this->yAxisColumn}][$row->{$this->xAxisColumn}] = $row; $pivotData[$row->{$this->yAxisColumn}][$row->{$this->xAxisColumn}] = $row;
} }
} }
return array($pivotData, $pivotHeader);
return $pivot;
} }
} }