[wip] add different steps to handle request

This commit is contained in:
2016-01-11 21:45:20 +01:00
parent b40b85527a
commit e1a9ad1612
13 changed files with 829 additions and 48 deletions

View File

@@ -94,9 +94,9 @@ class ExportManager
$this->exports[$alias] = $export;
}
public function addFormatter(FormatterInterface $formatter)
public function addFormatter(FormatterInterface $formatter, $alias)
{
array_push($this->formatters, $formatter);
$this->formatters[$alias] = $formatter;
}
/**
@@ -157,6 +157,13 @@ class ExportManager
return $this->filters[$alias];
}
public function getFilters(array $aliases)
{
foreach($aliases as $alias) {
yield $alias => $this->getFilter($alias);
}
}
/**
*
* @param string $alias
@@ -172,6 +179,31 @@ class ExportManager
return $this->aggregators[$alias];
}
public function getAggregators(array $aliases)
{
foreach ($aliases as $alias) {
yield $alias => $this->getAggregator($alias);
}
}
public function getFormatter($alias)
{
if (!array_key_exists($alias, $this->formatters)) {
throw new \RuntimeException("The formatter with alias $alias is not known.");
}
return $this->formatters[$alias];
}
public function getFormattersByTypes(array $types)
{
foreach ($this->formatters as $alias => $formatter) {
if (in_array($formatter->getType(), $types)) {
yield $alias => $formatter;
}
}
}
/**
* Return a \Generator containing filter which support type
@@ -242,11 +274,18 @@ class ExportManager
'class' => self::class, 'function' => __FUNCTION__
));
$results = $qb->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_SCALAR);
$result = $export->getResult($qb, array());
var_dump($results);
/* @var $formatter Formatter\CSVFormatter */
$formatter = $this->getFormatter('csv');
$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 new Response('everything is fine !');
return $formatter->getResponse($result, array(), $export,
$filters, $aggregators, array(), $data['filters'], $aggregatorsData);
}
/**
@@ -257,12 +296,10 @@ class ExportManager
*/
private function retrieveUsedModifiers($data)
{
$usedTypes = array();
// used filters
$this->retrieveUsedFilters($data, $usedTypes);
// used aggregators
$this->retrieveUsedAggregators($data, $usedTypes);
$usedTypes = array_merge(
$this->retrieveUsedFiltersType($data['filters']),
$this->retrieveUsedAggregatorsType($data['aggregators'])
);
$this->logger->debug('Required types are '.implode(', ', $usedTypes),
array('class' => self::class, 'function' => __FUNCTION__));
@@ -270,9 +307,10 @@ class ExportManager
return $usedTypes;
}
private function retrieveUsedFilters($data, &$usedTypes)
private function retrieveUsedFiltersType($data)
{
foreach($data['filters'] as $alias => $filterData) {
$usedTypes = array();
foreach($data as $alias => $filterData) {
if ($filterData['enabled'] == true){
$filter = $this->getFilter($alias);
if (!in_array($filter->applyOn(), $usedTypes)) {
@@ -280,16 +318,37 @@ class ExportManager
}
}
}
return $usedTypes;
}
private function retrieveUsedAggregators($data, &$usedTypes)
/**
*
* @param mixed $data
* @return string[]
*/
private function retrieveUsedAggregatorsType($data)
{
foreach($data['aggregators'] as $alias => $aggregatorData) {
$usedTypes = array();
foreach($this->retrieveUsedAggregators($data) as $alias => $aggregator) {
if (!in_array($aggregator->applyOn(), $usedTypes)) {
array_push($usedTypes, $aggregator->applyOn());
}
}
return $usedTypes;
}
/**
*
* @param mixed $data
* @return AggregatorInterface[]
*/
private function retrieveUsedAggregators($data)
{
foreach($data as $alias => $aggregatorData) {
if ($aggregatorData['order']> 0){
$aggregator = $this->getAggregator($alias);
if (!in_array($aggregator->applyOn(), $usedTypes)) {
array_push($usedTypes, $aggregator->applyOn());
}
yield $alias => $this->getAggregator($alias);
}
}
}

View File

@@ -0,0 +1,247 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Export\Formatter;
use Chill\MainBundle\Export\ExportInterface;
use Symfony\Component\HttpFoundation\Response;
use Chill\MainBundle\Export\FormatterInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Form\FormBuilderInterface;
// command to get the report with curl : curl --user "center a_social:password" "http://localhost:8000/fr/exports/generate/count_person?export[filters][person_gender_filter][enabled]=&export[filters][person_nationality_filter][enabled]=&export[filters][person_nationality_filter][form][nationalities]=&export[aggregators][person_nationality_aggregator][order]=1&export[aggregators][person_nationality_aggregator][form][group_by_level]=country&export[submit]=&export[_token]=RHpjHl389GrK-bd6iY5NsEqrD5UKOTHH40QKE9J1edU" --globoff
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class CSVFormatter implements FormatterInterface
{
/**
*
* @var TranslatorInterface
*/
protected $translator;
protected $result;
protected $formatterData;
protected $export;
protected $filters;
protected $aggregators;
protected $exportData;
protected $filterData;
protected $aggregatorsData;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function getType()
{
return 'tabular';
}
public function getName()
{
return 'CSV';
}
public function buildForm(FormBuilderInterface $builder, $exportAlias, array $aggregatorAliases)
{
}
/**
*
* @param mixed $result
* @param mixed $data
* @param \Chill\MainBundle\Export\ExportInterface $export
* @param \Chill\MainBundle\Export\FilterInterface[] $filters
* @param \Chill\MainBundle\Export\AggregatorInterface[] $aggregators
*/
public function getResponse($result, $formatterData, ExportInterface $export, $filters,
$aggregators, $exportData, $filterData, $aggregatorsData)
{
$this->result = $result;
$this->formatterData = $formatterData;
$this->export = $export;
$this->filters = $filters;
$this->aggregators = $aggregators;
$this->exportData = $exportData;
$this->filterData = $filterData;
$this->aggregatorsData = $aggregatorsData;
$response = new Response();
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
//$response->headers->set('Content-Disposition','attachment; filename="export.csv"');
$response->setContent($this->generateContent());
return $response;
}
protected function generateContent()
{
$labels = $this->gatherLabels();
$horizontalHeadersKeys = $this->getHorizontalHeaders();
$resultsKeys = $this->export->getQueryKeys($this->exportData);
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
//title
fputcsv($output, array($this->translator->trans($this->export->getTitle())));
//blank line
fputcsv($output, array(""));
//headers
$headerLine = array();
foreach($horizontalHeadersKeys as $headerKey) {
$headerLine[] = array_key_exists('_header', $labels[$headerKey]) ?
$labels[$headerKey]['_header'] : '';
}
foreach($resultsKeys as $key) {
$headerLine[] = array_key_exists('_header', $labels[$key]) ?
$labels[$key]['_header'] : '';
}
fputcsv($output, $headerLine);
unset($headerLine); //free memory
$content = array();
foreach($this->result as $row) {
$line = array();
//set horizontal headers
foreach($horizontalHeadersKeys as $headerKey) {
if (!array_key_exists($row[$headerKey], $labels[$headerKey])) {
throw new \LogicException("The value '".$row[$headerKey]."' "
. "is not available from the labels defined by aggregators. "
. "The key name was $headerKey");
}
$line[] = $labels[$headerKey][$row[$headerKey]];
}
//append result
foreach($resultsKeys as $key) {
$line[] = $labels[$key][$row[$key]];
}
// append to content
$content[] = $line;
}
//ordering content
array_multisort($content);
//generate CSV
foreach($content as $line) {
fputcsv($output, $line);
}
$text = stream_get_contents($output);
fclose($output);
return $text;
}
protected function getHorizontalHeaders()
{
$headers = array();
/* @var $aggregator AggregatorInterface */
foreach($this->aggregators as $alias => $aggregator) {
$headers = array_merge($headers, $aggregator->getQueryKeys($this->aggregatorsData[$alias]));
}
return $headers;
}
/**
*
* @param mixed $result
* @param \Chill\MainBundle\Export\AggregatorInterface[] $aggregators
*/
protected function gatherLabels()
{
return array_merge(
$this->gatherLabelsFromAggregators(),
$this->gatherLabelsFromExport()
);
}
protected function gatherLabelsFromAggregators()
{
$labels = array();
/* @var $aggretator \Chill\MainBundle\Export\AggregatorInterface */
foreach ($this->aggregators as $alias => $aggregator) {
$keys = $aggregator->getQueryKeys($this->aggregatorsData[$alias]);
// gather data in an array
foreach($keys as $key) {
$values = array_map(function($row) use ($key, $alias) {
if (!array_key_exists($key, $row)) {
throw new \LogicException("the key '".$key."' is declared by "
. "the aggregator with alias '".$alias."' but is not "
. "present in results");
}
return $row[$key];
}, $this->result);
$labels[$key] = $aggregator->getLabels($key, array_unique($values),
$this->aggregatorsData[$alias]);
}
}
return $labels;
}
protected function gatherLabelsFromExport()
{
$labels = array();
$export = $this->export;
$keys = $this->export->getQueryKeys($this->exportData);
foreach($keys as $key) {
$values = array_map(function($row) use ($key, $export) {
if (!array_key_exists($key, $row)) {
throw new \LogicException("the key '".$key."' is declared by "
. "the export with title '".$export->getTitle()."' but is not "
. "present in results");
}
return $row[$key];
}, $this->result);
$labels[$key] = $this->export->getLabels($key, array_unique($values),
$this->exportData);
}
return $labels;
}
}

View File

@@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\Mainbundle\Export;
namespace Chill\MainBundle\Export;
/**
*