From 9d18581db14456bf86e280e5822f14c49f25c7a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 9 Dec 2016 19:52:54 +0100 Subject: [PATCH] add an interface for export which are lists Lists does not supports aggregators. The export manager will filter the aggregators applying on the export depending on the interface implemented by the export: if the export implements `Export\ListInterface`, no aggregators will be returned for this export. --- Export/ExportInterface.php | 4 ++++ Export/ExportManager.php | 20 +++++++++++++++++++- Export/Formatter/CSVListFormatter.php | 3 ++- Export/ListInterface.php | 17 +++++++++++++++++ Form/Type/Export/ExportType.php | 2 +- Resources/views/Form/fields.html.twig | 1 + 6 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 Export/ListInterface.php diff --git a/Export/ExportInterface.php b/Export/ExportInterface.php index 5a9eba124..5b68b491f 100644 --- a/Export/ExportInterface.php +++ b/Export/ExportInterface.php @@ -25,6 +25,10 @@ use Doctrine\ORM\QueryBuilder; * Interface for Export. * * An export is a class which will initiate a query for an export. + * + * **Note** : the report implementing this class will be modified by + * both filters **and** aggregators. If the report does not support + * aggregation, use `ListInterface`. * * @example Chill\PersonBundle\Export\CountPerson an example of implementation * @author Julien Fastré diff --git a/Export/ExportManager.php b/Export/ExportManager.php index 35010b827..3a5e15afc 100644 --- a/Export/ExportManager.php +++ b/Export/ExportManager.php @@ -33,6 +33,7 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInt use Chill\MainBundle\Form\Type\Export\PickCenterType; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; use Chill\MainBundle\Form\Type\Export\ExportType; +use Chill\MainBundle\Export\ListInterface; /** * Collects all agregators, filters and export from @@ -235,6 +236,11 @@ class ExportManager return $this->filters[$alias]; } + /** + * get all filters + * + * @param \Generator $aliases + */ public function getFilters(array $aliases) { foreach($aliases as $alias) { @@ -273,6 +279,13 @@ class ExportManager return $this->formatters[$alias]; } + /** + * Get all formatters which supports one of the given types. + * + * + * @param array $types + * @return \Generator + */ public function getFormattersByTypes(array $types) { foreach ($this->formatters as $alias => $formatter) { @@ -358,12 +371,17 @@ class ExportManager } /** - * Return a \Generator containing aggregators which support type + * Return a \Generator containing aggregators supported by the given export * + * @internal This class check the interface implemented by export, and, if ´ListInterface´ is used, return an empty array * @return AggregatorInterface[] a \Generator that contains aggretagors. The key is the filter's alias */ public function &getAggregatorsApplyingOn(ExportInterface $export, array $centers = null) { + if ($export instanceof ListInterface) { + return array(); + } + foreach ($this->aggregators as $alias => $aggregator) { if (in_array($aggregator->applyOn(), $export->supportsModifiers()) && $this->isGrantedForElement($aggregator, $export, $centers)) { diff --git a/Export/Formatter/CSVListFormatter.php b/Export/Formatter/CSVListFormatter.php index a94b5f6a9..190c7308f 100644 --- a/Export/Formatter/CSVListFormatter.php +++ b/Export/Formatter/CSVListFormatter.php @@ -182,7 +182,8 @@ class CSVListFormatter implements FormatterInterface } foreach ($first_row as $key => $value) { - $header_line[] = $this->getLabel($key, '_header'); + $header_line[] = $this->translator->trans( + $this->getLabel($key, '_header')); } if (count($header_line) > 0) { diff --git a/Export/ListInterface.php b/Export/ListInterface.php new file mode 100644 index 000000000..2baab8565 --- /dev/null +++ b/Export/ListInterface.php @@ -0,0 +1,17 @@ +add($filterBuilder); - //add aggregators + //add aggregators $aggregators = $this->exportManager ->getAggregatorsApplyingOn($export, $options['picked_centers']); $aggregatorBuilder = $builder->create(self::AGGREGATOR_KEY, 'form', diff --git a/Resources/views/Form/fields.html.twig b/Resources/views/Form/fields.html.twig index 57c29f882..df758ce60 100644 --- a/Resources/views/Form/fields.html.twig +++ b/Resources/views/Form/fields.html.twig @@ -58,6 +58,7 @@ {{ form_widget(child) }} {{ form_label(child, '', {'label_attr': { 'class' : 'inline'} }) }} +
{% endfor %}