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:
parent
cc7eab6746
commit
403f7016ca
|
@ -60,6 +60,20 @@ class PivotTable
|
|||
*/
|
||||
protected $yAxisFilter;
|
||||
|
||||
/**
|
||||
* X-axis header
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $xAxisHeader;
|
||||
|
||||
/**
|
||||
* Y-axis header
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $yAxisHeader;
|
||||
|
||||
/**
|
||||
* Create a new pivot table
|
||||
*
|
||||
|
@ -100,6 +114,56 @@ class PivotTable
|
|||
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
|
||||
*
|
||||
|
@ -132,7 +196,7 @@ class PivotTable
|
|||
if ($this->xAxisQuery === null) {
|
||||
$this->xAxisQuery = clone $this->baseQuery;
|
||||
$this->xAxisQuery->group($this->xAxisColumn);
|
||||
$this->xAxisQuery->columns(array($this->xAxisColumn));
|
||||
$this->xAxisQuery->columns(array($this->xAxisColumn, $this->getXAxisHeader()));
|
||||
$this->xAxisQuery->setUseSubqueryCount();
|
||||
|
||||
if ($this->xAxisFilter !== null) {
|
||||
|
@ -157,7 +221,7 @@ class PivotTable
|
|||
if ($this->yAxisQuery === null) {
|
||||
$this->yAxisQuery = clone $this->baseQuery;
|
||||
$this->yAxisQuery->group($this->yAxisColumn);
|
||||
$this->yAxisQuery->columns(array($this->yAxisColumn));
|
||||
$this->yAxisQuery->columns(array($this->yAxisColumn, $this->getYAxisHeader()));
|
||||
$this->yAxisQuery->setUseSubqueryCount();
|
||||
|
||||
if ($this->yAxisFilter !== null) {
|
||||
|
@ -168,7 +232,6 @@ class PivotTable
|
|||
$this->yAxisQuery->order($this->yAxisColumn, 'asc');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->yAxisQuery;
|
||||
}
|
||||
|
||||
|
@ -245,33 +308,39 @@ class PivotTable
|
|||
($this->xAxisFilter === null && $this->yAxisFilter === null)
|
||||
|| ($this->xAxisFilter !== null && $this->yAxisFilter !== null)
|
||||
) {
|
||||
$xAxis = $this->queryXAxis()->fetchColumn();
|
||||
$yAxis = $this->queryYAxis()->fetchColumn();
|
||||
$xAxis = $this->queryXAxis()->fetchPairs();
|
||||
$yAxis = $this->queryYAxis()->fetchPairs();
|
||||
} else {
|
||||
if ($this->xAxisFilter !== null) {
|
||||
$xAxis = $this->queryXAxis()->fetchColumn();
|
||||
$yAxis = $this->queryYAxis()->where($this->xAxisColumn, $xAxis)->fetchColumn();
|
||||
$xAxis = $this->queryXAxis()->fetchPairs();
|
||||
$yAxis = $this->queryYAxis()->where($this->xAxisColumn, $xAxis)->fetchPairs();
|
||||
} else { // $this->yAxisFilter !== null
|
||||
$yAxis = $this->queryYAxis()->fetchColumn();
|
||||
$xAxis = $this->queryXAxis()->where($this->yAxisColumn, $yAxis)->fetchColumn();
|
||||
$yAxis = $this->queryYAxis()->fetchPairs();
|
||||
$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();
|
||||
if (!empty($xAxis) && !empty($yAxis)) {
|
||||
$this->baseQuery->where($this->xAxisColumn, $xAxis)->where($this->yAxisColumn, $yAxis);
|
||||
|
||||
foreach ($yAxis as $yLabel) {
|
||||
foreach ($xAxis as $xLabel) {
|
||||
$pivot[$yLabel][$xLabel] = null;
|
||||
foreach ($yAxisKeys as $yAxisKey) {
|
||||
foreach ($xAxisKeys as $xAxisKey) {
|
||||
$pivotData[$yAxisKey][$xAxisKey] = null;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->baseQuery as $row) {
|
||||
$pivot[$row->{$this->yAxisColumn}][$row->{$this->xAxisColumn}] = $row;
|
||||
$pivotData[$row->{$this->yAxisColumn}][$row->{$this->xAxisColumn}] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
return $pivot;
|
||||
return array($pivotData, $pivotHeader);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue