Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate totals row after applying search #22895

Draft
wants to merge 1 commit into
base: 5.x-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 69 additions & 41 deletions core/API/DataTableGenericFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ public function __construct($request, $report)
*
* @param DataTable $table
*/
public function filter($table)
public function filter($table, $filterToApplyAfterPatterns = null)
{
$this->applyGenericFilters($table);
$this->applyGenericFilters($table, self::getPatternFiltersToApply());
if (is_callable($filterToApplyAfterPatterns)) {
$table = $filterToApplyAfterPatterns($table);
}
$this->applyGenericFilters($table, self::getOtherFiltersToApply());
}

/**
Expand All @@ -79,47 +83,71 @@ public function disableFilters($filterNames)
*/
public static function getGenericFiltersInformation()
{
return array(
array('Pattern',
array(
'filter_column' => array('string', 'label'),
'filter_pattern' => array('string')
)),
array('PatternRecursive',
array(
'filter_column_recursive' => array('string', 'label'),
'filter_pattern_recursive' => array('string'),
)),
array('ExcludeLowPopulation',
array(
'filter_excludelowpop' => array('string'),
'filter_excludelowpop_value' => array('float', '0'),
)),
array('Sort',
array(
'filter_sort_column' => array('string'),
'filter_sort_order' => array('string', 'desc'),
$naturalSort = true,
$recursiveSort = true,
'filter_sort_column_secondary' => true
)),
array('Truncate',
array(
'filter_truncate' => array('integer'),
)),
array('Limit',
array(
'filter_offset' => array('integer', '0'),
'filter_limit' => array('integer'),
'keep_summary_row' => array('integer', '0'),
))
return array_merge(
self::getPatternFiltersToApply(),
self::getOtherFiltersToApply()
);
}

private function getGenericFiltersHavingDefaultValues()
private static function getPatternFiltersToApply(): array
{
return [
[
'Pattern',
[
'filter_column' => ['string', 'label'],
'filter_pattern' => ['string']
]
],
[
'PatternRecursive',
[
'filter_column_recursive' => ['string', 'label'],
'filter_pattern_recursive' => ['string'],
]
],
];
}

private static function getOtherFiltersToApply(): array
{
$filters = self::getGenericFiltersInformation();
return [
[
'ExcludeLowPopulation',
[
'filter_excludelowpop' => ['string'],
'filter_excludelowpop_value' => ['float', '0'],
]
],
[
'Sort',
[
'filter_sort_column' => ['string'],
'filter_sort_order' => ['string', 'desc'],
$naturalSort = true,
$recursiveSort = true,
'filter_sort_column_secondary' => true
]
],
[
'Truncate',
[
'filter_truncate' => ['integer'],
]
],
[
'Limit',
[
'filter_offset' => ['integer', '0'],
'filter_limit' => ['integer'],
'keep_summary_row' => ['integer', '0'],
]
]
];
}

private function enrichFilterWithDefaultValues(array $filters): array
{
if ($this->report && $this->report->getDefaultSortColumn()) {
foreach ($filters as $index => $filter) {
if ($filter[0] === 'Sort') {
Expand All @@ -145,18 +173,18 @@ private function getGenericFiltersHavingDefaultValues()
* @param DataTable $datatable
* @return bool
*/
protected function applyGenericFilters($datatable)
protected function applyGenericFilters($datatable, $genericFilters)
{
if ($datatable instanceof DataTable\Map) {
$tables = $datatable->getDataTables();
foreach ($tables as $table) {
$this->applyGenericFilters($table);
$this->applyGenericFilters($table, $genericFilters);
}
return;
}

$tableDisabledFilters = $datatable->getMetadata(DataTable::GENERIC_FILTERS_TO_DISABLE_METADATA_NAME) ?: [];
$genericFilters = $this->getGenericFiltersHavingDefaultValues();
$genericFilters = $this->enrichFilterWithDefaultValues($genericFilters);

$filterApplied = false;
foreach ($genericFilters as $filterMeta) {
Expand Down
8 changes: 6 additions & 2 deletions core/API/DataTablePostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public function process(DataTableInterface $dataTable)
// this is non-trivial since it will require, eg, to make sure processed metrics aren't added
// after pivotBy is handled.
$dataTable = $this->applyPivotByFilter($dataTable);
$dataTable = $this->applyTotalsCalculator($dataTable);
$dataTable = $this->applyFlattener($dataTable);

if ($this->callbackBeforeGenericFilters) {
Expand Down Expand Up @@ -253,7 +252,12 @@ public function applyGenericFilters($dataTable)
$genericFilter->disableFilters(array('Limit', 'Truncate'));
}

$genericFilter->filter($dataTable);
$genericFilter->filter($dataTable, function ($dataTable) {
return $this->applyTotalsCalculator($dataTable);
});
} else {
// ensure totals calculator is applied even if generic filters are disabled
$this->applyTotalsCalculator($dataTable);
}

return $dataTable;
Expand Down
Loading