added location based queries for restaurants

This commit is contained in:
arzynik 2012-12-15 18:49:49 -05:00
parent 862aea27d4
commit 6aa9304d4f
2 changed files with 26 additions and 1 deletions

View File

@ -2,6 +2,16 @@
class Controller_api_config extends Crunchbutton_Controller_Rest {
public function init() {
echo json_encode(c::appConfig());
$config = c::appConfig();
if ($_REQUEST['lat'] && $_REQUEST['lon']) {
$restaurants = Restaurant::byRange([
'lat' => $_REQUEST['lat'],
'lon' => $_REQUEST['lon']
]);
foreach ($restaurants as $restaurant) {
$config['restaurants'][] = $restaurant->exports();
}
}
echo json_encode($config);
}
}

View File

@ -517,6 +517,21 @@ class Crunchbutton_Restaurant extends Cana_Table {
return $this->_ratingCount;
}
public function byRange($params) {
$query = '
SET @lat = '.$params['lat'].', @lon = '.$params['lon'].';
SELECT ((ACOS(SIN(@lat * PI() / 180) * SIN(loc_lat * PI() / 180) + COS(@lat * PI() / 180) * COS(loc_lat * PI() / 180) * COS((@lon - loc_long) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance`, name, delivery_radius, delivery, takeout
FROM `restaurant`
WHERE
active=1
AND delivery=1
HAVING `distance`<=`delivery_radius`
ORDER BY name ASC;
';
return self::q($query);
}
public function save() {
if (!$this->timezone) {
$this->timezone = 'America/New_York';