mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
add column headers
This commit is contained in:
parent
8eba8dca24
commit
685703d863
@ -54,6 +54,8 @@ class CSVFormatter implements FormatterInterface
|
|||||||
|
|
||||||
protected $aggregatorsData;
|
protected $aggregatorsData;
|
||||||
|
|
||||||
|
protected $labels;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @var ExportManager
|
* @var ExportManager
|
||||||
@ -149,6 +151,7 @@ class CSVFormatter implements FormatterInterface
|
|||||||
->getAggregators(array_keys($aggregatorsData)));
|
->getAggregators(array_keys($aggregatorsData)));
|
||||||
$this->exportData = $exportData;
|
$this->exportData = $exportData;
|
||||||
$this->aggregatorsData = $aggregatorsData;
|
$this->aggregatorsData = $aggregatorsData;
|
||||||
|
$this->labels = $this->gatherLabels();
|
||||||
|
|
||||||
$response = new Response();
|
$response = new Response();
|
||||||
$response->setStatusCode(200);
|
$response->setStatusCode(200);
|
||||||
@ -182,11 +185,30 @@ class CSVFormatter implements FormatterInterface
|
|||||||
|
|
||||||
protected function generateContent()
|
protected function generateContent()
|
||||||
{
|
{
|
||||||
$labels = $this->gatherLabels();
|
$rowKeysNb = count($this->getRowHeaders());
|
||||||
$rowHeadersKeys = $this->getRowHeaders();
|
$columnKeysNb = count($this->getColumnHeaders());
|
||||||
$columnHeadersKeys = $this->getColumnHeaders();
|
$resultsKeysNb = count($this->export->getQueryKeys($this->exportData));
|
||||||
$resultsKeys = $this->export->getQueryKeys($this->exportData);
|
$results = $this->getOrderedResults();
|
||||||
print_r($columnHeadersKeys);
|
/* @var $columnHeaders string[] the column headers associations */
|
||||||
|
$columnHeaders = array();
|
||||||
|
/* @var $data string[] the data of the csv file */
|
||||||
|
$contentData = array();
|
||||||
|
$content = array();
|
||||||
|
|
||||||
|
function findColumnPosition(&$columnHeaders, $columnToFind) {
|
||||||
|
$i = 0;
|
||||||
|
foreach($columnHeaders as $set) {
|
||||||
|
if ($set === $columnToFind) {
|
||||||
|
return $i;
|
||||||
|
}
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
//we didn't find it, adding the column
|
||||||
|
$columnHeaders[] = $columnToFind;
|
||||||
|
|
||||||
|
return $i++;
|
||||||
|
}
|
||||||
|
|
||||||
// create a file pointer connected to the output stream
|
// create a file pointer connected to the output stream
|
||||||
$output = fopen('php://output', 'w');
|
$output = fopen('php://output', 'w');
|
||||||
@ -196,61 +218,117 @@ class CSVFormatter implements FormatterInterface
|
|||||||
//blank line
|
//blank line
|
||||||
fputcsv($output, array(""));
|
fputcsv($output, array(""));
|
||||||
|
|
||||||
//headers
|
// iterate on result to : 1. populate row headers, 2. populate column headers, 3. add result
|
||||||
$headerLine = array();
|
foreach ($results as $row) {
|
||||||
foreach($rowHeadersKeys as $headerKey) {
|
$rowHeaders = array_slice($row, 0, $rowKeysNb);
|
||||||
$headerLine[] = array_key_exists('_header', $labels[$headerKey]) ?
|
|
||||||
$labels[$headerKey]['_header'] : '';
|
//first line : we create line and adding them row headers
|
||||||
}
|
if (!isset($line)) {
|
||||||
foreach($resultsKeys as $key) {
|
$line = array_slice($row, 0, $rowKeysNb);
|
||||||
$headerLine[] = array_key_exists('_header', $labels[$key]) ?
|
}
|
||||||
$labels[$key]['_header'] : '';
|
|
||||||
}
|
|
||||||
fputcsv($output, $headerLine);
|
|
||||||
unset($headerLine); //free memory
|
|
||||||
|
|
||||||
$content = array();
|
// do we have to create a new line ? if the rows are equals, continue on the line, else create a next line
|
||||||
// create an array with keys as columnHeadersKeys values, values are empty array
|
if (array_slice($line, 0, $rowKeysNb) !== $rowHeaders) {
|
||||||
$columnHeaders = array_combine($columnHeadersKeys, array_pad(array(),
|
$contentData[] = $line;
|
||||||
count($columnHeadersKeys), array()));
|
$line = array_slice($row, 0, $rowKeysNb);
|
||||||
foreach($this->result as $row) { print_r($row);
|
}
|
||||||
$line = array();
|
|
||||||
//set horizontal headers
|
// add the column headers
|
||||||
foreach($rowHeadersKeys as $headerKey) {
|
/* @var $columns string[] the column for this row */
|
||||||
|
$columns = array_slice($row, $rowKeysNb, $columnKeysNb);
|
||||||
if (!array_key_exists($row[$headerKey], $labels[$headerKey])) {
|
$columnPosition = findColumnPosition($columnHeaders, $columns);
|
||||||
throw new \LogicException("The value '".$row[$headerKey]."' "
|
|
||||||
. "is not available from the labels defined by aggregators. "
|
//fill with blank at the position given by the columnPosition + nbRowHeaders
|
||||||
. "The key name was $headerKey");
|
for ($i=0; $i < $columnPosition; $i++) {
|
||||||
|
if (!isset($line[$rowKeysNb + $i])) {
|
||||||
|
$line[$rowKeysNb + $i] = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
$line[] = $labels[$headerKey][$row[$headerKey]];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($columnHeadersKeys as $headerKey) {
|
$resultData = array_slice($row, $resultsKeysNb*-1);
|
||||||
|
foreach($resultData as $data) {
|
||||||
|
$line[] = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// we add the last line
|
||||||
|
$contentData[] = $line;
|
||||||
|
|
||||||
|
//column title headers
|
||||||
|
for ($i=0; $i < $columnKeysNb; $i++) {
|
||||||
|
$line = array_fill(0, $rowKeysNb, '');
|
||||||
|
|
||||||
|
foreach($columnHeaders as $set) {
|
||||||
|
$line[] = $set[$i];
|
||||||
}
|
}
|
||||||
//append result
|
|
||||||
foreach($resultsKeys as $key) {
|
|
||||||
$line[] = $labels[$key][$row[$key]];
|
|
||||||
}
|
|
||||||
// append to content
|
|
||||||
$content[] = $line;
|
$content[] = $line;
|
||||||
}
|
}
|
||||||
|
|
||||||
//ordering content
|
|
||||||
//array_multisort($content);
|
//row title headers
|
||||||
|
$headerLine = array();
|
||||||
|
foreach($this->getRowHeaders() as $headerKey) {
|
||||||
|
$headerLine[] = array_key_exists('_header', $this->labels[$headerKey]) ?
|
||||||
|
$this->labels[$headerKey]['_header'] : '';
|
||||||
|
}
|
||||||
|
foreach($this->export->getQueryKeys($this->exportData) as $key) {
|
||||||
|
$headerLine[] = array_key_exists('_header', $this->labels[$key]) ?
|
||||||
|
$this->labels[$key]['_header'] : '';
|
||||||
|
}
|
||||||
|
fputcsv($output, $headerLine);
|
||||||
|
unset($headerLine); //free memory
|
||||||
|
|
||||||
//generate CSV
|
//generate CSV
|
||||||
foreach($content as $line) {
|
foreach($content as $line) {
|
||||||
fputcsv($output, $line);
|
fputcsv($output, $line);
|
||||||
}
|
}
|
||||||
|
foreach($contentData as $line) {
|
||||||
|
fputcsv($output, $line);
|
||||||
|
}
|
||||||
|
|
||||||
$text = stream_get_contents($output);
|
$text = stream_get_contents($output);
|
||||||
fclose($output);
|
fclose($output);
|
||||||
|
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function getOrderedResults()
|
||||||
|
{
|
||||||
|
$r = array();
|
||||||
|
$results = $this->result;
|
||||||
|
$labels = $this->labels;
|
||||||
|
$rowKeys = $this->getRowHeaders();
|
||||||
|
$columnKeys = $this->getColumnHeaders();
|
||||||
|
$resultsKeys = $this->export->getQueryKeys($this->exportData);
|
||||||
|
$headers = array_merge($rowKeys, $columnKeys);
|
||||||
|
|
||||||
|
foreach ($results as $row) {
|
||||||
|
$line = array();
|
||||||
|
foreach ($headers as $key) {
|
||||||
|
if (!array_key_exists($row[$key], $labels[$key])) {
|
||||||
|
throw new \LogicException("The value '".$row[$key]."' "
|
||||||
|
. "is not available from the labels defined by aggregator or report. "
|
||||||
|
. "The key provided by aggregator or report is '$key'");
|
||||||
|
}
|
||||||
|
|
||||||
|
$line[] = $labels[$key][$row[$key]];
|
||||||
|
}
|
||||||
|
|
||||||
|
//append result
|
||||||
|
foreach ($resultsKeys as $key) {
|
||||||
|
$line[] = $labels[$key][$row[$key]];
|
||||||
|
}
|
||||||
|
|
||||||
|
$r[] = $line;
|
||||||
|
}
|
||||||
|
|
||||||
|
array_multisort($r);
|
||||||
|
|
||||||
|
return $r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected function getRowHeaders()
|
protected function getRowHeaders()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user