updated query to include weight. added weight functions. added sorting by weight and open time to the frontend.

This commit is contained in:
arzynik 2013-02-22 15:17:58 -05:00
parent cd14a43e32
commit 0e7244df54
3 changed files with 138 additions and 41 deletions

View File

@ -633,6 +633,18 @@ class Crunchbutton_Restaurant extends Cana_Table
return $thumb; return $thumb;
} }
public function weight() {
if (!isset($this->_weight)) {
$res = self::q('
select count(*) as `weight`, `restaurant`.name from `order`
left join `restaurant` using(id_restaurant)
where id_restaurant='.$this->id_restaurant.'
');
$this->_weight = $res->weight;
}
return $this->_weight;
}
/** /**
* Returns an array with all the information for a Restaurant. * Returns an array with all the information for a Restaurant.
@ -646,6 +658,7 @@ class Crunchbutton_Restaurant extends Cana_Table
public function exports($ignore = []) { public function exports($ignore = []) {
$out = $this->properties(); $out = $this->properties();
$out['_open'] = $this->open(); $out['_open'] = $this->open();
$out['_weight'] = $this->weight();
$timezone = new DateTimeZone( $this->timezone ); $timezone = new DateTimeZone( $this->timezone );
$date = new DateTime( 'now ', $timezone ) ; $date = new DateTime( 'now ', $timezone ) ;
@ -723,21 +736,26 @@ class Crunchbutton_Restaurant extends Cana_Table
public static function byRange($params) { public static function byRange($params) {
$params[ 'miles' ] = ( $params[ 'miles' ] ) ? $params[ 'miles' ] : 2; $params[ 'miles' ] = ( $params[ 'miles' ] ) ? $params[ 'miles' ] : 2;
$query = ' $query = '
SELECT ((ACOS(SIN('.$params['lat'].' * PI() / 180) * SIN(loc_lat * PI() / 180) + COS('.$params['lat'].' * PI() / 180) * COS(loc_lat * PI() / 180) * COS(('.$params['lon'].' - loc_long) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance`, restaurant.* SELECT
FROM `restaurant` count(*) as _weight,
WHERE ((ACOS(SIN('.$params['lat'].' * PI() / 180) * SIN(loc_lat * PI() / 180) + COS('.$params['lat'].' * PI() / 180) * COS(loc_lat * PI() / 180) * COS(('.$params['lon'].' - loc_long) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance`,
active = 1 restaurant.*
HAVING FROM `restaurant`
takeout = 1 LEFT JOIN `order` using(id_restaurant)
AND WHERE
delivery = 0 active = 1
AND GROUP BY restaurant.id_restaurant
`distance` <= ' . $params[ 'miles' ] . ' HAVING
OR takeout = 1
delivery = 1 AND
AND delivery = 0
`distance` <= `delivery_radius` AND
ORDER BY name ASC; `distance` <= ' . $params[ 'miles' ] . '
OR
delivery = 1
AND
`distance` <= `delivery_radius`
ORDER BY _weight DESC;
'; ';
return self::q($query); return self::q($query);
} }

View File

@ -3038,7 +3038,23 @@ App.foodDelivery.preProcess = function() {
$('input').blur(); $('input').blur();
return; return;
} else { } else {
App.restaurants.list = json.restaurants; App.restaurants.list = [];
for (var x in json.restaurants) {
var res = new Restaurant(json.restaurants[x]);
res.open();
App.restaurants.list[App.restaurants.list.length] = res;
};
App.restaurants.list.sort(sort_by({
name: '_open',
reverse: true
}, {
name: '_weight',
primer: parseInt,
reverse: true
}));
console.log(App.restaurants.list);
if( App.foodDelivery.forceProcess ){ if( App.foodDelivery.forceProcess ){
App.foodDelivery.forceProcess = false; App.foodDelivery.forceProcess = false;
App.page.foodDelivery.load(); App.page.foodDelivery.load();
@ -3125,36 +3141,34 @@ App.page.foodDelivery.load = function(){
App.hasLocation = true; App.hasLocation = true;
for (var x in rs) { for (var x in rs) {
var id = rs[x].id_restaurant; var r = rs[x];
App.cache('Restaurant', id,function() { var restaurant = $('<div class="meal-item'+ (!r.open() ? ' meal-item-closed' : '') +'" data-id_restaurant="' + r.id_restaurant + '" data-permalink="' + r.permalink + '"></div>');
var restaurantContent = $('<div class="meal-item-content">');
var restaurant = $('<div class="meal-item'+ (!this.open() ? ' meal-item-closed' : '') +'" data-id_restaurant="' + this.id_restaurant + '" data-permalink="' + this.permalink + '"></div>');
var restaurantContent = $('<div class="meal-item-content">');
restaurantContent restaurantContent
.append('<div class="meal-pic" style="background: url(' + this.img64 + ');"></div>') .append('<div class="meal-pic" style="background: url(' + r.img64 + ');"></div>')
.append('<h2 class="meal-restaurant">' + this.name + '</h2>') .append('<h2 class="meal-restaurant">' + r.name + '</h2>')
.append('<h3 class="meal-food">' + (this.short_description || ('Top Order: ' + (this.top_name ? (this.top_name || this.top_name) : ''))) + '</h3>'); .append('<h3 class="meal-food">' + (r.short_description || ('Top Order: ' + (r.top_name ? (r.top_name || r.top_name) : ''))) + '</h3>');
if (this.open()) { if (r.open()) {
if (this.delivery != '1') { if (r.delivery != '1') {
restaurantContent.append('<div class="meal-item-tag">Take out only</div>'); restaurantContent.append('<div class="meal-item-tag">Take out only</div>');
} else if (this.isAboutToClose()) { } else if (r.isAboutToClose()) {
restaurantContent.append('<div class="meal-item-tag about-to-close">Hurry, closes in ' + this.isAboutToClose() +' min!</div>'); restaurantContent.append('<div class="meal-item-tag about-to-close">Hurry, closes in ' + r.isAboutToClose() +' min!</div>');
} else if (!this.delivery_fee) { } else if (!r.delivery_fee) {
// restaurantContent.append('<div class="meal-item-tag">Free Delivery</div>'); // restaurantContent.append('<div class="meal-item-tag">Free Delivery</div>');
}
} else {
restaurantContent.append('<div class="meal-item-tag-closed">Opens in a few hours</div>');
} }
} else {
restaurantContent.append('<div class="meal-item-tag-closed">Opens in a few hours</div>');
}
restaurant restaurant
.append('<div class="meal-item-spacer"></div>') .append('<div class="meal-item-spacer"></div>')
.append(restaurantContent); .append(restaurantContent);
$('.meal-items').append(restaurant);
$('.meal-items').append(restaurant);
});
} }
} }
@ -3279,4 +3293,66 @@ App.message.chrome = function( ){
App.message.show( title, message ); App.message.show( title, message );
} }
google.load('maps', '3', {callback: App.loc.preProcess, other_params: 'sensor=false'}); google.load('maps', '3', {callback: App.loc.preProcess, other_params: 'sensor=false'});
var sort_by;
(function() {
// utility functions
var default_cmp = function(a, b) {
if (a == b) return 0;
return a < b ? -1 : 1;
},
getCmpFunc = function(primer, reverse) {
var cmp = default_cmp;
if (primer) {
cmp = function(a, b) {
return default_cmp(primer(a), primer(b));
};
}
if (reverse) {
return function(a, b) {
return -1 * cmp(a, b);
};
}
return cmp;
};
// actual implementation
sort_by = function() {
var fields = [],
n_fields = arguments.length,
field, name, reverse, cmp;
// preprocess sorting options
for (var i = 0; i < n_fields; i++) {
field = arguments[i];
if (typeof field === 'string') {
name = field;
cmp = default_cmp;
}
else {
name = field.name;
cmp = getCmpFunc(field.primer, field.reverse);
}
fields.push({
name: name,
cmp: cmp
});
}
return function(A, B) {
var a, b, name, cmp, result;
for (var i = 0, l = n_fields; i < l; i++) {
result = 0;
field = fields[i];
name = field.name;
cmp = field.cmp;
result = cmp(A[name], B[name]);
if (result !== 0) break;
}
return result;
}
}
}());

View File

@ -145,12 +145,14 @@ var Restaurant = function(id) {
// If it doesn't have hours it means it is always opened // If it doesn't have hours it means it is always opened
if( !this._hours ){ if( !this._hours ){
this._open = true;
return true; return true;
} }
var isOpen = false; var isOpen = false;
var today = Date.today().toString('ddd').toLowerCase(); var today = Date.today().toString('ddd').toLowerCase();
if (this._hours == undefined || this._hours[today] == undefined) { if (this._hours == undefined || this._hours[today] == undefined) {
this._open = false;
return false; return false;
} }
todayHours = this._hours[today]; todayHours = this._hours[today];
@ -190,6 +192,7 @@ var Restaurant = function(id) {
} }
} }
} }
this._open = isOpen;
return isOpen; return isOpen;
} }