improve export to add filtering parameters on result

The csv formatter now add a message about filtering parameters.

The filterInterface has been adapted to render a message.
This commit is contained in:
Julien Fastré 2016-12-01 22:29:41 +01:00
parent 2a21787ad4
commit 12dbb587df
3 changed files with 79 additions and 10 deletions

View File

@ -429,7 +429,7 @@ class ExportManager
/* @var $formatter Formatter\CSVFormatter */
$formatter = $this->getFormatter($this->getFormatterAlias($data));
$filters = array();
$filtersData = array();
$aggregatorsData = array();
if ($query instanceof QueryBuilder) {
@ -440,12 +440,18 @@ class ExportManager
}
}
$filters = $this->retrieveUsedFilters($data[ExportType::FILTER_KEY]);
foreach($filters as $alias => $filter) {
$filtersData[$alias] = $data[ExportType::FILTER_KEY][$alias]['form'];
}
return $formatter->getResponse(
$result,
$formatterData,
$exportAlias,
$data[ExportType::EXPORT_KEY],
$filters,
$filtersData,
$aggregatorsData);
}

View File

@ -33,5 +33,38 @@ namespace Chill\MainBundle\Export;
*/
interface FilterInterface extends ModifierInterface
{
const STRING_FORMAT = 'string';
/**
* Describe the filtering action.
*
* The description will be inserted in report to remains to the user the
* filters applyied on this report.
*
* Should return a statement about the filtering action, like :
* "filtering by date: from xxxx-xx-xx to xxxx-xx-xx" or
* "filtering by nationality: only Germany, France"
*
* The format will be determined by the $format. Currently, only 'string' is
* supported, later some 'html' will be added. The filter should always
* implement the 'string' format and fallback to it if other format are used.
*
* If no i18n is necessery, or if the filter translate the string by himself,
* this function should return a string. If the filter does not do any translation,
* the return parameter should be an array, where
*
* - the first element is the string,
* - and the second an array of parameters (may be an empty array)
* - the 3rd the domain (optional)
* - the 4th the locale (optional)
*
* Example: `array('my string with %parameter%', ['%parameter%' => 'good news'], 'mydomain', 'mylocale')`
*
* @param array $data
* @param string $format the format
* @return string|array a string with the data or, if translatable, an array where first element is string, second elements is an array of arguments
*/
public function describeAction($data, $format = 'string');
}

View File

@ -54,6 +54,8 @@ class CSVFormatter implements FormatterInterface
protected $aggregatorsData;
protected $filtersData;
protected $labels;
/**
@ -133,14 +135,6 @@ class CSVFormatter implements FormatterInterface
));
}
/**
*
* @param mixed $result
* @param mixed $data
* @param \Chill\MainBundle\Export\ExportInterface $export
* @param \Chill\MainBundle\Export\FilterInterface[] $filters
* @param \Chill\MainBundle\Export\AggregatorInterface[] $aggregators
*/
public function getResponse(
$result,
$formatterData,
@ -157,6 +151,7 @@ class CSVFormatter implements FormatterInterface
$this->exportData = $exportData;
$this->aggregatorsData = $aggregatorsData;
$this->labels = $this->gatherLabels();
$this->filtersData = $filtersData;
$response = new Response();
$response->setStatusCode(200);
@ -223,6 +218,13 @@ class CSVFormatter implements FormatterInterface
//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);
@ -434,4 +436,32 @@ class CSVFormatter implements FormatterInterface
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;
}
}