Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 112 additions & 85 deletions src/services/MapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,14 @@ public function modifyElementsQuery (ElementQueryInterface $query, $value, MapFi
}

$oldOrderBy = null;
$search = false;

if (!is_array($query->orderBy))
{
$oldOrderBy = $query->orderBy;
$query->orderBy = [];
}

// Coordinate CraftQL support
if (array_key_exists('coordinate', $value))
$value['location'] = $value['coordinate'];

if (array_key_exists('location', $value))
$search = $this->_searchLocation($query, $value, $alias);
$search = $this->_searchLocation($query, $value, $alias);

if (array_key_exists('distance', $query->orderBy))
$this->_replaceOrderBy($query, $search);
Expand Down Expand Up @@ -298,107 +292,140 @@ public function populateMissingData (Map $map, MapField $field)
* search string if we can.
*
* @param ElementQuery $query
* @param mixed $value
* @param string $table
* @param mixed $values
* @param string $table
*
* @return bool|string
* @return array|bool
* @throws \Exception
*/
private function _searchLocation (ElementQuery $query, $value, $table)
private function _searchLocation (ElementQuery $query, $values, $table)
{
$location = $value['location'];
$country = $value['country'] ?? null;
$radius = $value['radius'] ?? 50.0;
$unit = $value['unit'] ?? 'km';

// Normalize location
$location = GeoService::normalizeLocation($location, $country);

if ($location === null)
return false;

$lat = $location['lat'];
$lng = $location['lng'];

// Normalize radius
if (!is_numeric($radius))
$radius = (float) $radius;

if (!is_numeric($radius))
$radius = 50.0;

// Normalize unit
$unit = GeoService::normalizeDistance($unit);

// Base Distance
$distance = $unit === 'km' ? '111.045' : '69.0';

// Store for populating search result distance
$this->_location = $location;
$this->_distance = (float) $distance;

// Search Query
$search = str_replace(["\r", "\n", "\t"], '', "(
$distance *
DEGREES(
ACOS(
COS(RADIANS($lat)) *
COS(RADIANS([[$table.lat]])) *
COS(RADIANS($lng) - RADIANS([[$table.lng]])) +
SIN(RADIANS($lat)) *
SIN(RADIANS([[$table.lat]]))
if (array_key_exists('location', $values)) {
$values = [$values];
}

$queryOrderBy = [];
$queryRestrict = ['or'];
$queryWhere = ['or'];
$queryHaving = ['or'];

foreach ($values as $index => $value) {
// Coordinate CraftQL support
if (array_key_exists('coordinate', $value))
$value['location'] = $value['coordinate'];

$location = $value['location'];
$country = $value['country'] ?? null;
$radius = $value['radius'] ?? 50.0;
$unit = $value['unit'] ?? 'km';

// Normalize location
$location = GeoService::normalizeLocation($location, $country);

if ($location === null)
return false;

$lat = $location['lat'];
$lng = $location['lng'];

// Normalize radius
if (!is_numeric($radius))
$radius = (float) $radius;

if (!is_numeric($radius))
$radius = 50.0;

// Normalize unit
$unit = GeoService::normalizeDistance($unit);

// Base Distance
$distance = $unit === 'km' ? '111.045' : '69.0';

// Store for populating search result distance
$this->_location = $location;
$this->_distance = (float) $distance;

// Search Query
$search = str_replace(["\r", "\n", "\t"], '', "(
$distance *
DEGREES(
ACOS(
COS(RADIANS($lat)) *
COS(RADIANS([[$table.lat]])) *
COS(RADIANS($lng) - RADIANS([[$table.lng]])) +
SIN(RADIANS($lat)) *
SIN(RADIANS([[$table.lat]]))
)
)
)
)");
)");

// Restrict the results
$restrict = [
'and',
[
'and',
"[[$table.lat]] >= $lat - ($radius / $distance)",
"[[$table.lat]] <= $lat + ($radius / $distance)",
],
[
// Restrict the results
$queryRestrict[] = [
'and',
"[[$table.lng]] >= $lng - ($radius / ($distance * COS(RADIANS($lat))))",
"[[$table.lng]] <= $lng + ($radius / ($distance * COS(RADIANS($lat))))",
]
];

// Filter the query
$query
->subQuery
->addSelect($search . ' as [[mapsCalculatedDistance]]')
->andWhere($restrict)
->andWhere([
'not',
['[[' . $table . '.lat]]' => null],
]);
[
'and',
"[[$table.lat]] >= $lat - ($radius / $distance)",
"[[$table.lat]] <= $lat + ($radius / $distance)",
],
[
'and',
"[[$table.lng]] >= $lng - ($radius / ($distance * COS(RADIANS($lat))))",
"[[$table.lng]] <= $lng + ($radius / ($distance * COS(RADIANS($lat))))",
]
];

$key = "mapsCalculatedDistance_$index";

// Filter the query
$query
->subQuery
->addSelect("$search as [[$key]]");

$queryWhere[] = "$search <= $radius";
$queryHaving[] = "[[$key]] <= $radius";
$queryOrderBy[] = "[[$key]]";
}

if (Craft::$app->getDb()->driverName === 'pgsql')
$query->subQuery->andWhere($search . ' <= ' . $radius);
else
$query->subQuery->andHaving('[[mapsCalculatedDistance]] <= ' . $radius);
if ($queryOrderBy) {
$query
->subQuery
->andWhere($queryRestrict)
->andWhere([
'not',
['[[' . $table . '.lat]]' => null],
]);

if (Craft::$app->getDb()->driverName === 'pgsql')
$query->subQuery->andWhere($queryWhere);
else
$query->subQuery->andHaving($queryHaving);

return $queryOrderBy;
}

return '[[mapsCalculatedDistance]]';
return false;
}

/**
* Will replace the distance search with the correct query if available,
* or otherwise remove it.
*
* @param ElementQuery $query
* @param bool $search
* @param array|bool $search
*/
private function _replaceOrderBy (ElementQuery $query, $search = false)
{
$nextOrder = [];

foreach ((array) $query->orderBy as $order => $sort)
{
if ($order === 'distance' && $search) $nextOrder[$search] = $sort;
elseif ($order !== 'distance') $nextOrder[$order] = $sort;
if ($order === 'distance' && $search) {
foreach ($search as $searchParam) {
$nextOrder[$searchParam] = $sort;
}
} elseif ($order !== 'distance') {
$nextOrder[$order] = $sort;
}
}

$query->orderBy($nextOrder);
Expand Down