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

@ -634,6 +634,18 @@ class Crunchbutton_Restaurant extends Cana_Table
}
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.
*
@ -646,6 +658,7 @@ class Crunchbutton_Restaurant extends Cana_Table
public function exports($ignore = []) {
$out = $this->properties();
$out['_open'] = $this->open();
$out['_weight'] = $this->weight();
$timezone = new DateTimeZone( $this->timezone );
$date = new DateTime( 'now ', $timezone ) ;
@ -723,10 +736,15 @@ class Crunchbutton_Restaurant extends Cana_Table
public static function byRange($params) {
$params[ 'miles' ] = ( $params[ 'miles' ] ) ? $params[ 'miles' ] : 2;
$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
count(*) as _weight,
((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.*
FROM `restaurant`
LEFT JOIN `order` using(id_restaurant)
WHERE
active = 1
GROUP BY restaurant.id_restaurant
HAVING
takeout = 1
AND
@ -737,7 +755,7 @@ class Crunchbutton_Restaurant extends Cana_Table
delivery = 1
AND
`distance` <= `delivery_radius`
ORDER BY name ASC;
ORDER BY _weight DESC;
';
return self::q($query);
}

View File

@ -3038,7 +3038,23 @@ App.foodDelivery.preProcess = function() {
$('input').blur();
return;
} 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 ){
App.foodDelivery.forceProcess = false;
App.page.foodDelivery.load();
@ -3125,24 +3141,22 @@ App.page.foodDelivery.load = function(){
App.hasLocation = true;
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'+ (!this.open() ? ' meal-item-closed' : '') +'" data-id_restaurant="' + this.id_restaurant + '" data-permalink="' + this.permalink + '"></div>');
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">');
restaurantContent
.append('<div class="meal-pic" style="background: url(' + this.img64 + ');"></div>')
.append('<h2 class="meal-restaurant">' + this.name + '</h2>')
.append('<h3 class="meal-food">' + (this.short_description || ('Top Order: ' + (this.top_name ? (this.top_name || this.top_name) : ''))) + '</h3>');
.append('<div class="meal-pic" style="background: url(' + r.img64 + ');"></div>')
.append('<h2 class="meal-restaurant">' + r.name + '</h2>')
.append('<h3 class="meal-food">' + (r.short_description || ('Top Order: ' + (r.top_name ? (r.top_name || r.top_name) : ''))) + '</h3>');
if (this.open()) {
if (this.delivery != '1') {
if (r.open()) {
if (r.delivery != '1') {
restaurantContent.append('<div class="meal-item-tag">Take out only</div>');
} else if (this.isAboutToClose()) {
restaurantContent.append('<div class="meal-item-tag about-to-close">Hurry, closes in ' + this.isAboutToClose() +' min!</div>');
} else if (!this.delivery_fee) {
} else if (r.isAboutToClose()) {
restaurantContent.append('<div class="meal-item-tag about-to-close">Hurry, closes in ' + r.isAboutToClose() +' min!</div>');
} else if (!r.delivery_fee) {
// restaurantContent.append('<div class="meal-item-tag">Free Delivery</div>');
}
} else {
@ -3154,7 +3168,7 @@ App.page.foodDelivery.load = function(){
.append(restaurantContent);
$('.meal-items').append(restaurant);
});
}
}
@ -3280,3 +3294,65 @@ App.message.chrome = function( ){
}
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( !this._hours ){
this._open = true;
return true;
}
var isOpen = false;
var today = Date.today().toString('ddd').toLowerCase();
if (this._hours == undefined || this._hours[today] == undefined) {
this._open = false;
return false;
}
todayHours = this._hours[today];
@ -190,6 +192,7 @@ var Restaurant = function(id) {
}
}
}
this._open = isOpen;
return isOpen;
}