Skip to content

Commit

Permalink
Allow to provide secondy sort column for reports
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Sep 27, 2017
1 parent 9611b36 commit 30ee6ea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
9 changes: 8 additions & 1 deletion core/API/DataTableGenericFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static function getGenericFiltersInformation()
'filter_sort_order' => array('string', 'desc'),
$naturalSort = true,
$recursiveSort = true,
$doSortBySecondaryColumn = true
'filter_sort_column_secondary' => true
)),
array('Truncate',
array(
Expand All @@ -124,6 +124,13 @@ private function getGenericFiltersHavingDefaultValues()
if ($filter[0] === 'Sort') {
$filters[$index][1]['filter_sort_column'] = array('string', $this->report->getDefaultSortColumn());
$filters[$index][1]['filter_sort_order'] = array('string', $this->report->getDefaultSortOrder());

$callback = $this->report->getSecondSortColumnCallback();

if (is_callable($callback)) {
$filters[$index][1]['filter_sort_column_secondary'] = $callback;
}

}
}
}
Expand Down
15 changes: 11 additions & 4 deletions core/DataTable/Filter/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Sort extends BaseFilter
protected $order;
protected $naturalSort;
protected $isSecondaryColumnSortEnabled;
protected $secondaryColumnSortCallback;

const ORDER_DESC = 'desc';
const ORDER_ASC = 'asc';
Expand All @@ -40,8 +41,9 @@ class Sort extends BaseFilter
* @param string $order order `'asc'` or `'desc'`.
* @param bool $naturalSort Whether to use a natural sort or not (see {@link http://php.net/natsort}).
* @param bool $recursiveSort Whether to sort all subtables or not.
* @param bool $doSortBySecondaryColumn If true will sort by a secondary column. The column is automatically
* detected and will be either nb_visits or label, if possible.
* @param bool|callback $doSortBySecondaryColumn If true will sort by a secondary column. The column is automatically
* detected and will be either nb_visits or label, if possible.
* If callback given it will sort by the column returned by the callback (if any)
*/
public function __construct($table, $columnToSort, $order = 'desc', $naturalSort = true, $recursiveSort = true, $doSortBySecondaryColumn = false)
{
Expand All @@ -54,7 +56,8 @@ public function __construct($table, $columnToSort, $order = 'desc', $naturalSort
$this->columnToSort = $columnToSort;
$this->naturalSort = $naturalSort;
$this->order = strtolower($order);
$this->isSecondaryColumnSortEnabled = $doSortBySecondaryColumn;
$this->isSecondaryColumnSortEnabled = !empty($doSortBySecondaryColumn);
$this->secondaryColumnSortCallback = is_callable($doSortBySecondaryColumn) ? $doSortBySecondaryColumn : null;
}

/**
Expand Down Expand Up @@ -90,7 +93,11 @@ public function filter($table)
$config->primaryColumnToSort = $sorter->getPrimaryColumnToSort($table, $this->columnToSort);
$config->primarySortOrder = $sorter->getPrimarySortOrder($this->order);
$config->primarySortFlags = $sorter->getBestSortFlags($table, $config->primaryColumnToSort);
$config->secondaryColumnToSort = $sorter->getSecondaryColumnToSort($row, $config->primaryColumnToSort);
if (!empty($this->secondaryColumnSortCallback)) {
$config->secondaryColumnToSort = ($this->secondaryColumnSortCallback)($config->primaryColumnToSort);
} else {
$config->secondaryColumnToSort = $sorter->getSecondaryColumnToSort($row, $config->primaryColumnToSort);
}
$config->secondarySortOrder = $sorter->getSecondarySortOrder($this->order, $config->secondaryColumnToSort);
$config->secondarySortFlags = $sorter->getBestSortFlags($table, $config->secondaryColumnToSort);

Expand Down
27 changes: 24 additions & 3 deletions core/Plugin/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@

use Piwik\API\Proxy;
use Piwik\API\Request;
use Piwik\Cache;
use Piwik\Columns\Dimension;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\DataTable\Filter\Sort;
use Piwik\Metrics;
use Piwik\Cache as PiwikCache;
use Piwik\Piwik;
use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph\Evolution;
use Piwik\ViewDataTable\Factory as ViewDataTableFactory;
use Exception;
use Piwik\Widget\WidgetsList;
Expand Down Expand Up @@ -647,6 +644,30 @@ public function getDefaultSortOrder()
return Sort::ORDER_ASC;
}

/**
* Allows to define a callback that will be used to determine the secondary column to sort by
*
* ```
* public function getSecondSortColumnCallback()
* {
* return function ($primaryColumn) {
* switch ($primaryColumn) {
* case Metrics::NB_CLICKS:
* return Metrics::NB_IMPRESSIONS;
* case 'label':
* default:
* return Metrics::NB_CLICKS;
* }
* };
* }
* ```
* @return null|callable
*/
public function getSecondSortColumnCallback()
{
return null;
}

/**
* Get the list of related reports if there are any. They will be displayed for instance below a report as a
* recommended related report.
Expand Down

0 comments on commit 30ee6ea

Please sign in to comment.