fix deprecations: use the fqcn for choiceType

This commit is contained in:
nobohan
2018-04-04 10:29:57 +02:00
parent c4f7256236
commit 56a695e66e
6 changed files with 269 additions and 268 deletions

View File

@@ -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;
}
}