Skip to content

Instantly share code, notes, and snippets.

@cedricziel
Last active December 22, 2015 23:49
Show Gist options
  • Save cedricziel/6549125 to your computer and use it in GitHub Desktop.
Save cedricziel/6549125 to your computer and use it in GitHub Desktop.
# 6371 is the earth radius in km
# 51.5 is the latitude of the center
# 12 is the longitude of the center
# 5000 is the search radius in km
# s.uid is the key to filter the tx_czielworldcombase_domain_model_organisation for
SELECT o.*, s.*, ( 6371 * acos( cos( radians('51.5') ) * cos( radians( o.lat ) ) * cos( radians( o.lon ) - radians('12') ) + sin( radians('51,5') ) * sin( radians( o.lat ) ) ) ) AS distance
FROM tx_czielworldcombase_domain_model_organisation o, tx_czielworldcombase_organisation_service_mm mm, tx_czielworldcombase_domain_model_service s
WHERE o.uid = mm.uid_local AND mm.uid_foreign = s.uid
HAVING s.uid = 3 AND distance < '5000' ORDER BY distance LIMIT 1
class OrganisationRepository extends AbstractWorldcomRepository {
/**
* Find closest matching Agency based on visitor's IP
*
* @param \Worldcom\CzielWorldcomBase\Domain\Model\Service $service
* @param int $limit The amount of data to be retreived
* @return \Worldcom\CzielWorldcomBase\Domain\Model\Organisation
*/
public function findClosestMatchingAgency(\Worldcom\CzielWorldcomBase\Domain\Model\Service $service, $limit = 1) {
if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {
$ip = "86.56.84.67";
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$geoJson = \Worldcom\CzielWorldcomBase\Service\GeocodingService::reverseCodeByIp($ip);
$kmEarthRadius = 6371;
$milesEarthRadius = 3959;
$queryStringRaw = "SELECT o.*, " .
" ( %s * acos( cos( radians('%s') ) * cos( radians( o.lat ) ) * cos( radians( o.lon ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( o.lat ) ) ) ) AS distance" .
" FROM tx_czielworldcombase_domain_model_organisation o, tx_czielworldcombase_organisation_service_mm mm, tx_czielworldcombase_domain_model_service s " .
" WHERE o.uid = mm.uid_local AND mm.uid_foreign = s.uid AND s.uid = %s " .
" HAVING distance < '%s' ORDER BY distance LIMIT %s";
$queryString = sprintf(
$queryStringRaw,
$kmEarthRadius,
$geoJson->latitude,
$geoJson->longitude,
$geoJson->latitude,
$service->getUid(),
5000,
$limit
);
$query = $this->createQuery();
/** @var \TYPO3\CMS\Extbase\Persistence\Generic\Query $query */
$query->statement($queryString);
return $query->execute()->getFirst();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment