[wip] clean controller + column header [ci skip]

This commit is contained in:
Julien Fastré 2016-01-20 21:59:38 +01:00
parent fe1a9a0838
commit fa246b37b6
3 changed files with 96 additions and 29 deletions

View File

@ -326,6 +326,6 @@ class ExportController extends Controller
$formFormatter->handleRequest($request); $formFormatter->handleRequest($request);
$dataFormatter = $formFormatter->getData(); $dataFormatter = $formFormatter->getData();
return $exportManager->generate($alias, $dataExport['export'], $dataFormatter); return $exportManager->generate($alias, $dataExport['export'], $dataFormatter['formatter']);
} }
} }

View File

@ -277,15 +277,17 @@ class ExportManager
$result = $export->getResult($qb, array()); $result = $export->getResult($qb, array());
/* @var $formatter Formatter\CSVFormatter */ /* @var $formatter Formatter\CSVFormatter */
$formatter = $this->getFormatter('csv'); $formatter = $this->getFormatter($this->getFormatterAlias($data));
$filters = array(); $filters = array();
$aggregators = iterator_to_array($this->retrieveUsedAggregators($data['aggregators']));
$aggregatorsData = array_combine(array_keys($data['aggregators']),
array_map(function($data) { return $data['form']; }, $data['aggregators'])
);
return $formatter->getResponse($result, array(), $export, $aggregators = $this->retrieveUsedAggregators($data['aggregators']);
$filters, $aggregators, array(), $data['filters'], $aggregatorsData); $aggregatorsData = array();
foreach($aggregators as $alias => $aggregator) {
$aggregatorsData[$alias] = $data['aggregators'][$alias]['form'];
}
return $formatter->getResponse($result, $formatterData, $exportAlias, $data,
$filters, $aggregatorsData);
} }
/** /**

View File

@ -48,14 +48,10 @@ class CSVFormatter implements FormatterInterface
protected $export; protected $export;
protected $filters;
protected $aggregators; protected $aggregators;
protected $exportData; protected $exportData;
protected $filterData;
protected $aggregatorsData; protected $aggregatorsData;
/** /**
@ -82,6 +78,13 @@ class CSVFormatter implements FormatterInterface
return 'Comma separated values (CSV)'; return 'Comma separated values (CSV)';
} }
/**
*
* @uses appendAggregatorForm
* @param FormBuilderInterface $builder
* @param type $exportAlias
* @param array $aggregatorAliases
*/
public function buildForm(FormBuilderInterface $builder, $exportAlias, array $aggregatorAliases) public function buildForm(FormBuilderInterface $builder, $exportAlias, array $aggregatorAliases)
{ {
$aggregators = $this->exportManager->getAggregators($aggregatorAliases); $aggregators = $this->exportManager->getAggregators($aggregatorAliases);
@ -97,6 +100,15 @@ class CSVFormatter implements FormatterInterface
} }
} }
/**
* append a form line by aggregator on the formatter form.
*
* This form allow to choose the aggregator position (row or column) and
* the ordering
*
* @param FormBuilderInterface $builder
* @param string $nbAggregators
*/
private function appendAggregatorForm(FormBuilderInterface $builder, $nbAggregators) private function appendAggregatorForm(FormBuilderInterface $builder, $nbAggregators)
{ {
$builder->add('order', 'choice', array( $builder->add('order', 'choice', array(
@ -127,16 +139,15 @@ class CSVFormatter implements FormatterInterface
* @param \Chill\MainBundle\Export\FilterInterface[] $filters * @param \Chill\MainBundle\Export\FilterInterface[] $filters
* @param \Chill\MainBundle\Export\AggregatorInterface[] $aggregators * @param \Chill\MainBundle\Export\AggregatorInterface[] $aggregators
*/ */
public function getResponse($result, $formatterData, ExportInterface $export, $filters, public function getResponse($result, $formatterData, $exportAlias, array $exportData, array $filtersData,
$aggregators, $exportData, $filterData, $aggregatorsData) array $aggregatorsData)
{ {
$this->result = $result; $this->result = $result;
$this->formatterData = $formatterData; $this->orderingHeaders($formatterData);
$this->export = $export; $this->export = $this->exportManager->getExport($exportAlias);
$this->filters = $filters; $this->aggregators = iterator_to_array($this->exportManager
$this->aggregators = $aggregators; ->getAggregators(array_keys($aggregatorsData)));
$this->exportData = $exportData; $this->exportData = $exportData;
$this->filterData = $filterData;
$this->aggregatorsData = $aggregatorsData; $this->aggregatorsData = $aggregatorsData;
$response = new Response(); $response = new Response();
@ -149,12 +160,33 @@ class CSVFormatter implements FormatterInterface
return $response; return $response;
} }
/**
* ordering aggregators, preserving key association.
*
* This function do not mind about position.
*
* If two aggregators have the same order, the second given will be placed
* after. This is not significant for the first ordering.
*
* @param type $formatterData
* @return type
*/
protected function orderingHeaders($formatterData)
{
$this->formatterData = $formatterData;
uasort($this->formatterData, function($a, $b) {
return ($a['order'] <= $b['order'] ? -1 : 1);
});
}
protected function generateContent() protected function generateContent()
{ {
$labels = $this->gatherLabels(); $labels = $this->gatherLabels();
$horizontalHeadersKeys = $this->getHorizontalHeaders(); $rowHeadersKeys = $this->getRowHeaders();
$columnHeadersKeys = $this->getColumnHeaders();
$resultsKeys = $this->export->getQueryKeys($this->exportData); $resultsKeys = $this->export->getQueryKeys($this->exportData);
print_r($columnHeadersKeys);
// 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');
@ -166,7 +198,7 @@ class CSVFormatter implements FormatterInterface
//headers //headers
$headerLine = array(); $headerLine = array();
foreach($horizontalHeadersKeys as $headerKey) { foreach($rowHeadersKeys as $headerKey) {
$headerLine[] = array_key_exists('_header', $labels[$headerKey]) ? $headerLine[] = array_key_exists('_header', $labels[$headerKey]) ?
$labels[$headerKey]['_header'] : ''; $labels[$headerKey]['_header'] : '';
} }
@ -178,10 +210,13 @@ class CSVFormatter implements FormatterInterface
unset($headerLine); //free memory unset($headerLine); //free memory
$content = array(); $content = array();
foreach($this->result as $row) { // create an array with keys as columnHeadersKeys values, values are empty array
$columnHeaders = array_combine($columnHeadersKeys, array_pad(array(),
count($columnHeadersKeys), array()));
foreach($this->result as $row) { print_r($row);
$line = array(); $line = array();
//set horizontal headers //set horizontal headers
foreach($horizontalHeadersKeys as $headerKey) { foreach($rowHeadersKeys as $headerKey) {
if (!array_key_exists($row[$headerKey], $labels[$headerKey])) { if (!array_key_exists($row[$headerKey], $labels[$headerKey])) {
throw new \LogicException("The value '".$row[$headerKey]."' " throw new \LogicException("The value '".$row[$headerKey]."' "
@ -191,6 +226,10 @@ class CSVFormatter implements FormatterInterface
$line[] = $labels[$headerKey][$row[$headerKey]]; $line[] = $labels[$headerKey][$row[$headerKey]];
} }
foreach($columnHeadersKeys as $headerKey) {
}
//append result //append result
foreach($resultsKeys as $key) { foreach($resultsKeys as $key) {
$line[] = $labels[$key][$row[$key]]; $line[] = $labels[$key][$row[$key]];
@ -200,7 +239,7 @@ class CSVFormatter implements FormatterInterface
} }
//ordering content //ordering content
array_multisort($content); //array_multisort($content);
//generate CSV //generate CSV
foreach($content as $line) { foreach($content as $line) {
@ -214,13 +253,39 @@ class CSVFormatter implements FormatterInterface
} }
protected function getHorizontalHeaders() protected function getRowHeaders()
{
return $this->getPositionnalHeaders('r');
}
protected function getColumnHeaders()
{
return $this->getPositionnalHeaders('c');
}
/**
*
* @param string $position may be 'c' (column) or 'r' (row)
* @return string[]
* @throws \RuntimeException
*/
private function getPositionnalHeaders($position)
{ {
$headers = array(); $headers = array();
/* @var $aggregator AggregatorInterface */ foreach($this->formatterData as $alias => $data) {
foreach($this->aggregators as $alias => $aggregator) { if (!array_key_exists($alias, $this->aggregatorsData)) {
$headers = array_merge($headers, $aggregator->getQueryKeys($this->aggregatorsData[$alias])); throw new \RuntimeException("the formatter wants to use the "
. "aggregator with alias $alias, but the export do not "
. "contains data about it");
}
$aggregator = $this->aggregators[$alias];
if ($data['position'] === $position) {
$headers = array_merge($headers, $aggregator->getQueryKeys($this->aggregatorsData[$alias]));
}
} }
return $headers; return $headers;
} }