mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-05 06:14: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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user