From 00fcab8da99eacf5ba3c563a80c22fc8f880f9eb Mon Sep 17 00:00:00 2001 From: Arunas Mazeika Date: Mon, 29 Jul 2024 16:21:53 +0200 Subject: [PATCH] #710 Add _sort method and proper handling of joined sort columns --- .../library/model/behavior/sortable.php | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/code/libraries/joomlatools/library/model/behavior/sortable.php b/code/libraries/joomlatools/library/model/behavior/sortable.php index eab751388..4321364e5 100644 --- a/code/libraries/joomlatools/library/model/behavior/sortable.php +++ b/code/libraries/joomlatools/library/model/behavior/sortable.php @@ -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'); } } } \ No newline at end of file