Stuardo Rodríguez 97018268ee fixed #685
* the admin controller loads the testing community as the default for new restaurants
* the Community model encapsulates the testing community fetching
* the view accepts not setting a default community
2013-01-30 14:12:37 -05:00

90 lines
2.1 KiB
PHP

<?php
class Crunchbutton_Community extends Cana_Table {
public static function all($force = null) {
$ip = preg_replace('/[^0-9\.]+/','',$_SERVER['REMOTE_ADDR']);
$force = preg_replace('/[^a-z\-]+/','',$force);
if ($force) {
$forceq = ' OR (community.permalink="'.c::db()->escape($force).'") ';
}
$q = '
select community.* from community
left join community_ip on community_ip.id_community=community.id_community
where
community.active=1
AND (
( community.private=0 )
OR
(community.private=1 AND community_ip.ip="'.c::db()->escape($ip).'")
'.$forceq.'
)
group by community.id_community
order by name
';
return self::q($q);
}
/**
* Returns all the restaurants that belong to this Community
*
* @return Cana_Iterator
*
* @todo probably not required to sort them as the front end sorts them
*/
public function restaurants() {
if (!isset($this->_restaurants)) {
$this->_restaurants = Restaurant::q('
SELECT
restaurant.*
, restaurant_community.sort
FROM restaurant
left join restaurant_community using(id_restaurant)
WHERE
id_community="'.$this->id_community.'"
and restaurant.active=1
ORDER by
restaurant_community.sort,
restaurant.delivery DESC
');
$this->_restaurants->sort([
'function' => 'open'
]);
}
return $this->_restaurants;
}
public function exports() {
$out = $this->properties();
foreach ($this->restaurants() as $restaurant) {
$out['_restaurants'][$restaurant->id_restaurant.' '] = $restaurant->exports();
}
return $out;
}
public static function permalink($permalink) {
return self::q('select * from community where permalink="'.$permalink.'"')->get(0);
}
public function __construct($id = null) {
parent::__construct();
$this
->table('community')
->idVar('id_community')
->load($id);
}
/**
* Returns the Testing community
*
* @return Crunchbutton_Community
*/
public function getTest()
{
$row = $this->q('SELECT * FROM community WHERE name="Testing" ')->current();
return $row;
}
}