From 54ac2bf9d949d45eb756768003a0d4e0008e0a61 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Mon, 16 Mar 2015 03:12:02 +0000 Subject: possibility to define a default sort order in a report class --- core/API/DataTableGenericFilter.php | 31 ++++++++++++++++++++++++++++--- core/API/DataTablePostProcessor.php | 2 +- core/Plugin/Report.php | 15 +++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) (limited to 'core') diff --git a/core/API/DataTableGenericFilter.php b/core/API/DataTableGenericFilter.php index 46274b0c31..ff49491642 100644 --- a/core/API/DataTableGenericFilter.php +++ b/core/API/DataTableGenericFilter.php @@ -10,7 +10,6 @@ namespace Piwik\API; use Exception; use Piwik\Common; -use Piwik\DataTable\Filter\AddColumnsProcessedMetricsGoal; use Piwik\DataTable; use Piwik\Plugin\ProcessedMetric; use Piwik\Plugin\Report; @@ -24,14 +23,25 @@ class DataTableGenericFilter */ private $disabledFilters = array(); + /** + * @var Report + */ + private $report; + + /** + * @var array + */ + private $request; + /** * Constructor * * @param $request */ - function __construct($request) + function __construct($request, $report) { $this->request = $request; + $this->report = $report; } /** @@ -102,6 +112,21 @@ class DataTableGenericFilter ); } + private function getGenericFiltersHavingDefaultValues() + { + $filters = self::getGenericFiltersInformation(); + + if ($this->report && $this->report->getDefaultSortColumn()) { + foreach ($filters as $index => $filter) { + if ($filter[0] === 'Sort') { + $filters[$index][1]['filter_sort_column'] = array('string', $this->report->getDefaultSortColumn()); + } + } + } + + return $filters; + } + /** * Apply generic filters to the DataTable object resulting from the API Call. * Disable this feature by setting the parameter disable_generic_filters to 1 in the API call request. @@ -119,7 +144,7 @@ class DataTableGenericFilter return; } - $genericFilters = self::getGenericFiltersInformation(); + $genericFilters = $this->getGenericFiltersHavingDefaultValues(); $filterApplied = false; foreach ($genericFilters as $filterMeta) { diff --git a/core/API/DataTablePostProcessor.php b/core/API/DataTablePostProcessor.php index dfa0434da6..8d936e6779 100644 --- a/core/API/DataTablePostProcessor.php +++ b/core/API/DataTablePostProcessor.php @@ -207,7 +207,7 @@ class DataTablePostProcessor if (0 == Common::getRequestVar('disable_generic_filters', '0', 'string', $this->request)) { $this->applyProcessedMetricsGenericFilters($dataTable); - $genericFilter = new DataTableGenericFilter($this->request); + $genericFilter = new DataTableGenericFilter($this->request, $this->report); $self = $this; $report = $this->report; diff --git a/core/Plugin/Report.php b/core/Plugin/Report.php index b9c407e10e..dfbe6e50cf 100644 --- a/core/Plugin/Report.php +++ b/core/Plugin/Report.php @@ -188,6 +188,13 @@ class Report */ protected $recursiveLabelSeparator = ' - '; + /** + * Default sort column. Either a column name or a column id. + * + * @var string|int + */ + protected $defaultSortColumn = ''; + /** * @var array * @ignore @@ -578,6 +585,14 @@ class Report return $report; } + /** + * @ignore + */ + public function getDefaultSortColumn() + { + return $this->defaultSortColumn; + } + /** * Get the list of related reports if there are any. They will be displayed for instance below a report as a * recommended related report. -- cgit v1.2.3 From 78d546f15ec66319dafbc547aabaa813c5b67a65 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Mon, 16 Mar 2015 03:17:17 +0000 Subject: fixed a couple of bugs in DataTable\Sort. * make sure to select correct column (the column value might be false which is valid, meaning column actually exists whereas we assumed before it does not exist) * use correct sort algorithm (if value of first column was false we picked under circumstances a string comparison instead of number) * If we sort by label, use always a string or natural comparison even if the label is numeric --- core/DataTable/Filter/Sort.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'core') diff --git a/core/DataTable/Filter/Sort.php b/core/DataTable/Filter/Sort.php index 3da11b4119..7e91bb338d 100644 --- a/core/DataTable/Filter/Sort.php +++ b/core/DataTable/Filter/Sort.php @@ -163,8 +163,8 @@ Sort extends BaseFilter */ protected function selectColumnToSort($row) { - $value = $row->getColumn($this->columnToSort); - if ($value !== false) { + $value = $row->hasColumn($this->columnToSort); + if ($value) { return $this->columnToSort; } @@ -172,9 +172,9 @@ Sort extends BaseFilter // sorting by "nb_visits" but the index is Metrics::INDEX_NB_VISITS in the table if (isset($columnIdToName[$this->columnToSort])) { $column = $columnIdToName[$this->columnToSort]; - $value = $row->getColumn($column); + $value = $row->hasColumn($column); - if ($value !== false) { + if ($value) { return $column; } } @@ -182,8 +182,8 @@ Sort extends BaseFilter // eg. was previously sorted by revenue_per_visit, but this table // doesn't have this column; defaults with nb_visits $column = Metrics::INDEX_NB_VISITS; - $value = $row->getColumn($column); - if ($value !== false) { + $value = $row->hasColumn($column); + if ($value) { return $column; } @@ -220,8 +220,9 @@ Sort extends BaseFilter $this->columnToSort = $this->selectColumnToSort($row); - $value = $row->getColumn($this->columnToSort); - if (is_numeric($value)) { + $value = $this->getFirstValueFromDataTable($table); + + if (is_numeric($value) && $this->columnToSort !== 'label') { $methodToUse = "numberSort"; } else { if ($this->naturalSort) { @@ -234,6 +235,16 @@ Sort extends BaseFilter $this->sort($table, $methodToUse); } + private function getFirstValueFromDataTable($table) + { + foreach ($table->getRows() as $row) { + $value = $this->getColumnValue($row); + if (!is_null($value)) { + return $value; + } + } + } + /** * Sorts the DataTable rows using the supplied callback function. * -- cgit v1.2.3