/** @var array Contains all "WHERE" clauses for the query */
$aWhereClauses = array();
$aOrderBy = array();
// Start with the minimal query
$sql = 'SELECT P.Id AS id, P.anchor_geolocation_lat AS lat, P.anchor_geolocation_lon AS lon, P.text_description as line2, P.text_footnote as line3, P.text_title as title';
// If the filter requires so, add distance calculation
if ($filter->lat !== NULL && $filter->lon !== NULL) {
$sql .= " , " . GeoUtil::EARTH_RADIUS . " * 2 * asin(sqrt(pow(sin((radians(" . addslashes($filter->lat) . ") - radians(anchor_geolocation_lat)) / 2), 2)$
$aOrderBy[]='distance';
}
$sql.=' FROM POI P , Layer L';
$aWhereClauses[]='P.LayerID = L.id';
if ($filter->layerName) {
$aWhereClauses[]="L.Layer='".$filter->layerName."'";
}
/*if (!empty($filter->requestedPoiId)) {
$aWhereClauses[]="id='" . addslashes($filter->requestedPoiId) . "'";
}*/
if (count($aWhereClauses)>0) {
$sql .= " WHERE ".implode(' AND ', $aWhereClauses);
}
if (!empty($filter->radius) && ($filter->lat !== NULL && $filter->lon !== NULL)) {
$sql .= ' HAVING ( distance<('.addslashes($filter->radius).'+'.addslashes($filter->accuracy).') OR anchor_referenceImage!="" )';
}
if (count($aOrderBy)>0) {
$sql .= ' ORDER BY '.implode(',', $aOrderBy).' ASC';
}
return $sql;