mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-04 13:54:59 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,68 +1,70 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Export\Formatter;
|
||||
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use LogicException;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* 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.
|
||||
*
|
||||
* @deprecated this formatter is not used any more.
|
||||
*/
|
||||
class CSVFormatter implements FormatterInterface
|
||||
{
|
||||
protected TranslatorInterface $translator;
|
||||
|
||||
protected $result;
|
||||
|
||||
protected $formatterData;
|
||||
|
||||
protected $export;
|
||||
|
||||
protected $aggregators;
|
||||
|
||||
protected $exportData;
|
||||
|
||||
protected $aggregatorsData;
|
||||
|
||||
protected $filtersData;
|
||||
protected $export;
|
||||
|
||||
protected $labels;
|
||||
protected $exportData;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ExportManager
|
||||
*/
|
||||
protected $exportManager;
|
||||
|
||||
protected $filtersData;
|
||||
|
||||
public function __construct(TranslatorInterface $translator,
|
||||
ExportManager $manager)
|
||||
protected $formatterData;
|
||||
|
||||
protected $labels;
|
||||
|
||||
protected $result;
|
||||
|
||||
protected TranslatorInterface $translator;
|
||||
|
||||
public function __construct(
|
||||
TranslatorInterface $translator,
|
||||
ExportManager $manager
|
||||
)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
$this->exportManager = $manager;
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return 'tabular';
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'Comma separated values (CSV)';
|
||||
}
|
||||
|
||||
/**
|
||||
* @uses appendAggregatorForm
|
||||
*
|
||||
* @param mixed $exportAlias
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, $exportAlias, array $aggregatorAliases)
|
||||
{
|
||||
@@ -70,58 +72,61 @@ class CSVFormatter implements FormatterInterface
|
||||
$nb = count($aggregatorAliases);
|
||||
|
||||
foreach ($aggregators as $alias => $aggregator) {
|
||||
$builderAggregator = $builder->create($alias, FormType::class, array(
|
||||
'label' => $aggregator->getTitle(),
|
||||
'block_name' => '_aggregator_placement_csv_formatter'
|
||||
));
|
||||
$builderAggregator = $builder->create($alias, FormType::class, [
|
||||
'label' => $aggregator->getTitle(),
|
||||
'block_name' => '_aggregator_placement_csv_formatter',
|
||||
]);
|
||||
$this->appendAggregatorForm($builderAggregator, $nb);
|
||||
$builder->add($builderAggregator);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
public function gatherFiltersDescriptions()
|
||||
{
|
||||
$builder->add('order', ChoiceType::class, array(
|
||||
'choices' => array_combine(
|
||||
range(1, $nbAggregators),
|
||||
range(1, $nbAggregators)
|
||||
),
|
||||
'multiple' => false,
|
||||
'expanded' => false
|
||||
));
|
||||
$descriptions = [];
|
||||
|
||||
$builder->add('position', ChoiceType::class, array(
|
||||
'choices' => array(
|
||||
'row' => 'r',
|
||||
'column' => 'c'
|
||||
),
|
||||
'multiple' => false,
|
||||
'expanded' => false
|
||||
));
|
||||
foreach ($this->filtersData as $key => $filterData) {
|
||||
$statement = $this->exportManager
|
||||
->getFilter($key)
|
||||
->describeAction($filterData);
|
||||
|
||||
if (null === $statement) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_array($statement)) {
|
||||
$descriptions[] = $this->translator->trans(
|
||||
$statement[0],
|
||||
$statement[1],
|
||||
$statement[2] ?? null,
|
||||
$statement[3] ?? null
|
||||
);
|
||||
} else {
|
||||
$descriptions[] = $statement;
|
||||
}
|
||||
}
|
||||
|
||||
return $descriptions;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'Comma separated values (CSV)';
|
||||
}
|
||||
|
||||
public function getResponse(
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
array $aggregatorsData
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
array $aggregatorsData
|
||||
) {
|
||||
$this->result = $result;
|
||||
$this->orderingHeaders($formatterData);
|
||||
$this->export = $this->exportManager->getExport($exportAlias);
|
||||
$this->aggregators = iterator_to_array($this->exportManager
|
||||
->getAggregators(array_keys($aggregatorsData)));
|
||||
->getAggregators(array_keys($aggregatorsData)));
|
||||
$this->exportData = $exportData;
|
||||
$this->aggregatorsData = $aggregatorsData;
|
||||
$this->labels = $this->gatherLabels();
|
||||
@@ -137,38 +142,72 @@ class CSVFormatter implements FormatterInterface
|
||||
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.
|
||||
*
|
||||
*/
|
||||
protected function orderingHeaders(array $formatterData)
|
||||
public function getType()
|
||||
{
|
||||
$this->formatterData = $formatterData;
|
||||
uasort(
|
||||
$this->formatterData,
|
||||
static fn(array $a, array $b): int => ($a['order'] <= $b['order'] ? -1 : 1)
|
||||
return 'tabular';
|
||||
}
|
||||
|
||||
protected function gatherLabels()
|
||||
{
|
||||
return array_merge(
|
||||
$this->gatherLabelsFromAggregators(),
|
||||
$this->gatherLabelsFromExport()
|
||||
);
|
||||
}
|
||||
|
||||
private function findColumnPosition(&$columnHeaders, $columnToFind): int
|
||||
protected function gatherLabelsFromAggregators()
|
||||
{
|
||||
$i = 0;
|
||||
foreach($columnHeaders as $set) {
|
||||
if ($set === $columnToFind) {
|
||||
return $i;
|
||||
$labels = [];
|
||||
/* @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]
|
||||
);
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
|
||||
//we didn't find it, adding the column
|
||||
$columnHeaders[] = $columnToFind;
|
||||
return $labels;
|
||||
}
|
||||
|
||||
return $i++;
|
||||
protected function gatherLabelsFromExport()
|
||||
{
|
||||
$labels = [];
|
||||
$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;
|
||||
}
|
||||
|
||||
protected function generateContent()
|
||||
@@ -179,25 +218,25 @@ class CSVFormatter implements FormatterInterface
|
||||
$resultsKeysNb = count($this->export->getQueryKeys($this->exportData));
|
||||
$results = $this->getOrderedResults();
|
||||
/* @var $columnHeaders string[] the column headers associations */
|
||||
$columnHeaders = array();
|
||||
$columnHeaders = [];
|
||||
/* @var $data string[] the data of the csv file */
|
||||
$contentData = array();
|
||||
$content = array();
|
||||
$contentData = [];
|
||||
$content = [];
|
||||
|
||||
// create a file pointer connected to the output stream
|
||||
$output = fopen('php://output', 'w');
|
||||
|
||||
//title
|
||||
fputcsv($output, array($this->translator->trans($this->export->getTitle())));
|
||||
fputcsv($output, [$this->translator->trans($this->export->getTitle())]);
|
||||
//blank line
|
||||
fputcsv($output, array(""));
|
||||
fputcsv($output, ['']);
|
||||
|
||||
// add filtering description
|
||||
foreach($this->gatherFiltersDescriptions() as $desc) {
|
||||
fputcsv($output, array($desc));
|
||||
foreach ($this->gatherFiltersDescriptions() as $desc) {
|
||||
fputcsv($output, [$desc]);
|
||||
}
|
||||
// blank line
|
||||
fputcsv($output, array(""));
|
||||
fputcsv($output, ['']);
|
||||
|
||||
// iterate on result to : 1. populate row headers, 2. populate column headers, 3. add result
|
||||
foreach ($results as $row) {
|
||||
@@ -220,41 +259,42 @@ class CSVFormatter implements FormatterInterface
|
||||
$columnPosition = $this->findColumnPosition($columnHeaders, $columns);
|
||||
|
||||
//fill with blank at the position given by the columnPosition + nbRowHeaders
|
||||
for ($i=0; $i < $columnPosition; $i++) {
|
||||
for ($i = 0; $i < $columnPosition; ++$i) {
|
||||
if (!isset($line[$rowKeysNb + $i])) {
|
||||
$line[$rowKeysNb + $i] = "";
|
||||
$line[$rowKeysNb + $i] = '';
|
||||
}
|
||||
}
|
||||
|
||||
$resultData = array_slice($row, $resultsKeysNb*-1);
|
||||
foreach($resultData as $data) {
|
||||
$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++) {
|
||||
for ($i = 0; $i < $columnKeysNb; ++$i) {
|
||||
$line = array_fill(0, $rowKeysNb, '');
|
||||
|
||||
foreach($columnHeaders as $set) {
|
||||
foreach ($columnHeaders as $set) {
|
||||
$line[] = $set[$i];
|
||||
}
|
||||
|
||||
$content[] = $line;
|
||||
}
|
||||
|
||||
|
||||
//row title headers
|
||||
$headerLine = array();
|
||||
foreach($this->getRowHeaders() as $headerKey) {
|
||||
$headerLine = [];
|
||||
|
||||
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) {
|
||||
|
||||
foreach ($this->export->getQueryKeys($this->exportData) as $key) {
|
||||
$headerLine[] = array_key_exists('_header', $this->labels[$key]) ?
|
||||
$this->labels[$key]['_header'] : '';
|
||||
}
|
||||
@@ -262,10 +302,11 @@ class CSVFormatter implements FormatterInterface
|
||||
unset($headerLine); //free memory
|
||||
|
||||
//generate CSV
|
||||
foreach($content as $line) {
|
||||
foreach ($content as $line) {
|
||||
fputcsv($output, $line);
|
||||
}
|
||||
foreach($contentData as $line) {
|
||||
|
||||
foreach ($contentData as $line) {
|
||||
fputcsv($output, $line);
|
||||
}
|
||||
|
||||
@@ -275,10 +316,82 @@ class CSVFormatter implements FormatterInterface
|
||||
return $text;
|
||||
}
|
||||
|
||||
protected function getColumnHeaders()
|
||||
{
|
||||
return $this->getPositionnalHeaders('c');
|
||||
}
|
||||
|
||||
protected function getRowHeaders()
|
||||
{
|
||||
return $this->getPositionnalHeaders('r');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
protected function orderingHeaders(array $formatterData)
|
||||
{
|
||||
$this->formatterData = $formatterData;
|
||||
uasort(
|
||||
$this->formatterData,
|
||||
static fn (array $a, array $b): int => ($a['order'] <= $b['order'] ? -1 : 1)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 string $nbAggregators
|
||||
*/
|
||||
private function appendAggregatorForm(FormBuilderInterface $builder, $nbAggregators)
|
||||
{
|
||||
$builder->add('order', ChoiceType::class, [
|
||||
'choices' => array_combine(
|
||||
range(1, $nbAggregators),
|
||||
range(1, $nbAggregators)
|
||||
),
|
||||
'multiple' => false,
|
||||
'expanded' => false,
|
||||
]);
|
||||
|
||||
$builder->add('position', ChoiceType::class, [
|
||||
'choices' => [
|
||||
'row' => 'r',
|
||||
'column' => 'c',
|
||||
],
|
||||
'multiple' => false,
|
||||
'expanded' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
private function findColumnPosition(&$columnHeaders, $columnToFind): int
|
||||
{
|
||||
$i = 0;
|
||||
|
||||
foreach ($columnHeaders as $set) {
|
||||
if ($set === $columnToFind) {
|
||||
return $i;
|
||||
}
|
||||
++$i;
|
||||
}
|
||||
|
||||
//we didn't find it, adding the column
|
||||
$columnHeaders[] = $columnToFind;
|
||||
|
||||
return $i++;
|
||||
}
|
||||
|
||||
private function getOrderedResults()
|
||||
{
|
||||
$r = array();
|
||||
$r = [];
|
||||
$results = $this->result;
|
||||
$labels = $this->labels;
|
||||
$rowKeys = $this->getRowHeaders();
|
||||
@@ -287,9 +400,9 @@ class CSVFormatter implements FormatterInterface
|
||||
$headers = array_merge($rowKeys, $columnKeys);
|
||||
|
||||
foreach ($results as $row) {
|
||||
$line = array();
|
||||
foreach ($headers as $key) {
|
||||
$line = [];
|
||||
|
||||
foreach ($headers as $key) {
|
||||
$line[] = call_user_func($labels[$key], $row[$key]);
|
||||
}
|
||||
|
||||
@@ -306,31 +419,22 @@ class CSVFormatter implements FormatterInterface
|
||||
return $r;
|
||||
}
|
||||
|
||||
|
||||
protected function getRowHeaders()
|
||||
{
|
||||
return $this->getPositionnalHeaders('r');
|
||||
}
|
||||
|
||||
protected function getColumnHeaders()
|
||||
{
|
||||
return $this->getPositionnalHeaders('c');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $position may be 'c' (column) or 'r' (row)
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*
|
||||
* @return string[]
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
private function getPositionnalHeaders($position)
|
||||
{
|
||||
$headers = array();
|
||||
foreach($this->formatterData as $alias => $data) {
|
||||
$headers = [];
|
||||
|
||||
foreach ($this->formatterData as $alias => $data) {
|
||||
if (!array_key_exists($alias, $this->aggregatorsData)) {
|
||||
throw new \RuntimeException("the formatter wants to use the "
|
||||
. "aggregator with alias $alias, but the export do not "
|
||||
. "contains data about it");
|
||||
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];
|
||||
@@ -342,95 +446,4 @@ class CSVFormatter implements FormatterInterface
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function gatherFiltersDescriptions()
|
||||
{
|
||||
$descriptions = array();
|
||||
|
||||
foreach($this->filtersData as $key => $filterData) {
|
||||
|
||||
$statement = $this->exportManager
|
||||
->getFilter($key)
|
||||
->describeAction($filterData);
|
||||
|
||||
if ($statement === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_array($statement)) {
|
||||
$descriptions[] = $this->translator->trans(
|
||||
$statement[0],
|
||||
$statement[1],
|
||||
isset($statement[2]) ? $statement[2] : null,
|
||||
isset($statement[3]) ? $statement[3] : null);
|
||||
} else {
|
||||
$descriptions[] = $statement;
|
||||
}
|
||||
}
|
||||
|
||||
return $descriptions;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,68 +1,58 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Export\Formatter;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use LogicException;
|
||||
use OutOfBoundsException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use function array_key_exists;
|
||||
use function array_keys;
|
||||
use function array_map;
|
||||
use function implode;
|
||||
|
||||
// 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
|
||||
|
||||
/**
|
||||
* Create a CSV List for the export
|
||||
*
|
||||
* @author Champs-Libres <info@champs-libres.coop>
|
||||
* Create a CSV List for the export.
|
||||
*/
|
||||
class CSVListFormatter implements FormatterInterface
|
||||
{
|
||||
|
||||
protected $exportAlias;
|
||||
|
||||
protected $exportData;
|
||||
|
||||
/**
|
||||
* This variable cache the labels internally
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $labelsCache = null;
|
||||
|
||||
protected $result = null;
|
||||
|
||||
protected $exportAlias = null;
|
||||
|
||||
protected $exportData = null;
|
||||
|
||||
protected $formatterData = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ExportManager
|
||||
*/
|
||||
protected $exportManager;
|
||||
|
||||
|
||||
protected $formatterData;
|
||||
|
||||
/**
|
||||
* This variable cache the labels internally.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $labelsCache;
|
||||
|
||||
protected $result;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
|
||||
public function __construct(TranslatorInterface $translatorInterface, ExportManager $exportManager)
|
||||
{
|
||||
@@ -70,152 +60,130 @@ class CSVListFormatter implements FormatterInterface
|
||||
$this->exportManager = $exportManager;
|
||||
}
|
||||
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return FormatterInterface::TYPE_LIST;
|
||||
/**
|
||||
* build a form, which will be used to collect data required for the execution
|
||||
* of this formatter.
|
||||
*
|
||||
* @uses appendAggregatorForm
|
||||
*
|
||||
* @param type $exportAlias
|
||||
*/
|
||||
public function buildForm(
|
||||
FormBuilderInterface $builder,
|
||||
$exportAlias,
|
||||
array $aggregatorAliases
|
||||
) {
|
||||
$builder->add('numerotation', ChoiceType::class, [
|
||||
'choices' => [
|
||||
'yes' => true,
|
||||
'no' => false,
|
||||
],
|
||||
'expanded' => true,
|
||||
'multiple' => false,
|
||||
'label' => 'Add a number on first column',
|
||||
'data' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'CSV vertical list';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* build a form, which will be used to collect data required for the execution
|
||||
* of this formatter.
|
||||
*
|
||||
* @uses appendAggregatorForm
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param type $exportAlias
|
||||
* @param array $aggregatorAliases
|
||||
*/
|
||||
public function buildForm(
|
||||
FormBuilderInterface $builder,
|
||||
$exportAlias,
|
||||
array $aggregatorAliases
|
||||
){
|
||||
$builder->add('numerotation', ChoiceType::class, array(
|
||||
'choices' => array(
|
||||
'yes' => true,
|
||||
'no' => false
|
||||
),
|
||||
'expanded' => true,
|
||||
'multiple' => false,
|
||||
'label' => "Add a number on first column",
|
||||
'data' => true
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a response from the data collected on differents ExportElementInterface
|
||||
*
|
||||
* Generate a response from the data collected on differents ExportElementInterface.
|
||||
*
|
||||
* @param mixed[] $result The result, as given by the ExportInterface
|
||||
* @param mixed[] $formatterData collected from the current form
|
||||
* @param string $exportAlias the id of the current export
|
||||
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
|
||||
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response The response to be shown
|
||||
*/
|
||||
public function getResponse(
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
array $aggregatorsData
|
||||
) {
|
||||
$this->result = $result;
|
||||
$this->exportAlias = $exportAlias;
|
||||
$this->exportData = $exportData;
|
||||
$this->formatterData = $formatterData;
|
||||
|
||||
|
||||
$output = fopen('php://output', 'w');
|
||||
|
||||
|
||||
$this->prepareHeaders($output);
|
||||
|
||||
|
||||
$i = 1;
|
||||
|
||||
foreach ($result as $row) {
|
||||
$line = array();
|
||||
|
||||
if ($this->formatterData['numerotation'] === true) {
|
||||
$line = [];
|
||||
|
||||
if (true === $this->formatterData['numerotation']) {
|
||||
$line[] = $i;
|
||||
}
|
||||
|
||||
|
||||
foreach ($row as $key => $value) {
|
||||
$line[] = $this->getLabel($key, $value);
|
||||
}
|
||||
|
||||
|
||||
fputcsv($output, $line);
|
||||
|
||||
$i++;
|
||||
|
||||
++$i;
|
||||
}
|
||||
|
||||
|
||||
$csvContent = stream_get_contents($output);
|
||||
fclose($output);
|
||||
|
||||
|
||||
$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($csvContent);
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* add the headers to the csv file
|
||||
*
|
||||
* @param resource $output
|
||||
*/
|
||||
protected function prepareHeaders($output)
|
||||
|
||||
public function getType()
|
||||
{
|
||||
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
|
||||
// we want to keep the order of the first row. So we will iterate on the first row of the results
|
||||
$first_row = count($this->result) > 0 ? $this->result[0] : array();
|
||||
$header_line = array();
|
||||
|
||||
if ($this->formatterData['numerotation'] === true) {
|
||||
$header_line[] = $this->translator->trans('Number');
|
||||
}
|
||||
|
||||
foreach ($first_row as $key => $value) {
|
||||
$header_line[] = $this->translator->trans(
|
||||
$this->getLabel($key, '_header'));
|
||||
}
|
||||
|
||||
if (count($header_line) > 0) {
|
||||
fputcsv($output, $header_line);
|
||||
}
|
||||
return FormatterInterface::TYPE_LIST;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Give the label corresponding to the given key and value.
|
||||
*
|
||||
* Give the label corresponding to the given key and value.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*
|
||||
* @throws LogicException if the label is not found
|
||||
*
|
||||
* @return string
|
||||
* @throws \LogicException if the label is not found
|
||||
*/
|
||||
protected function getLabel($key, $value)
|
||||
{
|
||||
|
||||
if ($this->labelsCache === null) {
|
||||
if (null === $this->labelsCache) {
|
||||
$this->prepareCacheLabels();
|
||||
}
|
||||
|
||||
if (!\array_key_exists($key, $this->labelsCache)){
|
||||
throw new \OutOfBoundsException(sprintf("The key \"%s\" "
|
||||
. "is not present in the list of keys handled by "
|
||||
. "this query. Check your `getKeys` and `getLabels` "
|
||||
. "methods. Available keys are %s.", $key,
|
||||
\implode(", ", \array_keys($this->labelsCache))));
|
||||
|
||||
if (!array_key_exists($key, $this->labelsCache)) {
|
||||
throw new OutOfBoundsException(sprintf(
|
||||
'The key "%s" '
|
||||
. 'is not present in the list of keys handled by '
|
||||
. 'this query. Check your `getKeys` and `getLabels` '
|
||||
. 'methods. Available keys are %s.',
|
||||
$key,
|
||||
implode(', ', array_keys($this->labelsCache))
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
return $this->labelsCache[$key]($value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepare the label cache which will be used by getLabel. This function
|
||||
* should be called only once in the generation lifecycle.
|
||||
@@ -224,14 +192,39 @@ class CSVListFormatter implements FormatterInterface
|
||||
{
|
||||
$export = $this->exportManager->getExport($this->exportAlias);
|
||||
$keys = $export->getQueryKeys($this->exportData);
|
||||
|
||||
foreach($keys as $key) {
|
||||
|
||||
foreach ($keys as $key) {
|
||||
// get an array with all values for this key if possible
|
||||
$values = \array_map(function ($v) use ($key) { return $v[$key]; }, $this->result);
|
||||
$values = array_map(function ($v) use ($key) { return $v[$key]; }, $this->result);
|
||||
// store the label in the labelsCache property
|
||||
$this->labelsCache[$key] = $export->getLabels($key, $values, $this->exportData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* add the headers to the csv file.
|
||||
*
|
||||
* @param resource $output
|
||||
*/
|
||||
protected function prepareHeaders($output)
|
||||
{
|
||||
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
|
||||
// we want to keep the order of the first row. So we will iterate on the first row of the results
|
||||
$first_row = count($this->result) > 0 ? $this->result[0] : [];
|
||||
$header_line = [];
|
||||
|
||||
if (true === $this->formatterData['numerotation']) {
|
||||
$header_line[] = $this->translator->trans('Number');
|
||||
}
|
||||
|
||||
foreach ($first_row as $key => $value) {
|
||||
$header_line[] = $this->translator->trans(
|
||||
$this->getLabel($key, '_header')
|
||||
);
|
||||
}
|
||||
|
||||
if (count($header_line) > 0) {
|
||||
fputcsv($output, $header_line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,126 +1,106 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Export\Formatter;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use LogicException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use function array_map;
|
||||
|
||||
/**
|
||||
* Create a CSV List for the export where the header are printed on the
|
||||
* Create a CSV List for the export where the header are printed on the
|
||||
* first column, and the result goes from left to right.
|
||||
*
|
||||
* @author Champs-Libres <info@champs-libres.coop>
|
||||
*/
|
||||
class CSVPivotedListFormatter implements FormatterInterface
|
||||
{
|
||||
|
||||
protected $exportAlias;
|
||||
|
||||
protected $exportData;
|
||||
|
||||
/**
|
||||
* This variable cache the labels internally
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $labelsCache = null;
|
||||
|
||||
protected $result = null;
|
||||
|
||||
protected $exportAlias = null;
|
||||
|
||||
protected $exportData = null;
|
||||
|
||||
protected $formatterData = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ExportManager
|
||||
*/
|
||||
protected $exportManager;
|
||||
|
||||
|
||||
protected $formatterData;
|
||||
|
||||
/**
|
||||
* This variable cache the labels internally.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $labelsCache;
|
||||
|
||||
protected $result;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
|
||||
|
||||
public function __construct(TranslatorInterface $translatorInterface, ExportManager $exportManager)
|
||||
{
|
||||
$this->translator = $translatorInterface;
|
||||
$this->exportManager = $exportManager;
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return FormatterInterface::TYPE_LIST;
|
||||
|
||||
/**
|
||||
* build a form, which will be used to collect data required for the execution
|
||||
* of this formatter.
|
||||
*
|
||||
* @uses appendAggregatorForm
|
||||
*
|
||||
* @param type $exportAlias
|
||||
*/
|
||||
public function buildForm(
|
||||
FormBuilderInterface $builder,
|
||||
$exportAlias,
|
||||
array $aggregatorAliases
|
||||
) {
|
||||
$builder->add('numerotation', ChoiceType::class, [
|
||||
'choices' => [
|
||||
'yes' => true,
|
||||
'no' => false,
|
||||
],
|
||||
'expanded' => true,
|
||||
'multiple' => false,
|
||||
'label' => 'Add a number on first column',
|
||||
'data' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'CSV horizontal list';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* build a form, which will be used to collect data required for the execution
|
||||
* of this formatter.
|
||||
*
|
||||
* @uses appendAggregatorForm
|
||||
* @param FormBuilderInterface $builder
|
||||
* @param type $exportAlias
|
||||
* @param array $aggregatorAliases
|
||||
*/
|
||||
public function buildForm(
|
||||
FormBuilderInterface $builder,
|
||||
$exportAlias,
|
||||
array $aggregatorAliases
|
||||
){
|
||||
$builder->add('numerotation', ChoiceType::class, array(
|
||||
'choices' => array(
|
||||
'yes' => true,
|
||||
'no' => false
|
||||
),
|
||||
'expanded' => true,
|
||||
'multiple' => false,
|
||||
'label' => "Add a number on first column",
|
||||
'data' => true
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a response from the data collected on differents ExportElementInterface
|
||||
*
|
||||
* Generate a response from the data collected on differents ExportElementInterface.
|
||||
*
|
||||
* @param mixed[] $result The result, as given by the ExportInterface
|
||||
* @param mixed[] $formatterData collected from the current form
|
||||
* @param string $exportAlias the id of the current export
|
||||
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
|
||||
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response The response to be shown
|
||||
*/
|
||||
public function getResponse(
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
array $aggregatorsData
|
||||
) {
|
||||
@@ -128,85 +108,70 @@ class CSVPivotedListFormatter implements FormatterInterface
|
||||
$this->exportAlias = $exportAlias;
|
||||
$this->exportData = $exportData;
|
||||
$this->formatterData = $formatterData;
|
||||
|
||||
|
||||
$output = fopen('php://output', 'w');
|
||||
|
||||
|
||||
$i = 1;
|
||||
$lines = array();
|
||||
$lines = [];
|
||||
$this->prepareHeaders($lines);
|
||||
|
||||
|
||||
foreach ($result as $row) {
|
||||
$j = 0;
|
||||
|
||||
if ($this->formatterData['numerotation'] === true) {
|
||||
|
||||
if (true === $this->formatterData['numerotation']) {
|
||||
$lines[$j][] = $i;
|
||||
$j++;
|
||||
++$j;
|
||||
}
|
||||
|
||||
|
||||
foreach ($row as $key => $value) {
|
||||
$lines[$j][] = $this->getLabel($key, $value);
|
||||
$j++;
|
||||
++$j;
|
||||
}
|
||||
$i++;
|
||||
++$i;
|
||||
}
|
||||
|
||||
|
||||
//adding the lines to the csv output
|
||||
foreach($lines as $line) {
|
||||
foreach ($lines as $line) {
|
||||
fputcsv($output, $line);
|
||||
}
|
||||
|
||||
|
||||
$csvContent = stream_get_contents($output);
|
||||
fclose($output);
|
||||
|
||||
|
||||
$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->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
|
||||
|
||||
$response->setContent($csvContent);
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* add the headers to lines array
|
||||
*
|
||||
* @param array $lines the lines where the header will be added
|
||||
*/
|
||||
protected function prepareHeaders(array &$lines)
|
||||
|
||||
public function getType()
|
||||
{
|
||||
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
|
||||
// we want to keep the order of the first row. So we will iterate on the first row of the results
|
||||
$first_row = count($this->result) > 0 ? $this->result[0] : array();
|
||||
$header_line = array();
|
||||
|
||||
if ($this->formatterData['numerotation'] === true) {
|
||||
$lines[] = array($this->translator->trans('Number'));
|
||||
}
|
||||
|
||||
foreach ($first_row as $key => $value) {
|
||||
$lines[] = array($this->getLabel($key, '_header'));
|
||||
}
|
||||
return FormatterInterface::TYPE_LIST;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Give the label corresponding to the given key and value.
|
||||
*
|
||||
* Give the label corresponding to the given key and value.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*
|
||||
* @throws LogicException if the label is not found
|
||||
*
|
||||
* @return string
|
||||
* @throws \LogicException if the label is not found
|
||||
*/
|
||||
protected function getLabel($key, $value)
|
||||
{
|
||||
|
||||
if ($this->labelsCache === null) {
|
||||
if (null === $this->labelsCache) {
|
||||
$this->prepareCacheLabels();
|
||||
}
|
||||
|
||||
|
||||
return $this->labelsCache[$key]($value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepare the label cache which will be used by getLabel. This function
|
||||
* should be called only once in the generation lifecycle.
|
||||
@@ -215,14 +180,33 @@ class CSVPivotedListFormatter implements FormatterInterface
|
||||
{
|
||||
$export = $this->exportManager->getExport($this->exportAlias);
|
||||
$keys = $export->getQueryKeys($this->exportData);
|
||||
|
||||
foreach($keys as $key) {
|
||||
|
||||
foreach ($keys as $key) {
|
||||
// get an array with all values for this key if possible
|
||||
$values = \array_map(function ($v) use ($key) { return $v[$key]; }, $this->result);
|
||||
$values = array_map(function ($v) use ($key) { return $v[$key]; }, $this->result);
|
||||
// store the label in the labelsCache property
|
||||
$this->labelsCache[$key] = $export->getLabels($key, $values, $this->exportData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* add the headers to lines array.
|
||||
*
|
||||
* @param array $lines the lines where the header will be added
|
||||
*/
|
||||
protected function prepareHeaders(array &$lines)
|
||||
{
|
||||
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
|
||||
// we want to keep the order of the first row. So we will iterate on the first row of the results
|
||||
$first_row = count($this->result) > 0 ? $this->result[0] : [];
|
||||
$header_line = [];
|
||||
|
||||
if (true === $this->formatterData['numerotation']) {
|
||||
$lines[] = [$this->translator->trans('Number')];
|
||||
}
|
||||
|
||||
foreach ($first_row as $key => $value) {
|
||||
$lines[] = [$this->getLabel($key, '_header')];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,96 +1,38 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2017 Champs Libres Cooperative <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 Symfony\Component\HttpFoundation\Response;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Export\Formatter;
|
||||
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use LogicException;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use function array_map;
|
||||
use function array_merge;
|
||||
use function array_multisort;
|
||||
use function array_unique;
|
||||
use function fopen;
|
||||
use function stream_get_contents;
|
||||
use function sys_get_temp_dir;
|
||||
use function tempnam;
|
||||
use function unlink;
|
||||
|
||||
class SpreadSheetFormatter implements FormatterInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ExportManager
|
||||
*/
|
||||
protected $exportManager;
|
||||
|
||||
/**
|
||||
* The result, as returned by the export
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var type
|
||||
*/
|
||||
protected $result;
|
||||
|
||||
/**
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var type
|
||||
*/
|
||||
protected $formatterData;
|
||||
|
||||
/**
|
||||
* The export
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var \Chill\MainBundle\Export\ExportInterface
|
||||
*/
|
||||
protected $export;
|
||||
|
||||
/**
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var type
|
||||
*/
|
||||
//protected $aggregators;
|
||||
|
||||
/**
|
||||
* array containing value of export form
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $exportData;
|
||||
|
||||
/**
|
||||
* an array where keys are the aggregators aliases and
|
||||
* values are the data
|
||||
* values are the data.
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
@@ -99,7 +41,36 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
protected $aggregatorsData;
|
||||
|
||||
/**
|
||||
* The export.
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var \Chill\MainBundle\Export\ExportInterface
|
||||
*/
|
||||
protected $export;
|
||||
|
||||
/**
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var type
|
||||
*/
|
||||
//protected $aggregators;
|
||||
|
||||
/**
|
||||
* array containing value of export form.
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $exportData;
|
||||
|
||||
/**
|
||||
* @var ExportManager
|
||||
*/
|
||||
protected $exportManager;
|
||||
|
||||
/**
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var array
|
||||
@@ -107,7 +78,22 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
protected $filtersData;
|
||||
|
||||
/**
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var type
|
||||
*/
|
||||
protected $formatterData;
|
||||
|
||||
/**
|
||||
* The result, as returned by the export.
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var type
|
||||
*/
|
||||
protected $result;
|
||||
|
||||
/**
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var array
|
||||
@@ -115,12 +101,17 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
//protected $labels;
|
||||
|
||||
/**
|
||||
* temporary file to store spreadsheet
|
||||
* temporary file to store spreadsheet.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $tempfile;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* cache for displayable result.
|
||||
*
|
||||
@@ -137,7 +128,7 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
/**
|
||||
* Whethe `cacheDisplayableResult` is initialized or not.
|
||||
*
|
||||
* @var boolean
|
||||
* @var bool
|
||||
*/
|
||||
private $cacheDisplayableResultIsInitialized = false;
|
||||
|
||||
@@ -147,58 +138,35 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
$this->exportManager = $exportManager;
|
||||
}
|
||||
|
||||
|
||||
public function buildForm(
|
||||
FormBuilderInterface $builder,
|
||||
$exportAlias,
|
||||
array $aggregatorAliases
|
||||
) {
|
||||
// choosing between formats
|
||||
$builder->add('format', ChoiceType::class, array(
|
||||
'choices' => array(
|
||||
$builder->add('format', ChoiceType::class, [
|
||||
'choices' => [
|
||||
'OpenDocument Format (.ods) (LibreOffice, ...)' => 'ods',
|
||||
'Microsoft Excel 2007-2013 XML (.xlsx) (Microsoft Excel, LibreOffice)' => 'xlsx',
|
||||
'Comma separated values (.csv)' => 'csv'
|
||||
),
|
||||
'placeholder' => 'Choose the format'
|
||||
));
|
||||
'Comma separated values (.csv)' => 'csv',
|
||||
],
|
||||
'placeholder' => 'Choose the format',
|
||||
]);
|
||||
|
||||
// ordering aggregators
|
||||
$aggregators = $this->exportManager->getAggregators($aggregatorAliases);
|
||||
$nb = count($aggregatorAliases);
|
||||
|
||||
foreach ($aggregators as $alias => $aggregator) {
|
||||
$builderAggregator = $builder->create($alias, FormType::class, array(
|
||||
'label' => $aggregator->getTitle(),
|
||||
'block_name' => '_aggregator_placement_spreadsheet_formatter'
|
||||
));
|
||||
$builderAggregator = $builder->create($alias, FormType::class, [
|
||||
'label' => $aggregator->getTitle(),
|
||||
'block_name' => '_aggregator_placement_spreadsheet_formatter',
|
||||
]);
|
||||
$this->appendAggregatorForm($builderAggregator, $nb);
|
||||
$builder->add($builderAggregator);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$builder->add('order', ChoiceType::class, array(
|
||||
'choices' => array_combine(
|
||||
range(1, $nbAggregators),
|
||||
range(1, $nbAggregators)
|
||||
),
|
||||
'multiple' => false,
|
||||
'expanded' => false
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'SpreadSheet (xlsx, ods)';
|
||||
@@ -221,69 +189,123 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
$this->aggregatorsData = $aggregatorsData;
|
||||
|
||||
// reset cache
|
||||
$this->cacheDisplayableResult = array();
|
||||
$this->cacheDisplayableResult = [];
|
||||
$this->cacheDisplayableResultIsInitialized = false;
|
||||
|
||||
$response = new Response();
|
||||
$response->headers->set('Content-Type',
|
||||
$this->getContentType($this->formatterData['format']));
|
||||
$response->headers->set(
|
||||
'Content-Type',
|
||||
$this->getContentType($this->formatterData['format'])
|
||||
);
|
||||
|
||||
$this->tempfile = \tempnam(\sys_get_temp_dir(), '');
|
||||
$this->tempfile = tempnam(sys_get_temp_dir(), '');
|
||||
$this->generateContent();
|
||||
|
||||
$f = \fopen($this->tempfile, 'r');
|
||||
$response->setContent(\stream_get_contents($f));
|
||||
$f = fopen($this->tempfile, 'r');
|
||||
$response->setContent(stream_get_contents($f));
|
||||
fclose($f);
|
||||
|
||||
// remove the temp file from disk
|
||||
\unlink($this->tempfile);
|
||||
unlink($this->tempfile);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the content and write it to php://temp
|
||||
*/
|
||||
protected function generateContent()
|
||||
public function getType()
|
||||
{
|
||||
list($spreadsheet, $worksheet) = $this->createSpreadsheet();
|
||||
return 'tabular';
|
||||
}
|
||||
|
||||
$this->addTitleToWorkSheet($worksheet);
|
||||
$line = $this->addFiltersDescription($worksheet);
|
||||
protected function addContentTable(
|
||||
Worksheet $worksheet,
|
||||
$sortedResults,
|
||||
$line
|
||||
) {
|
||||
$worksheet->fromArray(
|
||||
$sortedResults,
|
||||
null,
|
||||
'A' . $line
|
||||
);
|
||||
|
||||
// at this point, we are going to sort retsults for an easier manipulation
|
||||
list($sortedResult, $exportKeys, $aggregatorKeys, $globalKeys) =
|
||||
$this->sortResult();
|
||||
|
||||
$line = $this->addHeaders($worksheet, $globalKeys, $line);
|
||||
|
||||
$line = $this->addContentTable($worksheet, $sortedResult, $line);
|
||||
|
||||
switch ($this->formatterData['format'])
|
||||
{
|
||||
case 'ods':
|
||||
$writer = \PhpOffice\PhpSpreadsheet\IOFactory
|
||||
::createWriter($spreadsheet, 'Ods');
|
||||
break;
|
||||
case 'xlsx':
|
||||
$writer = \PhpOffice\PhpSpreadsheet\IOFactory
|
||||
::createWriter($spreadsheet, 'Xlsx');
|
||||
break;
|
||||
case 'csv':
|
||||
$writer = \PhpOffice\PhpSpreadsheet\IOFactory
|
||||
::createWriter($spreadsheet, 'Csv');
|
||||
break;
|
||||
default:
|
||||
// this should not happen
|
||||
// throw an exception to ensure that the error is catched
|
||||
throw new \LogicException();
|
||||
}
|
||||
|
||||
$writer->save($this->tempfile);
|
||||
return $line + count($sortedResults) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a spreadsheet and a working worksheet
|
||||
* Add filter description since line 3.
|
||||
*
|
||||
* return the line number after the last description
|
||||
*
|
||||
* @return int the line number after the last description
|
||||
*/
|
||||
protected function addFiltersDescription(Worksheet &$worksheet)
|
||||
{
|
||||
$line = 3;
|
||||
|
||||
foreach ($this->filtersData as $alias => $data) {
|
||||
$filter = $this->exportManager->getFilter($alias);
|
||||
$description = $filter->describeAction($data, 'string');
|
||||
|
||||
if (is_array($description)) {
|
||||
$description = $this->translator
|
||||
->trans(
|
||||
$description[0],
|
||||
$description[1] ?? []
|
||||
);
|
||||
}
|
||||
|
||||
$worksheet->setCellValue('A' . $line, $description);
|
||||
++$line;
|
||||
}
|
||||
|
||||
return $line;
|
||||
}
|
||||
|
||||
/**
|
||||
* add headers to worksheet.
|
||||
*
|
||||
* return the line number where the next content (i.e. result) should
|
||||
* be appended.
|
||||
*
|
||||
* @param int $line
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function addHeaders(
|
||||
Worksheet &$worksheet,
|
||||
array $globalKeys,
|
||||
$line
|
||||
) {
|
||||
// get the displayable form of headers
|
||||
$displayables = [];
|
||||
|
||||
foreach ($globalKeys as $key) {
|
||||
$displayables[] = $this->translator->trans(
|
||||
$this->getDisplayableResult($key, '_header')
|
||||
);
|
||||
}
|
||||
|
||||
// add headers on worksheet
|
||||
$worksheet->fromArray(
|
||||
$displayables,
|
||||
null,
|
||||
'A' . $line
|
||||
);
|
||||
|
||||
return $line + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the title to the worksheet and merge the cell containing
|
||||
* the title.
|
||||
*/
|
||||
protected function addTitleToWorkSheet(Worksheet &$worksheet)
|
||||
{
|
||||
$worksheet->setCellValue('A1', $this->getTitle());
|
||||
$worksheet->mergeCells('A1:G1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a spreadsheet and a working worksheet.
|
||||
*
|
||||
* @return array where 1st member is spreadsheet, 2nd is worksheet
|
||||
*/
|
||||
@@ -301,46 +323,179 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the title to the worksheet and merge the cell containing
|
||||
* the title
|
||||
*
|
||||
* @param Worksheet $worksheet
|
||||
* Generate the content and write it to php://temp.
|
||||
*/
|
||||
protected function addTitleToWorkSheet(Worksheet &$worksheet)
|
||||
protected function generateContent()
|
||||
{
|
||||
$worksheet->setCellValue('A1', $this->getTitle());
|
||||
$worksheet->mergeCells('A1:G1');
|
||||
[$spreadsheet, $worksheet] = $this->createSpreadsheet();
|
||||
|
||||
$this->addTitleToWorkSheet($worksheet);
|
||||
$line = $this->addFiltersDescription($worksheet);
|
||||
|
||||
// at this point, we are going to sort retsults for an easier manipulation
|
||||
[$sortedResult, $exportKeys, $aggregatorKeys, $globalKeys] =
|
||||
$this->sortResult();
|
||||
|
||||
$line = $this->addHeaders($worksheet, $globalKeys, $line);
|
||||
|
||||
$line = $this->addContentTable($worksheet, $sortedResult, $line);
|
||||
|
||||
switch ($this->formatterData['format']) {
|
||||
case 'ods':
|
||||
$writer = \PhpOffice\PhpSpreadsheet\IOFactory
|
||||
::createWriter($spreadsheet, 'Ods');
|
||||
|
||||
break;
|
||||
|
||||
case 'xlsx':
|
||||
$writer = \PhpOffice\PhpSpreadsheet\IOFactory
|
||||
::createWriter($spreadsheet, 'Xlsx');
|
||||
|
||||
break;
|
||||
|
||||
case 'csv':
|
||||
$writer = \PhpOffice\PhpSpreadsheet\IOFactory
|
||||
::createWriter($spreadsheet, 'Csv');
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
// this should not happen
|
||||
// throw an exception to ensure that the error is catched
|
||||
throw new LogicException();
|
||||
}
|
||||
|
||||
$writer->save($this->tempfile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add filter description since line 3.
|
||||
* get an array of aggregator keys. The keys are sorted as asked
|
||||
* by user in the form.
|
||||
*
|
||||
* return the line number after the last description
|
||||
*
|
||||
* @param Worksheet $worksheet
|
||||
* @return int the line number after the last description
|
||||
* @return string[] an array containing the keys of aggregators
|
||||
*/
|
||||
protected function addFiltersDescription(Worksheet &$worksheet)
|
||||
protected function getAggregatorKeysSorted()
|
||||
{
|
||||
$line = 3;
|
||||
// empty array for aggregators keys
|
||||
$keys = [];
|
||||
// this association between key and aggregator alias will be used
|
||||
// during sorting
|
||||
$aggregatorKeyAssociation = [];
|
||||
|
||||
foreach ($this->filtersData as $alias => $data) {
|
||||
$filter = $this->exportManager->getFilter($alias);
|
||||
$description = $filter->describeAction($data, 'string');
|
||||
|
||||
if (is_array($description)) {
|
||||
$description = $this->translator
|
||||
->trans(
|
||||
$description[0],
|
||||
isset($description[1]) ? $description[1] : []
|
||||
);
|
||||
foreach ($this->aggregatorsData as $alias => $data) {
|
||||
$aggregator = $this->exportManager->getAggregator($alias);
|
||||
$aggregatorsKeys = $aggregator->getQueryKeys($data);
|
||||
// append the keys from aggregator to the $keys existing array
|
||||
$keys = array_merge($keys, $aggregatorsKeys);
|
||||
// append the key with the alias, which will be use later for sorting
|
||||
foreach ($aggregatorsKeys as $key) {
|
||||
$aggregatorKeyAssociation[$key] = $alias;
|
||||
}
|
||||
|
||||
$worksheet->setCellValue('A'.$line, $description);
|
||||
$line ++;
|
||||
}
|
||||
|
||||
return $line;
|
||||
// sort the result using the form
|
||||
usort($keys, function ($a, $b) use ($aggregatorKeyAssociation) {
|
||||
$A = $this->formatterData[$aggregatorKeyAssociation[$a]]['order'];
|
||||
$B = $this->formatterData[$aggregatorKeyAssociation[$b]]['order'];
|
||||
|
||||
if ($A === $B) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ($A > $B) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
});
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
protected function getContentType($format)
|
||||
{
|
||||
switch ($format) {
|
||||
case 'csv':
|
||||
return 'text/csv';
|
||||
|
||||
case 'ods':
|
||||
return 'application/vnd.oasis.opendocument.spreadsheet';
|
||||
|
||||
case 'xlsx':
|
||||
return 'application/vnd.openxmlformats-officedocument.'
|
||||
. 'spreadsheetml.sheet';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the displayable result.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getDisplayableResult($key, $value)
|
||||
{
|
||||
if (false === $this->cacheDisplayableResultIsInitialized) {
|
||||
$this->initializeCache($key);
|
||||
}
|
||||
|
||||
return call_user_func($this->cacheDisplayableResult[$key], $value);
|
||||
}
|
||||
|
||||
protected function getTitle()
|
||||
{
|
||||
return $this->translator->trans($this->export->getTitle());
|
||||
}
|
||||
|
||||
protected function initializeCache($key)
|
||||
{
|
||||
/*
|
||||
* this function follows the following steps :
|
||||
*
|
||||
* 1. associate all keys used in result with their export element
|
||||
* (export or aggregator) and data;
|
||||
* 2. associate all keys used in result with all the possible values :
|
||||
* this array will be necessary to call `getLabels`
|
||||
* 3. store the `callable` in an associative array, in cache
|
||||
*/
|
||||
// 1. create an associative array with key and export / aggregator
|
||||
$keysExportElementAssociation = [];
|
||||
// keys for export
|
||||
foreach ($this->export->getQueryKeys($this->exportData) as $key) {
|
||||
$keysExportElementAssociation[$key] = [$this->export,
|
||||
$this->exportData, ];
|
||||
}
|
||||
// keys for aggregator
|
||||
foreach ($this->aggregatorsData as $alias => $data) {
|
||||
$aggregator = $this->exportManager->getAggregator($alias);
|
||||
|
||||
foreach ($aggregator->getQueryKeys($data) as $key) {
|
||||
$keysExportElementAssociation[$key] = [$aggregator, $data];
|
||||
}
|
||||
}
|
||||
|
||||
// 2. collect all the keys before iteration
|
||||
$keys = array_keys($keysExportElementAssociation);
|
||||
|
||||
$allValues = [];
|
||||
// store all the values in an array
|
||||
foreach ($this->result as $row) {
|
||||
foreach ($keys as $key) {
|
||||
$allValues[$key][] = $row[$key];
|
||||
}
|
||||
}
|
||||
|
||||
// 3. iterate on `keysExportElementAssociation` to store the callable
|
||||
// in cache
|
||||
foreach ($keysExportElementAssociation as $key => [$element, $data]) {
|
||||
$this->cacheDisplayableResult[$key] =
|
||||
$element->getLabels($key, array_unique($allValues[$key]), $data);
|
||||
}
|
||||
|
||||
// the cache is initialized !
|
||||
$this->cacheDisplayableResultIsInitialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -348,8 +503,7 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
* - 0 => sorted results
|
||||
* - 1 => export keys
|
||||
* - 2 => aggregator keys
|
||||
* - 3 => global keys (aggregator keys and export keys)
|
||||
*
|
||||
* - 3 => global keys (aggregator keys and export keys).
|
||||
*
|
||||
* Example, assuming that the result contains two aggregator keys :
|
||||
*
|
||||
@@ -378,7 +532,6 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
* array( 5, 6, 4 )
|
||||
* )
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
protected function sortResult()
|
||||
{
|
||||
@@ -386,198 +539,40 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
$exportKeys = $this->export->getQueryKeys($this->exportData);
|
||||
$aggregatorKeys = $this->getAggregatorKeysSorted();
|
||||
|
||||
$globalKeys = \array_merge($aggregatorKeys, $exportKeys);
|
||||
$globalKeys = array_merge($aggregatorKeys, $exportKeys);
|
||||
|
||||
$sortedResult = \array_map(function ($row) use ($globalKeys) {
|
||||
$newRow = array();
|
||||
$sortedResult = array_map(function ($row) use ($globalKeys) {
|
||||
$newRow = [];
|
||||
|
||||
foreach ($globalKeys as $key) {
|
||||
$newRow[] = $this->getDisplayableResult($key, $row[$key]);
|
||||
}
|
||||
foreach ($globalKeys as $key) {
|
||||
$newRow[] = $this->getDisplayableResult($key, $row[$key]);
|
||||
}
|
||||
|
||||
return $newRow;
|
||||
}, $this->result);
|
||||
return $newRow;
|
||||
}, $this->result);
|
||||
|
||||
\array_multisort($sortedResult);
|
||||
array_multisort($sortedResult);
|
||||
|
||||
return array($sortedResult, $exportKeys, $aggregatorKeys, $globalKeys);
|
||||
return [$sortedResult, $exportKeys, $aggregatorKeys, $globalKeys];
|
||||
}
|
||||
|
||||
/**
|
||||
* get an array of aggregator keys. The keys are sorted as asked
|
||||
* by user in the form.
|
||||
* append a form line by aggregator on the formatter form.
|
||||
*
|
||||
* @return string[] an array containing the keys of aggregators
|
||||
* This form allow to choose the aggregator position (row or column) and
|
||||
* the ordering
|
||||
*
|
||||
* @param string $nbAggregators
|
||||
*/
|
||||
protected function getAggregatorKeysSorted()
|
||||
private function appendAggregatorForm(FormBuilderInterface $builder, $nbAggregators)
|
||||
{
|
||||
// empty array for aggregators keys
|
||||
$keys = array();
|
||||
// this association between key and aggregator alias will be used
|
||||
// during sorting
|
||||
$aggregatorKeyAssociation = array();
|
||||
|
||||
foreach ($this->aggregatorsData as $alias => $data) {
|
||||
$aggregator = $this->exportManager->getAggregator($alias);
|
||||
$aggregatorsKeys = $aggregator->getQueryKeys($data);
|
||||
// append the keys from aggregator to the $keys existing array
|
||||
$keys = \array_merge($keys, $aggregatorsKeys);
|
||||
// append the key with the alias, which will be use later for sorting
|
||||
foreach ($aggregatorsKeys as $key) {
|
||||
$aggregatorKeyAssociation[$key] = $alias;
|
||||
}
|
||||
}
|
||||
|
||||
// sort the result using the form
|
||||
usort($keys, function ($a, $b) use ($aggregatorKeyAssociation) {
|
||||
$A = $this->formatterData[$aggregatorKeyAssociation[$a]]['order'];
|
||||
$B = $this->formatterData[$aggregatorKeyAssociation[$b]]['order'];
|
||||
|
||||
if ($A === $B) {
|
||||
return 0;
|
||||
} elseif ($A > $B) {
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* add headers to worksheet
|
||||
*
|
||||
* return the line number where the next content (i.e. result) should
|
||||
* be appended.
|
||||
*
|
||||
* @param Worksheet $worksheet
|
||||
* @param array $aggregatorKeys
|
||||
* @param array $exportKeys
|
||||
* @param int $line
|
||||
* @return int
|
||||
*/
|
||||
protected function addHeaders(
|
||||
Worksheet &$worksheet,
|
||||
array $globalKeys,
|
||||
$line
|
||||
) {
|
||||
// get the displayable form of headers
|
||||
$displayables = array();
|
||||
foreach ($globalKeys as $key) {
|
||||
$displayables[] = $this->translator->trans(
|
||||
$this->getDisplayableResult($key, '_header')
|
||||
);
|
||||
}
|
||||
|
||||
// add headers on worksheet
|
||||
$worksheet->fromArray(
|
||||
$displayables,
|
||||
NULL,
|
||||
'A'.$line);
|
||||
|
||||
return $line + 1;
|
||||
}
|
||||
|
||||
protected function addContentTable(Worksheet $worksheet,
|
||||
$sortedResults,
|
||||
$line
|
||||
) {
|
||||
$worksheet->fromArray(
|
||||
$sortedResults,
|
||||
NULL,
|
||||
'A'.$line);
|
||||
|
||||
return $line + count($sortedResults) + 1;
|
||||
}
|
||||
|
||||
protected function getTitle()
|
||||
{
|
||||
return $this->translator->trans($this->export->getTitle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the displayable result.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
protected function getDisplayableResult($key, $value)
|
||||
{
|
||||
if ($this->cacheDisplayableResultIsInitialized === false) {
|
||||
$this->initializeCache($key);
|
||||
}
|
||||
|
||||
return call_user_func($this->cacheDisplayableResult[$key], $value);
|
||||
}
|
||||
|
||||
protected function initializeCache($key)
|
||||
{
|
||||
/*
|
||||
* this function follows the following steps :
|
||||
*
|
||||
* 1. associate all keys used in result with their export element
|
||||
* (export or aggregator) and data;
|
||||
* 2. associate all keys used in result with all the possible values :
|
||||
* this array will be necessary to call `getLabels`
|
||||
* 3. store the `callable` in an associative array, in cache
|
||||
*/
|
||||
// 1. create an associative array with key and export / aggregator
|
||||
$keysExportElementAssociation = array();
|
||||
// keys for export
|
||||
foreach ($this->export->getQueryKeys($this->exportData) as $key) {
|
||||
$keysExportElementAssociation[$key] = [$this->export,
|
||||
$this->exportData];
|
||||
}
|
||||
// keys for aggregator
|
||||
foreach ($this->aggregatorsData as $alias => $data) {
|
||||
$aggregator = $this->exportManager->getAggregator($alias);
|
||||
|
||||
foreach ($aggregator->getQueryKeys($data) as $key) {
|
||||
$keysExportElementAssociation[$key] = [$aggregator, $data];
|
||||
}
|
||||
}
|
||||
|
||||
// 2. collect all the keys before iteration
|
||||
$keys = array_keys($keysExportElementAssociation);
|
||||
|
||||
$allValues = array();
|
||||
// store all the values in an array
|
||||
foreach ($this->result as $row) {
|
||||
foreach ($keys as $key) {
|
||||
$allValues[$key][] = $row[$key];
|
||||
}
|
||||
}
|
||||
|
||||
// 3. iterate on `keysExportElementAssociation` to store the callable
|
||||
// in cache
|
||||
foreach ($keysExportElementAssociation as $key => list($element, $data)) {
|
||||
$this->cacheDisplayableResult[$key] =
|
||||
$element->getLabels($key, \array_unique($allValues[$key]), $data);
|
||||
}
|
||||
|
||||
// the cache is initialized !
|
||||
$this->cacheDisplayableResultIsInitialized = true;
|
||||
}
|
||||
|
||||
protected function getContentType($format)
|
||||
{
|
||||
switch ($format)
|
||||
{
|
||||
case 'csv':
|
||||
return 'text/csv';
|
||||
case 'ods':
|
||||
return 'application/vnd.oasis.opendocument.spreadsheet';
|
||||
case 'xlsx':
|
||||
return 'application/vnd.openxmlformats-officedocument.'
|
||||
. 'spreadsheetml.sheet';
|
||||
}
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return 'tabular';
|
||||
$builder->add('order', ChoiceType::class, [
|
||||
'choices' => array_combine(
|
||||
range(1, $nbAggregators),
|
||||
range(1, $nbAggregators)
|
||||
),
|
||||
'multiple' => false,
|
||||
'expanded' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -1,163 +1,157 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Export\Formatter;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\MainBundle\Export\ExportManager;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Chill\MainBundle\Export\FormatterInterface;
|
||||
use DateTimeInterface;
|
||||
use LogicException;
|
||||
use OutOfBoundsException;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use function array_key_exists;
|
||||
use function array_keys;
|
||||
use function array_map;
|
||||
use function fopen;
|
||||
use function implode;
|
||||
use function stream_get_contents;
|
||||
use function sys_get_temp_dir;
|
||||
use function tempnam;
|
||||
use function unlink;
|
||||
|
||||
// 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
|
||||
|
||||
/**
|
||||
* Create a CSV List for the export
|
||||
*
|
||||
* @author Champs-Libres <info@champs-libres.coop>
|
||||
* Create a CSV List for the export.
|
||||
*/
|
||||
class SpreadsheetListFormatter implements FormatterInterface
|
||||
{
|
||||
protected $exportAlias;
|
||||
|
||||
protected $exportData;
|
||||
|
||||
/**
|
||||
* This variable cache the labels internally
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $labelsCache = null;
|
||||
|
||||
protected $result = null;
|
||||
|
||||
protected $exportAlias = null;
|
||||
|
||||
protected $exportData = null;
|
||||
|
||||
protected $formatterData = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ExportManager
|
||||
*/
|
||||
protected $exportManager;
|
||||
|
||||
|
||||
protected $formatterData;
|
||||
|
||||
/**
|
||||
* This variable cache the labels internally.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $labelsCache;
|
||||
|
||||
protected $result;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
|
||||
|
||||
public function __construct(TranslatorInterface $translatorInterface, ExportManager $exportManager)
|
||||
{
|
||||
$this->translator = $translatorInterface;
|
||||
$this->exportManager = $exportManager;
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return FormatterInterface::TYPE_LIST;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* build a form, which will be used to collect data required for the execution
|
||||
* of this formatter.
|
||||
*
|
||||
*
|
||||
* @uses appendAggregatorForm
|
||||
* @param FormBuilderInterface $builder
|
||||
*
|
||||
* @param type $exportAlias
|
||||
* @param array $aggregatorAliases
|
||||
*/
|
||||
public function buildForm(
|
||||
FormBuilderInterface $builder,
|
||||
$exportAlias,
|
||||
FormBuilderInterface $builder,
|
||||
$exportAlias,
|
||||
array $aggregatorAliases
|
||||
){
|
||||
) {
|
||||
$builder
|
||||
->add('format', ChoiceType::class, array(
|
||||
'choices' => array(
|
||||
->add('format', ChoiceType::class, [
|
||||
'choices' => [
|
||||
'OpenDocument Format (.ods) (LibreOffice, ...)' => 'ods',
|
||||
'Microsoft Excel 2007-2013 XML (.xlsx) (Microsoft Excel, LibreOffice)' => 'xlsx'
|
||||
),
|
||||
'placeholder' => 'Choose the format'
|
||||
))
|
||||
->add('numerotation', ChoiceType::class, array(
|
||||
'choices' => array(
|
||||
'Microsoft Excel 2007-2013 XML (.xlsx) (Microsoft Excel, LibreOffice)' => 'xlsx',
|
||||
],
|
||||
'placeholder' => 'Choose the format',
|
||||
])
|
||||
->add('numerotation', ChoiceType::class, [
|
||||
'choices' => [
|
||||
'yes' => true,
|
||||
'no' => false
|
||||
),
|
||||
'no' => false,
|
||||
],
|
||||
'expanded' => true,
|
||||
'multiple' => false,
|
||||
'label' => "Add a number on first column",
|
||||
'data' => true
|
||||
));
|
||||
'label' => 'Add a number on first column',
|
||||
'data' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'Spreadsheet list formatter (.xlsx, .ods)';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate a response from the data collected on differents ExportElementInterface
|
||||
*
|
||||
* Generate a response from the data collected on differents ExportElementInterface.
|
||||
*
|
||||
* @param mixed[] $result The result, as given by the ExportInterface
|
||||
* @param mixed[] $formatterData collected from the current form
|
||||
* @param string $exportAlias the id of the current export
|
||||
* @param array $filtersData an array containing the filters data. The key are the filters id, and the value are the data
|
||||
* @param array $aggregatorsData an array containing the aggregators data. The key are the filters id, and the value are the data
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response The response to be shown
|
||||
*/
|
||||
public function getResponse(
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
array $aggregatorsData
|
||||
) {
|
||||
$this->result = $result;
|
||||
$this->exportAlias = $exportAlias;
|
||||
$this->exportData = $exportData;
|
||||
$this->formatterData = $formatterData;
|
||||
|
||||
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
|
||||
$this->prepareHeaders($worksheet);
|
||||
|
||||
|
||||
$i = 1;
|
||||
|
||||
foreach ($result as $row) {
|
||||
$line = array();
|
||||
|
||||
if ($this->formatterData['numerotation'] === true) {
|
||||
$worksheet->setCellValue('A'.($i+1), (string) $i);
|
||||
$line = [];
|
||||
|
||||
if (true === $this->formatterData['numerotation']) {
|
||||
$worksheet->setCellValue('A' . ($i + 1), (string) $i);
|
||||
}
|
||||
|
||||
|
||||
$a = $this->formatterData['numerotation'] ? 'B' : 'A';
|
||||
|
||||
foreach ($row as $key => $value) {
|
||||
$row = $a.($i+1);
|
||||
if ($value instanceof \DateTimeInterface) {
|
||||
$row = $a . ($i + 1);
|
||||
|
||||
if ($value instanceof DateTimeInterface) {
|
||||
$worksheet->setCellValue($row, Date::PHPToExcel($value));
|
||||
$worksheet->getStyle($row)
|
||||
->getNumberFormat()
|
||||
@@ -165,105 +159,93 @@ class SpreadsheetListFormatter implements FormatterInterface
|
||||
} else {
|
||||
$worksheet->setCellValue($row, $this->getLabel($key, $value));
|
||||
}
|
||||
$a ++;
|
||||
++$a;
|
||||
}
|
||||
|
||||
$i++;
|
||||
|
||||
++$i;
|
||||
}
|
||||
|
||||
switch ($this->formatterData['format'])
|
||||
{
|
||||
switch ($this->formatterData['format']) {
|
||||
case 'ods':
|
||||
$writer = \PhpOffice\PhpSpreadsheet\IOFactory
|
||||
::createWriter($spreadsheet, 'Ods');
|
||||
$contentType = "application/vnd.oasis.opendocument.spreadsheet";
|
||||
$contentType = 'application/vnd.oasis.opendocument.spreadsheet';
|
||||
|
||||
break;
|
||||
|
||||
case 'xlsx':
|
||||
$writer = \PhpOffice\PhpSpreadsheet\IOFactory
|
||||
::createWriter($spreadsheet, 'Xlsx');
|
||||
$contentType = 'application/vnd.openxmlformats-officedocument.'
|
||||
. 'spreadsheetml.sheet';
|
||||
|
||||
break;
|
||||
|
||||
case 'csv':
|
||||
$writer = \PhpOffice\PhpSpreadsheet\IOFactory
|
||||
::createWriter($spreadsheet, 'Csv');
|
||||
$contentType = 'text/csv';
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
// this should not happen
|
||||
// throw an exception to ensure that the error is catched
|
||||
throw new \OutOfBoundsException("The format ".$this->formatterData['format'].
|
||||
" is not supported");
|
||||
throw new OutOfBoundsException('The format ' . $this->formatterData['format'] .
|
||||
' is not supported');
|
||||
}
|
||||
|
||||
|
||||
$response = new Response();
|
||||
$response->headers->set('content-type', $contentType);
|
||||
|
||||
$tempfile = \tempnam(\sys_get_temp_dir(), '');
|
||||
|
||||
$tempfile = tempnam(sys_get_temp_dir(), '');
|
||||
$writer->save($tempfile);
|
||||
|
||||
$f = \fopen($tempfile, 'r');
|
||||
$response->setContent(\stream_get_contents($f));
|
||||
|
||||
$f = fopen($tempfile, 'r');
|
||||
$response->setContent(stream_get_contents($f));
|
||||
fclose($f);
|
||||
|
||||
// remove the temp file from disk
|
||||
\unlink($tempfile);
|
||||
|
||||
unlink($tempfile);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* add the headers to the csv file
|
||||
*
|
||||
* @param Worksheet $worksheet
|
||||
*/
|
||||
protected function prepareHeaders(Worksheet $worksheet)
|
||||
|
||||
public function getType()
|
||||
{
|
||||
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
|
||||
// we want to keep the order of the first row. So we will iterate on the first row of the results
|
||||
$first_row = count($this->result) > 0 ? $this->result[0] : array();
|
||||
$header_line = array();
|
||||
|
||||
if ($this->formatterData['numerotation'] === true) {
|
||||
$header_line[] = $this->translator->trans('Number');
|
||||
}
|
||||
|
||||
foreach ($first_row as $key => $value) {
|
||||
$header_line[] = $this->translator->trans(
|
||||
$this->getLabel($key, '_header'));
|
||||
}
|
||||
|
||||
if (count($header_line) > 0) {
|
||||
$worksheet->fromArray($header_line, NULL, 'A1');
|
||||
}
|
||||
return FormatterInterface::TYPE_LIST;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Give the label corresponding to the given key and value.
|
||||
*
|
||||
* Give the label corresponding to the given key and value.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*
|
||||
* @throws LogicException if the label is not found
|
||||
*
|
||||
* @return string
|
||||
* @throws \LogicException if the label is not found
|
||||
*/
|
||||
protected function getLabel($key, $value)
|
||||
{
|
||||
|
||||
if ($this->labelsCache === null) {
|
||||
if (null === $this->labelsCache) {
|
||||
$this->prepareCacheLabels();
|
||||
}
|
||||
|
||||
if (!\array_key_exists($key, $this->labelsCache)){
|
||||
throw new \OutOfBoundsException(sprintf("The key \"%s\" "
|
||||
. "is not present in the list of keys handled by "
|
||||
. "this query. Check your `getKeys` and `getLabels` "
|
||||
. "methods. Available keys are %s.", $key,
|
||||
\implode(", ", \array_keys($this->labelsCache))));
|
||||
|
||||
if (!array_key_exists($key, $this->labelsCache)) {
|
||||
throw new OutOfBoundsException(sprintf(
|
||||
'The key "%s" '
|
||||
. 'is not present in the list of keys handled by '
|
||||
. 'this query. Check your `getKeys` and `getLabels` '
|
||||
. 'methods. Available keys are %s.',
|
||||
$key,
|
||||
implode(', ', array_keys($this->labelsCache))
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
return $this->labelsCache[$key]($value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepare the label cache which will be used by getLabel. This function
|
||||
* should be called only once in the generation lifecycle.
|
||||
@@ -272,14 +254,37 @@ class SpreadsheetListFormatter implements FormatterInterface
|
||||
{
|
||||
$export = $this->exportManager->getExport($this->exportAlias);
|
||||
$keys = $export->getQueryKeys($this->exportData);
|
||||
|
||||
foreach($keys as $key) {
|
||||
|
||||
foreach ($keys as $key) {
|
||||
// get an array with all values for this key if possible
|
||||
$values = \array_map(function ($v) use ($key) { return $v[$key]; }, $this->result);
|
||||
$values = array_map(function ($v) use ($key) { return $v[$key]; }, $this->result);
|
||||
// store the label in the labelsCache property
|
||||
$this->labelsCache[$key] = $export->getLabels($key, $values, $this->exportData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* add the headers to the csv file.
|
||||
*/
|
||||
protected function prepareHeaders(Worksheet $worksheet)
|
||||
{
|
||||
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
|
||||
// we want to keep the order of the first row. So we will iterate on the first row of the results
|
||||
$first_row = count($this->result) > 0 ? $this->result[0] : [];
|
||||
$header_line = [];
|
||||
|
||||
if (true === $this->formatterData['numerotation']) {
|
||||
$header_line[] = $this->translator->trans('Number');
|
||||
}
|
||||
|
||||
foreach ($first_row as $key => $value) {
|
||||
$header_line[] = $this->translator->trans(
|
||||
$this->getLabel($key, '_header')
|
||||
);
|
||||
}
|
||||
|
||||
if (count($header_line) > 0) {
|
||||
$worksheet->fromArray($header_line, null, 'A1');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user