1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Metrics;
use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Metrics;
use Piwik\Plugin\Metric;
class Sorter
{
/**
* @var Sorter\Config
*/
private $config;
public function __construct(Sorter\Config $config)
{
$this->config = $config;
}
/**
* Sorts the DataTable rows using the supplied callback function.
*
* @param DataTable $table The table to sort.
*/
public function sort(DataTable $table)
{
// all that code is in here and not in separate methods for best performance. It does make a difference once
// php has to copy many (eg 50k) rows otherwise.
$table->setTableSortedBy($this->config->primaryColumnToSort);
$rows = $table->getRowsWithoutSummaryRow();
// we need to sort rows that have a value separately from rows that do not have a value since we always want
// to append rows that do not have a value at the end.
$rowsWithValues = array();
$rowsWithoutValues = array();
$valuesToSort = array();
foreach ($rows as $key => $row) {
$value = $this->getColumnValue($row);
if (isset($value)) {
$valuesToSort[] = $value;
$rowsWithValues[] = $row;
} else {
$rowsWithoutValues[] = $row;
}
}
unset($rows);
if ($this->config->isSecondaryColumnSortEnabled && $this->config->secondaryColumnToSort) {
$secondaryValues = array();
foreach ($rowsWithValues as $key => $row) {
$secondaryValues[$key] = $row->getColumn($this->config->secondaryColumnToSort);
}
array_multisort($valuesToSort, $this->config->primarySortOrder, $this->config->primarySortFlags, $secondaryValues, $this->config->secondarySortOrder, $this->config->secondarySortFlags, $rowsWithValues);
} else {
array_multisort($valuesToSort, $this->config->primarySortOrder, $this->config->primarySortFlags, $rowsWithValues);
}
if (!empty($rowsWithoutValues) && $this->config->secondaryColumnToSort) {
$secondaryValues = array();
foreach ($rowsWithoutValues as $key => $row) {
$secondaryValues[$key] = $row->getColumn($this->config->secondaryColumnToSort);
}
array_multisort($secondaryValues, $this->config->secondarySortOrder, $this->config->secondarySortFlags, $rowsWithoutValues);
}
unset($secondaryValues);
foreach ($rowsWithoutValues as $row) {
$rowsWithValues[] = $row;
}
$table->setRows(array_values($rowsWithValues));
}
private function getColumnValue(Row $row)
{
$value = $row->getColumn($this->config->primaryColumnToSort);
if ($value === false || is_array($value)) {
return null;
}
return $value;
}
/**
* @param string $order 'asc' or 'desc'
* @return int
*/
public function getPrimarySortOrder($order)
{
if ($order === 'asc') {
return SORT_ASC;
}
return SORT_DESC;
}
/**
* @param string $order 'asc' or 'desc'
* @param string|int $secondarySortColumn column name or column id
* @return int
*/
public function getSecondarySortOrder($order, $secondarySortColumn)
{
if ($secondarySortColumn === 'label') {
$secondaryOrder = SORT_ASC;
if ($order === 'asc') {
$secondaryOrder = SORT_DESC;
}
return $secondaryOrder;
}
return $this->getPrimarySortOrder($order);
}
/**
* Detect the column to be used for sorting
*
* @param string|int $columnToSort column name or column id
* @return int
*/
public function getPrimaryColumnToSort(DataTable $table, $columnToSort)
{
// we fallback to nb_visits in case columnToSort does not exist
$columnsToCheck = array($columnToSort, 'nb_visits');
$row = $table->getFirstRow();
foreach ($columnsToCheck as $column) {
$column = Metric::getActualMetricColumn($table, $column);
if ($row->hasColumn($column)) {
// since getActualMetricColumn() returns a default value, we need to make sure it actually has that column
return $column;
}
}
return $columnToSort;
}
/**
* Detect the secondary sort column to be used for sorting
*
* @param int|string $primaryColumnToSort
* @return ?string
*/
public function getSecondaryColumnToSort(Row $row, $primaryColumnToSort)
{
$defaultSecondaryColumn = array(Metrics::INDEX_NB_VISITS, 'nb_visits');
if (in_array($primaryColumnToSort, $defaultSecondaryColumn)) {
// if sorted by visits, then sort by label as a secondary column
$column = 'label';
$value = $row->hasColumn($column);
if ($value !== false) {
return $column;
}
return null;
}
if ($primaryColumnToSort !== 'label') {
// we do not add this by default to make sure we do not sort by label as a first and secondary column
$defaultSecondaryColumn[] = 'label';
}
foreach ($defaultSecondaryColumn as $column) {
$value = $row->hasColumn($column);
if ($value !== false) {
return $column;
}
}
return null;
}
/**
* @param string|int|null $columnToSort A column name or column id. Make sure that column actually exists in the row.
* You might want to get a valid column via {@link getPrimaryColumnToSort()} or
* {@link getSecondaryColumnToSort()}
* @return int
*/
public function getBestSortFlags(DataTable $table, $columnToSort)
{
// when column is label we always to sort by string or natural
if (isset($columnToSort) && $columnToSort !== 'label') {
foreach ($table->getRowsWithoutSummaryRow() as $row) {
$value = $row->getColumn($columnToSort);
if ($value !== false && $value !== null && !is_array($value)) {
if (is_numeric($value)) {
$sortFlags = SORT_NUMERIC;
} else {
$sortFlags = $this->getStringSortFlags();
}
return $sortFlags;
}
}
}
return $this->getStringSortFlags();
}
private function getStringSortFlags()
{
if ($this->config->naturalSort) {
$sortFlags = SORT_NATURAL | SORT_FLAG_CASE;
} else {
$sortFlags = SORT_STRING | SORT_FLAG_CASE;
}
return $sortFlags;
}
}
|