This commit is contained in:
Daniel Camargo 2013-02-25 14:46:43 -03:00
commit b5ae265f1e
7 changed files with 112 additions and 40 deletions

View File

@ -10,6 +10,9 @@ class Controller_admin_restaurants extends Crunchbutton_Controller_Account
/**
* Default method to show a restaurant form
*
* You can't use the $view->community var to set the current restaurant community
* as that var is used for something else.
*
* @return void
*/
private function _form()
@ -19,22 +22,22 @@ class Controller_admin_restaurants extends Crunchbutton_Controller_Account
$communities = Community::q('SELECT * FROM community');
if (count($this->restaurant->community()->items())) {
$community = $this->restaurant->community()->items()[0];
$community = $this->restaurant->community()->items()[0];
} else {
$community = new Crunchbutton_Community();
$community = $community->getTest();
$community = new Crunchbutton_Community();
$community = $community->getTest();
}
$view->communities = $communities;
$view->community = $community;
$view->communities = $communities;
$view->restaurantCommunity = $community;
$view->display('admin/restaurants/restaurant');
}
/**
* Shows all the restaurants
*
* @return void
*/
/**
* Shows all the restaurants
*
* @return void
*/
private function _list()
{
$view = Cana::view();

View File

@ -91,19 +91,30 @@ class Controller_api_restaurant extends Crunchbutton_Controller_Rest {
/**
* Echo JSON with restaurant data
*
* We do not use the Restaurant->json() method as we need to send the $where
* variable to the Restaurant->export() metod after we detected the API was
* called from the admin side
*
* @return void
*/
private function _returnRestaurant()
{
$out = Restaurant::o(c::getPagePiece(2));
if (!$out->id_restaurant) {
$out = Restaurant::permalink(c::getPagePiece(2));
$restaurant = Crunchbutton_Restaurant::o(c::getPagePiece(2));
/* @var $restaurant Crunchbutton_Restaurant */
if (!$restaurant->id_restaurant) {
$restaurant = Crunchbutton_Restaurant::permalink(c::getPagePiece(2));
}
if ($out->id_restaurant) {
echo $out->json();
if ($restaurant->id_restaurant) {
$where = [];
if (preg_match('/admin/i',$_SERVER['HTTP_REFERER'])) { // if API is being called by the admin
$where['Dish']['active'] = NULL;
}
$json = json_encode($restaurant->exports($ignore = [], $where));
} else {
echo json_encode(['error' => 'invalid object']);
$json = json_encode(['error' => 'invalid object']);
}
echo $json;
}
public function init() {

View File

@ -33,10 +33,6 @@ class Crunchbutton_Category extends Cana_Table {
'id_category' => $this->id_category,
'active' => 1,
];
if (isset($_SESSION['admin'])) {
$where['active'] = NULL;
}
$whereSql = $this->_mergeWhere($defaultFilters, $where);
$sql = "SELECT * FROM dish WHERE $whereSql ORDER BY sort ASC";
$this->_dishes = Dish::q($sql);
@ -44,8 +40,23 @@ class Crunchbutton_Category extends Cana_Table {
return $this->_dishes;
}
public function exports() {
/**
* Exports the category and it's active dishes
*
* You can overwrite the thishes filter like sending both active and inactive
* dishes by sending $where = ['Dish']['active' => NULL];
*
* @param array $where SQL WHERE filters
* @return array
*/
public function exports($where = []) {
$out = $this->properties();
if (isset($where['Dish'])) {
$dishes = $this->dishes($where['Dish']);
} else {
$dishes = $this->dishes();
}
foreach ($this->dishes() as $dish) {
$out['_dishes'][] = $dish->exports();
}

View File

@ -56,6 +56,13 @@ class Crunchbutton_Community extends Cana_Table {
return $this->_restaurants;
}
/**
* Returns all data related to this Community
*
* @return array
*
* @see Cana_Table::exports()
*/
public function exports() {
$out = $this->properties();
$out[ 'name_alt' ] = $this->name_alt();

View File

@ -45,10 +45,12 @@ class Crunchbutton_Restaurant extends Cana_Table
/**
* Return the dishes for the restaurant
*
* Save actions should fetch all by addint active=null in the $where param
*
* Save actions should fetch all by addint active=null in the $where param
*
* @param string[] $where Associative array with the filters to use to fetch the dishes
*
* @todo Why is the restaurant calling the dishes directly instead of using the categoyr->dishes() method?
*/
public function dishes($where = []) {
if (!isset($this->_dishes)) {
@ -633,7 +635,7 @@ class Crunchbutton_Restaurant extends Cana_Table
return $thumb;
}
public function weight() {
if (!isset($this->_weight)) {
$res = self::q('
@ -652,17 +654,18 @@ class Crunchbutton_Restaurant extends Cana_Table
* This is usualy used to JSON encode and send to the browser
*
* @param array $ignore An indexed array of what items not to ad to the export array
* @param array $where Adds a layer to filter the SQL WHERE statements
*
* @return array
*/
public function exports($ignore = []) {
public function exports($ignore = [], $where = []) {
$out = $this->properties();
$out['_open'] = $this->open();
$out['_weight'] = $this->weight();
$timezone = new DateTimeZone( $this->timezone );
$date = new DateTime( 'now ', $timezone ) ;
// Return the offset to help the Javascript to calculate the open/close hour correctly
$out['_tzoffset'] = ( $date->getOffset() ) / 60 / 60;
$out['_tzabbr'] = $date->format('T');
@ -674,9 +677,8 @@ class Crunchbutton_Restaurant extends Cana_Table
// $out['img64'] = '/assets/images/food/310x310/'.$this->image;
if (!$ignore['categories']) {
$categories = $this->categories();
foreach ($this->categories() as $category) {
$out['_categories'][] = $category->exports();
$out['_categories'][] = $category->exports($where);
}
}
@ -736,7 +738,7 @@ class Crunchbutton_Restaurant extends Cana_Table
public static function byRange($params) {
$params[ 'miles' ] = ( $params[ 'miles' ] ) ? $params[ 'miles' ] : 2;
$query = '
SELECT
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.*
@ -746,14 +748,14 @@ class Crunchbutton_Restaurant extends Cana_Table
active = 1
GROUP BY restaurant.id_restaurant
HAVING
takeout = 1
takeout = 1
AND
delivery = 0
AND
`distance` <= ' . $params[ 'miles' ] . '
OR
AND
`distance` <= ' . $params[ 'miles' ] . '
OR
delivery = 1
AND
AND
`distance` <= `delivery_radius`
ORDER BY _weight DESC;
';
@ -764,7 +766,7 @@ class Crunchbutton_Restaurant extends Cana_Table
foreach ($restaurants as $restaurant) {
$restaurant->_weight = (($restaurant->_weight / $sum) * 100) + $restaurant->weight_adj;
}
return $restaurants;
}

View File

@ -5,7 +5,9 @@
* @package Crunchbutton.Admin.Restaurant
* @category view
*/
/* @var $this Cana_View */
/* @var $this->restaurantCommunity Crunchbutton_Community */
/**
* View helper to preselect the <select> community form input
@ -261,7 +263,9 @@ input.notification {width: 400px;}
<select name="id_community" class="dataset-restaurant">
<? foreach ($this->communities as $community) : ?>
<? /* @var $community Crunchbutton_Community */ ?>
<option value="<?=$community->id_community?>" <?=($this->community) ? _selectedCommunity($this->community, $community) : ''?>>
<option value="<?=$community->id_community?>"
<?=($this->restaurantCommunity) ? _selectedCommunity($this->restaurantCommunity, $community) : ''?>
>
<?=$community->name?>
</option>
<? endforeach ; ?>

View File

@ -1,12 +1,46 @@
<? /*
<?
/**
* Still not sure what this does
*
* View variables you can't overwrite (see Controller_admin_restaurants::_form())
* - $this->category
*
* @package Cruncbutton.Bundle
* @category view
*
*/
/* @var $this Cana_View */
/* Exports current community as JSON
*
* @todo move this to a controller
*/
if ($this->community) {
/* @var $this->community Crunchbutton_Community */
if ($this->admin) {
$community = $this->community->exports();
} else {
$community = $this->community->exports();
}
$communityJson = json_encode($community);
$communityJson = str_replace("'","\\'", $communityJson);
}
/*
<script src="/assets/js/json2.js"></script>
<script src="/assets/js/jquery.min.js"></script>
<script src="/assets/js/jquery-ui.min.js"></script>
<script src="/assets/js/underscore-min.js"></script>
*/
?>
<script src="/assets/js/jquery.history.js"></script>
<script src="/assets/js/jquery.cookie.js"></script>
<script src="/assets/js/date.js"></script>
@ -31,8 +65,8 @@ $(function() {
stored: true
};
<? endforeach ; ?>
<? if ($this->community) : ?>
App.cached['Community'][<?=$this->community->id_community?>] = new Community(<?=str_replace("'","\\'",json_encode($this->community->exports()))?>);
<? if ($this->community && $communityJson) : ?>
App.cached['Community'][<?=$this->community->id_community?>] = new Community(<?=$communityJson?>);
App.cached['Community']['<?=$this->community->permalink?>'] = App.cached['Community'][<?=$this->community->id_community?>];
<? if (!c::getPagePiece(1)) : ?>
App.loadedPage = '<?=$this->community->permalink?>';