mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-27 18:13:48 +00:00
fix deprecations: use the fqcn for choiceType
This commit is contained in:
@@ -30,7 +30,7 @@ use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
// command to get the report with curl : curl --user "center a_social:password" "http://localhost:8000/fr/exports/generate/count_person?export[filters][person_gender_filter][enabled]=&export[filters][person_nationality_filter][enabled]=&export[filters][person_nationality_filter][form][nationalities]=&export[aggregators][person_nationality_aggregator][order]=1&export[aggregators][person_nationality_aggregator][form][group_by_level]=country&export[submit]=&export[_token]=RHpjHl389GrK-bd6iY5NsEqrD5UKOTHH40QKE9J1edU" --globoff
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @deprecated this formatter is not used any more.
|
||||
@@ -42,49 +42,49 @@ class CSVFormatter implements FormatterInterface
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
|
||||
protected $result;
|
||||
|
||||
|
||||
protected $formatterData;
|
||||
|
||||
|
||||
protected $export;
|
||||
|
||||
|
||||
protected $aggregators;
|
||||
|
||||
|
||||
protected $exportData;
|
||||
|
||||
|
||||
protected $aggregatorsData;
|
||||
|
||||
|
||||
protected $filtersData;
|
||||
|
||||
|
||||
protected $labels;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ExportManager
|
||||
*/
|
||||
protected $exportManager;
|
||||
|
||||
|
||||
public function __construct(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 FormBuilderInterface $builder
|
||||
* @param type $exportAlias
|
||||
@@ -94,7 +94,7 @@ class CSVFormatter implements FormatterInterface
|
||||
{
|
||||
$aggregators = $this->exportManager->getAggregators($aggregatorAliases);
|
||||
$nb = count($aggregatorAliases);
|
||||
|
||||
|
||||
foreach ($aggregators as $alias => $aggregator) {
|
||||
$builderAggregator = $builder->create($alias, FormType::class, array(
|
||||
'label' => $aggregator->getTitle(),
|
||||
@@ -104,19 +104,19 @@ class CSVFormatter implements FormatterInterface
|
||||
$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
|
||||
*
|
||||
* 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', 'choice', array(
|
||||
$builder->add('order', ChoiceType::class, array(
|
||||
'choices' => array_combine(
|
||||
range(1, $nbAggregators),
|
||||
range(1, $nbAggregators)
|
||||
@@ -124,8 +124,8 @@ class CSVFormatter implements FormatterInterface
|
||||
'multiple' => false,
|
||||
'expanded' => false
|
||||
));
|
||||
|
||||
$builder->add('position', 'choice', array(
|
||||
|
||||
$builder->add('position', ChoiceType::class, array(
|
||||
'choices' => array(
|
||||
'row' => 'r',
|
||||
'column' => 'c'
|
||||
@@ -135,13 +135,13 @@ class CSVFormatter implements FormatterInterface
|
||||
'expanded' => false
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
public function getResponse(
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
array $aggregatorsData
|
||||
) {
|
||||
$this->result = $result;
|
||||
@@ -153,25 +153,25 @@ class CSVFormatter implements FormatterInterface
|
||||
$this->aggregatorsData = $aggregatorsData;
|
||||
$this->labels = $this->gatherLabels();
|
||||
$this->filtersData = $filtersData;
|
||||
|
||||
|
||||
$response = new Response();
|
||||
$response->setStatusCode(200);
|
||||
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
|
||||
//$response->headers->set('Content-Disposition','attachment; filename="export.csv"');
|
||||
|
||||
$response->setContent($this->generateContent());
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* If two aggregators have the same order, the second given will be placed
|
||||
* after. This is not significant for the first ordering.
|
||||
*
|
||||
*
|
||||
* @param type $formatterData
|
||||
* @return type
|
||||
*/
|
||||
@@ -179,11 +179,11 @@ class CSVFormatter implements FormatterInterface
|
||||
{
|
||||
$this->formatterData = $formatterData;
|
||||
uasort($this->formatterData, function($a, $b) {
|
||||
|
||||
|
||||
return ($a['order'] <= $b['order'] ? -1 : 1);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
protected function generateContent()
|
||||
{
|
||||
$rowKeysNb = count($this->getRowHeaders());
|
||||
@@ -195,7 +195,7 @@ class CSVFormatter implements FormatterInterface
|
||||
/* @var $data string[] the data of the csv file */
|
||||
$contentData = array();
|
||||
$content = array();
|
||||
|
||||
|
||||
function findColumnPosition(&$columnHeaders, $columnToFind) {
|
||||
$i = 0;
|
||||
foreach($columnHeaders as $set) {
|
||||
@@ -204,13 +204,13 @@ class CSVFormatter implements FormatterInterface
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
|
||||
|
||||
//we didn't find it, adding the column
|
||||
$columnHeaders[] = $columnToFind;
|
||||
|
||||
|
||||
return $i++;
|
||||
}
|
||||
|
||||
|
||||
// create a file pointer connected to the output stream
|
||||
$output = fopen('php://output', 'w');
|
||||
|
||||
@@ -218,18 +218,18 @@ class CSVFormatter implements FormatterInterface
|
||||
fputcsv($output, array($this->translator->trans($this->export->getTitle())));
|
||||
//blank line
|
||||
fputcsv($output, array(""));
|
||||
|
||||
|
||||
// add filtering description
|
||||
foreach($this->gatherFiltersDescriptions() as $desc) {
|
||||
fputcsv($output, array($desc));
|
||||
}
|
||||
// blank line
|
||||
fputcsv($output, array(""));
|
||||
|
||||
|
||||
// iterate on result to : 1. populate row headers, 2. populate column headers, 3. add result
|
||||
foreach ($results as $row) {
|
||||
$rowHeaders = array_slice($row, 0, $rowKeysNb);
|
||||
|
||||
|
||||
//first line : we create line and adding them row headers
|
||||
if (!isset($line)) {
|
||||
$line = array_slice($row, 0, $rowKeysNb);
|
||||
@@ -245,49 +245,49 @@ class CSVFormatter implements FormatterInterface
|
||||
/* @var $columns string[] the column for this row */
|
||||
$columns = array_slice($row, $rowKeysNb, $columnKeysNb);
|
||||
$columnPosition = findColumnPosition($columnHeaders, $columns);
|
||||
|
||||
|
||||
//fill with blank at the position given by the columnPosition + nbRowHeaders
|
||||
for ($i=0; $i < $columnPosition; $i++) {
|
||||
if (!isset($line[$rowKeysNb + $i])) {
|
||||
$line[$rowKeysNb + $i] = "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$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++) {
|
||||
$line = array_fill(0, $rowKeysNb, '');
|
||||
|
||||
|
||||
foreach($columnHeaders as $set) {
|
||||
$line[] = $set[$i];
|
||||
}
|
||||
|
||||
|
||||
$content[] = $line;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//row title headers
|
||||
$headerLine = array();
|
||||
foreach($this->getRowHeaders() as $headerKey) {
|
||||
$headerLine[] = array_key_exists('_header', $this->labels[$headerKey]) ?
|
||||
$headerLine[] = array_key_exists('_header', $this->labels[$headerKey]) ?
|
||||
$this->labels[$headerKey]['_header'] : '';
|
||||
}
|
||||
foreach($this->export->getQueryKeys($this->exportData) as $key) {
|
||||
$headerLine[] = array_key_exists('_header', $this->labels[$key]) ?
|
||||
$headerLine[] = array_key_exists('_header', $this->labels[$key]) ?
|
||||
$this->labels[$key]['_header'] : '';
|
||||
}
|
||||
fputcsv($output, $headerLine);
|
||||
unset($headerLine); //free memory
|
||||
|
||||
|
||||
//generate CSV
|
||||
foreach($content as $line) {
|
||||
fputcsv($output, $line);
|
||||
@@ -295,14 +295,14 @@ class CSVFormatter implements FormatterInterface
|
||||
foreach($contentData as $line) {
|
||||
fputcsv($output, $line);
|
||||
}
|
||||
|
||||
|
||||
$text = stream_get_contents($output);
|
||||
fclose($output);
|
||||
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private function getOrderedResults()
|
||||
{
|
||||
$r = array();
|
||||
@@ -312,24 +312,24 @@ class CSVFormatter implements FormatterInterface
|
||||
$columnKeys = $this->getColumnHeaders();
|
||||
$resultsKeys = $this->export->getQueryKeys($this->exportData);
|
||||
$headers = array_merge($rowKeys, $columnKeys);
|
||||
|
||||
|
||||
foreach ($results as $row) {
|
||||
$line = array();
|
||||
foreach ($headers as $key) {
|
||||
|
||||
|
||||
$line[] = call_user_func($labels[$key], $row[$key]);
|
||||
}
|
||||
|
||||
|
||||
//append result
|
||||
foreach ($resultsKeys as $key) {
|
||||
$line[] = call_user_func($labels[$key], $row[$key]);
|
||||
}
|
||||
|
||||
|
||||
$r[] = $line;
|
||||
}
|
||||
|
||||
|
||||
array_multisort($r);
|
||||
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
@@ -338,14 +338,14 @@ class CSVFormatter implements FormatterInterface
|
||||
{
|
||||
return $this->getPositionnalHeaders('r');
|
||||
}
|
||||
|
||||
|
||||
protected function getColumnHeaders()
|
||||
{
|
||||
return $this->getPositionnalHeaders('c');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param string $position may be 'c' (column) or 'r' (row)
|
||||
* @return string[]
|
||||
* @throws \RuntimeException
|
||||
@@ -359,19 +359,19 @@ class CSVFormatter implements FormatterInterface
|
||||
. "aggregator with alias $alias, but the export do not "
|
||||
. "contains data about it");
|
||||
}
|
||||
|
||||
|
||||
$aggregator = $this->aggregators[$alias];
|
||||
|
||||
|
||||
if ($data['position'] === $position) {
|
||||
$headers = array_merge($headers, $aggregator->getQueryKeys($this->aggregatorsData[$alias]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $headers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param mixed $result
|
||||
* @param \Chill\MainBundle\Export\AggregatorInterface[] $aggregators
|
||||
*/
|
||||
@@ -382,14 +382,14 @@ class CSVFormatter implements FormatterInterface
|
||||
$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) {
|
||||
@@ -398,57 +398,57 @@ class CSVFormatter implements FormatterInterface
|
||||
. "the aggregator with alias '".$alias."' but is not "
|
||||
. "present in results");
|
||||
}
|
||||
|
||||
|
||||
return $row[$key];
|
||||
}, $this->result);
|
||||
$labels[$key] = $aggregator->getLabels($key, array_unique($values),
|
||||
$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) {
|
||||
$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[0],
|
||||
$statement[1],
|
||||
isset($statement[2]) ? $statement[2] : null,
|
||||
isset($statement[3]) ? $statement[3] : null);
|
||||
@@ -456,8 +456,8 @@ class CSVFormatter implements FormatterInterface
|
||||
$descriptions[] = $statement;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $descriptions;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
@@ -38,119 +38,119 @@ 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
|
||||
*
|
||||
* @var type
|
||||
*/
|
||||
protected $result;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var type
|
||||
*
|
||||
* @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
|
||||
* @var type
|
||||
*/
|
||||
//protected $aggregators;
|
||||
|
||||
|
||||
/**
|
||||
* array containing value of export form
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $exportData;
|
||||
|
||||
|
||||
/**
|
||||
* an array where keys are the aggregators aliases and
|
||||
* values are the data
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var type
|
||||
*
|
||||
* @var type
|
||||
*/
|
||||
protected $aggregatorsData;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $filtersData;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* replaced when `getResponse` is called.
|
||||
*
|
||||
* @var array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
//protected $labels;
|
||||
|
||||
|
||||
/**
|
||||
* temporary file to store spreadsheet
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $tempfile;
|
||||
|
||||
|
||||
/**
|
||||
* cache for displayable result.
|
||||
*
|
||||
*
|
||||
* This cache is reset when `getResponse` is called.
|
||||
*
|
||||
* The array's keys are the keys in the raw result, and
|
||||
* values are the callable which will transform the raw result to
|
||||
*
|
||||
* The array's keys are the keys in the raw result, and
|
||||
* values are the callable which will transform the raw result to
|
||||
* displayable result.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $cacheDisplayableResult;
|
||||
|
||||
|
||||
/**
|
||||
* Whethe `cacheDisplayableResult` is initialized or not.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
private $cacheDisplayableResultIsInitialized = false;
|
||||
|
||||
|
||||
public function __construct(TranslatorInterface $translatorInterface, ExportManager $exportManager)
|
||||
{
|
||||
$this->translator = $translatorInterface;
|
||||
$this->exportManager = $exportManager;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function buildForm(
|
||||
FormBuilderInterface $builder,
|
||||
$exportAlias,
|
||||
FormBuilderInterface $builder,
|
||||
$exportAlias,
|
||||
array $aggregatorAliases
|
||||
) {
|
||||
// choosing between formats
|
||||
@@ -163,11 +163,11 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
'choices_as_values' => true,
|
||||
'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(),
|
||||
@@ -177,19 +177,19 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
$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
|
||||
*
|
||||
* 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', 'choice', array(
|
||||
$builder->add('order', ChoiceType::class, array(
|
||||
'choices' => array_combine(
|
||||
range(1, $nbAggregators),
|
||||
range(1, $nbAggregators)
|
||||
@@ -197,7 +197,7 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
'multiple' => false,
|
||||
'expanded' => false
|
||||
));
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function getName()
|
||||
@@ -206,11 +206,11 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
}
|
||||
|
||||
public function getResponse(
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
$result,
|
||||
$formatterData,
|
||||
$exportAlias,
|
||||
array $exportData,
|
||||
array $filtersData,
|
||||
array $aggregatorsData
|
||||
) {
|
||||
// store all data when the process is initiated
|
||||
@@ -220,46 +220,46 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
$this->exportData = $exportData;
|
||||
$this->filtersData = $filtersData;
|
||||
$this->aggregatorsData = $aggregatorsData;
|
||||
|
||||
|
||||
// reset cache
|
||||
$this->cacheDisplayableResult = array();
|
||||
$this->cacheDisplayableResultIsInitialized = false;
|
||||
|
||||
|
||||
$response = new Response();
|
||||
$response->headers->set('Content-Type',
|
||||
$response->headers->set('Content-Type',
|
||||
$this->getContentType($this->formatterData['format']));
|
||||
|
||||
|
||||
$this->tempfile = \tempnam(\sys_get_temp_dir(), '');
|
||||
$this->generatecontent();
|
||||
|
||||
|
||||
$f = \fopen($this->tempfile, 'r');
|
||||
$response->setContent(\stream_get_contents($f));
|
||||
fclose($f);
|
||||
|
||||
|
||||
// remove the temp file from disk
|
||||
\unlink($this->tempfile);
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate the content and write it to php://temp
|
||||
*/
|
||||
protected function generateContent()
|
||||
{
|
||||
list($spreadsheet, $worksheet) = $this->createSpreadsheet();
|
||||
|
||||
|
||||
$this->addTitleToWorkSheet($worksheet);
|
||||
$line = $this->addFiltersDescription($worksheet);
|
||||
|
||||
|
||||
// at this point, we are going to sort retsults for an easier manipulation
|
||||
list($sortedResult, $exportKeys, $aggregatorKeys, $globalKeys) =
|
||||
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':
|
||||
@@ -279,32 +279,32 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
// throw an exception to ensure that the error is catched
|
||||
throw new \LogicException();
|
||||
}
|
||||
|
||||
|
||||
$writer->save($this->tempfile);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a spreadsheet and a working worksheet
|
||||
*
|
||||
*
|
||||
* @return array where 1st member is spreadsheet, 2nd is worksheet
|
||||
*/
|
||||
protected function createSpreadsheet()
|
||||
{
|
||||
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
|
||||
// setting the worksheet title and code name
|
||||
$worksheet
|
||||
->setTitle($this->getTitle())
|
||||
->setCodeName('result');
|
||||
|
||||
|
||||
return [$spreadsheet, $worksheet];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add the title to the worksheet and merge the cell containing
|
||||
* the title
|
||||
*
|
||||
*
|
||||
* @param Worksheet $worksheet
|
||||
*/
|
||||
protected function addTitleToWorkSheet(Worksheet &$worksheet)
|
||||
@@ -312,50 +312,50 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
$worksheet->setCellValue('A1', $this->getTitle());
|
||||
$worksheet->mergeCells('A1:G1');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add filter description since line 3.
|
||||
*
|
||||
*
|
||||
* return the line number after the last description
|
||||
*
|
||||
*
|
||||
* @param Worksheet $worksheet
|
||||
* @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[0],
|
||||
isset($description[1]) ? $description[1] : []
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
$worksheet->setCellValue('A'.$line, $description);
|
||||
$line ++;
|
||||
$line ++;
|
||||
}
|
||||
|
||||
|
||||
return $line;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* sort the results, and return an array where :
|
||||
* sort the results, and return an array where :
|
||||
* - 0 => sorted results
|
||||
* - 1 => export keys
|
||||
* - 2 => aggregator keys
|
||||
* - 2 => aggregator keys
|
||||
* - 3 => global keys (aggregator keys and export keys)
|
||||
*
|
||||
*
|
||||
* Example, assuming that the result contains two aggregator keys :
|
||||
*
|
||||
* array in result :
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example, assuming that the result contains two aggregator keys :
|
||||
*
|
||||
* array in result :
|
||||
*
|
||||
* ```
|
||||
* array(
|
||||
* array( //row 1
|
||||
@@ -370,54 +370,54 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
* )
|
||||
* )
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* the sorted result will be :
|
||||
*
|
||||
*
|
||||
* ```
|
||||
* array(
|
||||
* array( 2, 3, 1 ),
|
||||
* array( 5, 6, 4 )
|
||||
* )
|
||||
* ```
|
||||
*
|
||||
*
|
||||
*/
|
||||
protected function sortResult()
|
||||
{
|
||||
// get the keys for each row
|
||||
$exportKeys = $this->export->getQueryKeys($this->exportData);
|
||||
$aggregatorKeys = $this->getAggregatorKeysSorted();
|
||||
|
||||
|
||||
$globalKeys = \array_merge($aggregatorKeys, $exportKeys);
|
||||
|
||||
|
||||
$sortedResult = \array_map(function ($row) use ($globalKeys) {
|
||||
$newRow = array();
|
||||
|
||||
|
||||
foreach ($globalKeys as $key) {
|
||||
$newRow[] = $this->getDisplayableResult($key, $row[$key]);
|
||||
}
|
||||
|
||||
|
||||
return $newRow;
|
||||
}, $this->result);
|
||||
|
||||
|
||||
\array_multisort($sortedResult);
|
||||
|
||||
|
||||
return array($sortedResult, $exportKeys, $aggregatorKeys, $globalKeys);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get an array of aggregator keys. The keys are sorted as asked
|
||||
* by user in the form.
|
||||
*
|
||||
*
|
||||
* @return string[] an array containing the keys of aggregators
|
||||
*/
|
||||
protected function getAggregatorKeysSorted()
|
||||
{
|
||||
// empty array for aggregators keys
|
||||
$keys = array();
|
||||
// this association between key and aggregator alias will be used
|
||||
// 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);
|
||||
@@ -428,12 +428,12 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
$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) {
|
||||
@@ -441,18 +441,18 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
} 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
|
||||
@@ -460,8 +460,8 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
* @return int
|
||||
*/
|
||||
protected function addHeaders(
|
||||
Worksheet &$worksheet,
|
||||
array $globalKeys,
|
||||
Worksheet &$worksheet,
|
||||
array $globalKeys,
|
||||
$line
|
||||
) {
|
||||
// get the displayable form of headers
|
||||
@@ -471,36 +471,36 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
$this->getDisplayableResult($key, '_header')
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// add headers on worksheet
|
||||
$worksheet->fromArray(
|
||||
$displayables,
|
||||
NULL,
|
||||
$displayables,
|
||||
NULL,
|
||||
'A'.$line);
|
||||
|
||||
|
||||
return $line + 1;
|
||||
}
|
||||
|
||||
|
||||
protected function addContentTable(Worksheet $worksheet,
|
||||
$sortedResults,
|
||||
$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
|
||||
@@ -510,40 +510,40 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
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
|
||||
*
|
||||
* 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`
|
||||
* 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,
|
||||
$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
|
||||
|
||||
// 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) {
|
||||
@@ -551,18 +551,18 @@ class SpreadSheetFormatter implements FormatterInterface
|
||||
$allValues[$key][] = $row[$key];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 3. iterate on `keysExportElementAssociation` to store the callable
|
||||
// in cache
|
||||
foreach ($keysExportElementAssociation as $key => list($element, $data)) {
|
||||
$this->cacheDisplayableResult[$key] =
|
||||
$this->cacheDisplayableResult[$key] =
|
||||
$element->getLabels($key, \array_unique($allValues[$key]), $data);
|
||||
}
|
||||
|
||||
|
||||
// the cache is initialized !
|
||||
$this->cacheDisplayableResultIsInitialized = true;
|
||||
}
|
||||
|
||||
|
||||
protected function getContentType($format)
|
||||
{
|
||||
switch ($format)
|
||||
|
Reference in New Issue
Block a user