Skip to content

Commit

Permalink
#710 Add _sort method and proper handling of joined sort columns
Browse files Browse the repository at this point in the history
  • Loading branch information
amazeika committed Jul 29, 2024
1 parent 5263201 commit 00fcab8
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions code/libraries/joomlatools/library/model/behavior/sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,38 @@ protected function _beforeFetch(KModelContextInterface $context)
{
$state = $context->state;

$sort = $state->sort;
$column = $state->sort;
$direction = strtoupper($state->direction ?? '');
$columns = array_keys($this->getTable()->getColumns());

if ($sort)
{
$column = $this->getTable()->mapColumns($sort);
$this->_sort($column, $direction, $context->query);
}
}

//if(in_array($column, $columns)) {
$context->query->order($column, $direction);
//}
}
protected function _sort($column, $direction, KDatabaseQuerySelect $query)
{
$columns = array_keys($this->getTable()->getColumns());

$parts = explode('.', $column ?? '');

if ($sort != 'ordering' && in_array('ordering', $columns)) {
$context->query->order('tbl.ordering', 'ASC');
if (isset($parts[1])) {
$alias = $parts[0];
} else {
$alias = false;
}

if ($column)
{
if (!$alias) {
$column = $this->getTable()->mapColumns($column);
}

//if(in_array($column, $columns)) {
$query->order($column, $direction);
//}
}

if (!$alias && $column != 'ordering' && in_array('ordering', $columns)) {
$query->order('tbl.ordering', 'ASC');
}
}
}

0 comments on commit 00fcab8

Please sign in to comment.