mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 05:44:24 +00:00
replacing the response of Export::getLabel by a Closure
This commit is contained in:
parent
85dfb222ae
commit
4bfe4b361f
@ -116,9 +116,9 @@ interface ExportInterface extends ExportElementInterface
|
|||||||
* have, as value, a string for the header. See example in return declaration
|
* have, as value, a string for the header. See example in return declaration
|
||||||
*
|
*
|
||||||
* @param string $key The column key, as added in the query
|
* @param string $key The column key, as added in the query
|
||||||
* @param mixed[] $values The values from the result. Each value should be unique. Example: array('FR', 'BE', 'CZ')
|
* @param mixed[] $values The values from the result. if there are duplicates, those might be given twice. Example: array('FR', 'BE', 'CZ', 'FR', 'BE', 'FR')
|
||||||
* @param mixed $data The data from the form
|
* @param mixed $data The data from the export's form (as defined in `buildForm`
|
||||||
* @return string[] where keys are the identifier given by result, and the value a string which should be displayed to the user. A special key '_header' should be added for the header. Example: array('FR' => 'France', 'BE' => 'Belgium', 'CZ' => 'Tchécoslovaquie', '_header' => 'Pays')
|
* @return \Closure where the first argument is the value, and the function should return the label to show in the formatted file. Example : `function($countryCode) use ($countries) { return $countries[$countryCode]->getName(); }`
|
||||||
*/
|
*/
|
||||||
public function getLabels($key, array $values, $data);
|
public function getLabels($key, array $values, $data);
|
||||||
|
|
||||||
|
@ -398,9 +398,9 @@ class ExportManager
|
|||||||
//handle aggregators
|
//handle aggregators
|
||||||
$this->handleAggregators($export, $qb, $data[ExportType::AGGREGATOR_KEY], $centers);
|
$this->handleAggregators($export, $qb, $data[ExportType::AGGREGATOR_KEY], $centers);
|
||||||
|
|
||||||
$this->logger->debug('current query is '.$qb->getDQL(), array(
|
// $this->logger->debug('current query is '.$qb->getDQL(), array(
|
||||||
'class' => self::class, 'function' => __FUNCTION__
|
// 'class' => self::class, 'function' => __FUNCTION__
|
||||||
));
|
// ));
|
||||||
|
|
||||||
$result = $export->getResult($qb, $data[ExportType::EXPORT_KEY]);
|
$result = $export->getResult($qb, $data[ExportType::EXPORT_KEY]);
|
||||||
|
|
||||||
|
@ -19,13 +19,12 @@
|
|||||||
|
|
||||||
namespace Chill\MainBundle\Export\Formatter;
|
namespace Chill\MainBundle\Export\Formatter;
|
||||||
|
|
||||||
use Chill\MainBundle\Export\ExportInterface;
|
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Chill\MainBundle\Export\FormatterInterface;
|
use Chill\MainBundle\Export\FormatterInterface;
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Chill\MainBundle\Export\ExportManager;
|
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
|
// 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
|
||||||
|
|
||||||
@ -50,6 +49,8 @@ class CSVListFormatter implements FormatterInterface
|
|||||||
|
|
||||||
protected $exportData = null;
|
protected $exportData = null;
|
||||||
|
|
||||||
|
protected $formatterData = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @var ExportManager
|
* @var ExportManager
|
||||||
@ -94,7 +95,17 @@ class CSVListFormatter implements FormatterInterface
|
|||||||
$exportAlias,
|
$exportAlias,
|
||||||
array $aggregatorAliases
|
array $aggregatorAliases
|
||||||
){
|
){
|
||||||
// do nothing
|
$builder->add('numerotation', ChoiceType::class, array(
|
||||||
|
'choices' => array(
|
||||||
|
'yes' => true,
|
||||||
|
'no' => false
|
||||||
|
),
|
||||||
|
'expanded' => true,
|
||||||
|
'multiple' => false,
|
||||||
|
'label' => "Add a number on first column",
|
||||||
|
'choices_as_values' => true,
|
||||||
|
'data' => true
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -118,17 +129,27 @@ class CSVListFormatter implements FormatterInterface
|
|||||||
$this->result = $result;
|
$this->result = $result;
|
||||||
$this->exportAlias = $exportAlias;
|
$this->exportAlias = $exportAlias;
|
||||||
$this->exportData = $exportData;
|
$this->exportData = $exportData;
|
||||||
|
$this->formatterData = $formatterData;
|
||||||
|
|
||||||
$output = fopen('php://output', 'w');
|
$output = fopen('php://output', 'w');
|
||||||
|
|
||||||
$this->prepareHeaders($output);
|
$this->prepareHeaders($output);
|
||||||
|
|
||||||
|
$i = 1;
|
||||||
foreach ($result as $row) {
|
foreach ($result as $row) {
|
||||||
$line = array();
|
$line = array();
|
||||||
|
|
||||||
|
if ($this->formatterData['numerotation'] === true) {
|
||||||
|
$line[] = $i;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($row as $key => $value) {
|
foreach ($row as $key => $value) {
|
||||||
$line[] = $this->getLabel($key, $value);
|
$line[] = $this->getLabel($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
fputcsv($output, $line);
|
fputcsv($output, $line);
|
||||||
|
|
||||||
|
$i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$csvContent = stream_get_contents($output);
|
$csvContent = stream_get_contents($output);
|
||||||
@ -144,6 +165,11 @@ class CSVListFormatter implements FormatterInterface
|
|||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add the headers to the csv file
|
||||||
|
*
|
||||||
|
* @param resource $output
|
||||||
|
*/
|
||||||
protected function prepareHeaders($output)
|
protected function prepareHeaders($output)
|
||||||
{
|
{
|
||||||
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
|
$keys = $this->exportManager->getExport($this->exportAlias)->getQueryKeys($this->exportData);
|
||||||
@ -151,6 +177,10 @@ class CSVListFormatter implements FormatterInterface
|
|||||||
$first_row = count($this->result) > 0 ? $this->result[0] : array();
|
$first_row = count($this->result) > 0 ? $this->result[0] : array();
|
||||||
$header_line = array();
|
$header_line = array();
|
||||||
|
|
||||||
|
if ($this->formatterData['numerotation'] === true) {
|
||||||
|
$header_line[] = $this->translator->trans('Number');
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($first_row as $key => $value) {
|
foreach ($first_row as $key => $value) {
|
||||||
$header_line[] = $this->getLabel($key, '_header');
|
$header_line[] = $this->getLabel($key, '_header');
|
||||||
}
|
}
|
||||||
@ -160,29 +190,36 @@ class CSVListFormatter implements FormatterInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Give the label corresponding to the given key and value.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @param string $value
|
||||||
|
* @return string
|
||||||
|
* @throws \LogicException if the label is not found
|
||||||
|
*/
|
||||||
protected function getLabel($key, $value)
|
protected function getLabel($key, $value)
|
||||||
{
|
{
|
||||||
|
|
||||||
if ($this->labelsCache === null) {
|
if ($this->labelsCache === null) {
|
||||||
$this->prepareLabels();
|
$this->prepareCacheLabels();
|
||||||
}
|
|
||||||
|
|
||||||
if (!isset($this->labelsCache[$key][$value])) {
|
|
||||||
throw new \LogicException("The label for key $key and value $value was not given "
|
|
||||||
. "by the export, aggregator or filter responsible for this key.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->labelsCache[$key][$value];
|
return $this->labelsCache[$key]($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function prepareLabels()
|
/**
|
||||||
|
* Prepare the label cache which will be used by getLabel. This function
|
||||||
|
* should be called only once in the generation lifecycle.
|
||||||
|
*/
|
||||||
|
protected function prepareCacheLabels()
|
||||||
{
|
{
|
||||||
$export = $this->exportManager->getExport($this->exportAlias);
|
$export = $this->exportManager->getExport($this->exportAlias);
|
||||||
$keys = $export->getQueryKeys($this->exportData);
|
$keys = $export->getQueryKeys($this->exportData);
|
||||||
|
|
||||||
foreach($keys as $key) {
|
foreach($keys as $key) {
|
||||||
// get an array with all values for this key
|
// get an array with all values for this key if possible
|
||||||
$values = array_unique(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
|
// store the label in the labelsCache property
|
||||||
$this->labelsCache[$key] = $export->getLabels($key, $values, $this->exportData);
|
$this->labelsCache[$key] = $export->getLabels($key, $values, $this->exportData);
|
||||||
}
|
}
|
||||||
|
@ -105,3 +105,9 @@ Circle: Cercle
|
|||||||
Circle edit: Modification du cercle
|
Circle edit: Modification du cercle
|
||||||
Circle creation: Création d'un cercle
|
Circle creation: Création d'un cercle
|
||||||
Create a new circle: Créer un nouveau cercle
|
Create a new circle: Créer un nouveau cercle
|
||||||
|
|
||||||
|
#export
|
||||||
|
|
||||||
|
#CSV List Formatter
|
||||||
|
Add a number on first column: La première colonne est un numéro
|
||||||
|
Number: Numéro
|
Loading…
x
Reference in New Issue
Block a user