Stuardo Rodríguez bb852de0c4 fixed #736, "inactive" items are randomly activated, at least when logged into admin
* fixing the problem of the admin loading the community and all it's dependencies twice because a naming collition
* replacing $this->community for $this->restaurantCommunity in the admin/restaurant form because of the naming collition
* detecting if the call has been from the admin or from the front end because API calls do not send params
* unifying the return exit in the API _returnRestaurant() action
* sending the $where filters from the API controller instead of detecint it in the models
* sending the $where param in cascade from the API  to the Restaurant model to the Category model for the Category->dishes($where) filter
* improving the readability in the bundle where the community is sent, adding documentation about the $view->category variable in the view file
2013-02-25 12:27:45 -05:00

77 lines
1.8 KiB
PHP

<?php
/**
* Dish categories to group the dishes in a restaurant.
*
* @package Crunchbutton.Category
* @category model
*
* @property int id_restaurant
* @property int id_category
* @property string name
* @property int sort
*/
class Crunchbutton_Category extends Cana_Table {
public function restaurant() {
return Restaurant::o($this->id_restaurant);
}
public function community() {
return $this->restaurant()->community();
}
/**
* Return the dishes ffor the current category
*
* @param array $where Associative array with the WHERE filters
*
* @return Crunchbutton_Dish[]
*/
public function dishes($where = []) {
if (!isset($this->_dishes)) {
$defaultFilters = [
'id_restaurant' => $this->id_restaurant,
'id_category' => $this->id_category,
'active' => 1,
];
$whereSql = $this->_mergeWhere($defaultFilters, $where);
$sql = "SELECT * FROM dish WHERE $whereSql ORDER BY sort ASC";
$this->_dishes = Dish::q($sql);
}
return $this->_dishes;
}
/**
* 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();
}
return $out;
}
public function name() {
return $this->name.($this->loc ? (' '.$this->community()->prep.' '.$this->community()->name) : '');
}
public function __construct($id = null) {
parent::__construct();
$this
->table('category')
->idVar('id_category')
->load($id);
}
}