Skip to content

Commit

Permalink
Update MapService.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam authored Feb 17, 2019
1 parent be90293 commit 34155a9
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/services/MapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}

}

0 comments on commit 34155a9

Please sign in to comment.