monitoring: Fix that sorting a data view is not possible if its sort

rules are empty
This commit is contained in:
Alexander Fuhr 2014-10-07 16:05:20 +02:00
parent 421263af00
commit a9ae75b6b0

View File

@ -204,42 +204,43 @@ public function dump()
public function sort($column = null, $order = null) public function sort($column = null, $order = null)
{ {
$sortRules = $this->getSortRules(); $sortRules = $this->getSortRules();
if ($column === null) {
if ($sortRules !== null) { // Use first available sort rule as default
if ($column === null) { if (empty($sortRules)) {
$sortColumns = reset($sortRules); return $this;
}
$sortColumns = reset($sortRules);
if (! isset($sortColumns['columns'])) {
$sortColumns['columns'] = array(key($sortRules));
}
} else {
if (isset($sortRules[$column])) {
$sortColumns = $sortRules[$column];
if (! isset($sortColumns['columns'])) { if (! isset($sortColumns['columns'])) {
$sortColumns['columns'] = array(key($sortRules)); $sortColumns['columns'] = array($column);
} }
} else { } else {
if (isset($sortRules[$column])) { $sortColumns = array(
$sortColumns = $sortRules[$column]; 'columns' => array($column),
if (! isset($sortColumns['columns'])) { 'order' => $order
$sortColumns['columns'] = array($column); );
} };
} else {
$sortColumns = array(
'columns' => array($column),
'order' => $order
);
};
}
$order = $order === null ? (isset($sortColumns['order']) ? $sortColumns['order'] : self::SORT_ASC) : $order;
$order = (strtoupper($order) === self::SORT_ASC) ? 'ASC' : 'DESC';
foreach ($sortColumns['columns'] as $column) {
if (! $this->isValidFilterTarget($column)) {
throw new QueryException(
t('The sort column "%s" is not allowed in "%s".'),
$column,
get_class($this)
);
}
$this->query->order($column, $order);
}
$this->isSorted = true;
} }
$order = $order === null ? (isset($sortColumns['order']) ? $sortColumns['order'] : self::SORT_ASC) : $order;
$order = (strtoupper($order) === self::SORT_ASC) ? 'ASC' : 'DESC';
foreach ($sortColumns['columns'] as $column) {
if (! $this->isValidFilterTarget($column)) {
throw new QueryException(
t('The sort column "%s" is not allowed in "%s".'),
$column,
get_class($this)
);
}
$this->query->order($column, $order);
}
$this->isSorted = true;
return $this; return $this;
} }
@ -250,7 +251,7 @@ public function dump()
*/ */
public function getSortRules() public function getSortRules()
{ {
return null; return array();
} }
/** /**
@ -365,6 +366,9 @@ public function dump()
*/ */
public function paginate($itemsPerPage = null, $pageNumber = null) public function paginate($itemsPerPage = null, $pageNumber = null)
{ {
if (! $this->isSorted) {
$this->order();
}
return $this->query->paginate($itemsPerPage, $pageNumber); return $this->query->paginate($itemsPerPage, $pageNumber);
} }