From 34155a9856fcdebf431bd6571e2486d5bacb0fb1 Mon Sep 17 00:00:00 2001 From: Tam Date: Sun, 17 Feb 2019 09:02:31 +0000 Subject: [PATCH] Update MapService.php --- src/services/MapService.php | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/services/MapService.php b/src/services/MapService.php index 63c2e2f..c7d456f 100644 --- a/src/services/MapService.php +++ b/src/services/MapService.php @@ -288,7 +288,7 @@ public function modifyElementsQuery (ElementQueryInterface $query, $value) if (array_key_exists('location', $value)) { $this->_searchLocation($query, $value, $tableAlias); - } else if (array_key_exists('distance', $query->orderBy)) { + } else if ($this->_shouldReplaceOrderBy($query)) { $this->_replaceOrderBy($query); } @@ -540,7 +540,7 @@ private function _searchLocation (ElementQuery $query, $value, $tableAlias) } if ($location == null) { - if (array_key_exists('distance', $query->orderBy)) { + if ($this->_shouldReplaceOrderBy($query)) { $this->_replaceOrderBy($query, false); } return; @@ -581,7 +581,7 @@ private function _searchLocation (ElementQuery $query, $value, $tableAlias) ] ]; - if (array_key_exists('distance', $query->orderBy)) { + if ($this->_shouldReplaceOrderBy($query)) { $this->_replaceOrderBy($query, $distanceSearch); } @@ -603,10 +603,35 @@ private function _replaceOrderBy (ElementQuery $query, $distanceSearch = false) foreach ((array)$query->orderBy as $order => $sort) { if ($order == 'distance' && $distanceSearch) $nextOrder[$distanceSearch] = $sort; + elseif (preg_match('/COS\(RADIANS\(.*\)\)/',$order) && preg_match('/SIN\(RADIANS\(.*\)\)/',$order) && $distanceSearch) $nextOrder[$distanceSearch] = $sort; elseif ($order != 'distance') $nextOrder[$order] = $sort; } $query->orderBy($nextOrder); } + /** + * Check if the order by clause should be replaced. + * @param ElementQueryInterface $query + * @return bool + */ + private function _shouldReplaceOrderBy(ElementQueryInterface $query): bool + { + if ($query->orderBy == 'distance' ){ + return true; + } + if (is_array($query->orderBy)) { + $keys = array_keys($query->orderBy); + foreach ($keys as $key) { + if ('distance' == $key) { + return true; + } + if (preg_match('/COS\(RADIANS\(.*\)\)/',$key) && preg_match('/SIN\(RADIANS\(.*\)\)/',$key)) { + return true; + } + } + } + return false; + } + }