Skip to content

Commit

Permalink
Attempting to get ORDER BY working in join element 'filter where'
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesegrits committed Aug 26, 2016
1 parent 2b50e80 commit ce91ba5
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions plugins/fabrik_element/databasejoin/databasejoin.php
Expand Up @@ -1075,6 +1075,12 @@ protected function buildQueryWhere($data = array(), $incWhere = true, $thisTable
$filterWhere = trim($params->get('database_join_filter_where_sql', ''));
if (FArrayHelper::getValue($opts, 'mode', '') === 'filter' && !empty($filterWhere))
{
if (preg_match('/(ORDER\s+BY)(.*)/i', $filterWhere, $matches))
{
$this->orderBy = $this->parseThisTable($matches[0], $join);
$filterWhere = str_replace($this->orderBy, '', $filterWhere);
$filterWhere = str_replace($matches[0], '', $filterWhere);
}
$where .= JString::stristr($where, 'WHERE') ? ' AND ' . $filterWhere : ' WHERE ' . $filterWhere;
}

Expand Down Expand Up @@ -2378,6 +2384,7 @@ protected function getOrderBy($view = '', $query = false)
break;
case '-1':
default:
$orders = array();
// Check if the 'Joins where and/or order by statement' has an order by
$joinWhere = $params->get('database_join_where_sql');

Expand All @@ -2388,9 +2395,25 @@ protected function getOrderBy($view = '', $query = false)

if (count($joinWhere) > 1)
{
$order = $joinWhere[count($joinWhere) - 1];
$orders[] = $joinWhere[count($joinWhere) - 1];
}
}

$filterWhere = trim($params->get('database_join_filter_where_sql', ''));

if (JString::stristr($filterWhere, 'ORDER BY'))
{
$filterWhere = str_replace('order by', 'ORDER BY', $filterWhere);
$filterWhere = explode('ORDER BY', $filterWhere);

if (count($filterWhere) > 1)
{
$orders[] = $filterWhere[count($filterWhere) - 1];
}
}

$order = implode(', ', $orders);

break;
}

Expand Down Expand Up @@ -2691,11 +2714,13 @@ protected function checkboxRows($groupBy = null, $condition = null, $value = nul
$query->where($where . ' ' . $condition . ' ' . $value);
}

$this->getOrderBy('filter', $query);

$db->setQuery($query, $offset, $limit);
$sql = (string) $query;
$groupBy = FabrikString::shortColName($groupBy);
$rows = $db->loadObjectList($groupBy);
ksort($rows);
//ksort($rows);

return $rows;
}
Expand Down

0 comments on commit ce91ba5

Please sign in to comment.