This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
memberdb/application/classes/Model/Setup.php

178 lines
4.5 KiB
PHP
Raw Normal View History

2014-07-09 03:04:56 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Membership Database Setup Model
*
* This module must remain in applications/ as it is used very early in the
* Membership Database initialisation.
*
* @package Membership Database
* @category Models
* @author Deon George
* @copyright (c) 2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Setup extends ORM {
// Setup doesnt use the update column
protected $_updated_column = FALSE;
protected $_has_one = array(
'account'=>array('foreign_key'=>'id','far_key'=>'admin_id'),
'country'=>array('foreign_key'=>'id','far_key'=>'country_id'),
'language'=>array('foreign_key'=>'id','far_key'=>'language_id'),
);
protected $_has_many = array(
'dates'=>array('model'=>'Site_Dates','far_key'=>'id','foreign_key'=>'site_id'),
'rooms'=>array('far_key'=>'id','foreign_key'=>'site_id'),
);
2014-07-09 03:04:56 +00:00
protected $_compress_column = array(
'module_config',
'site_details',
);
// Validation rules
public function rules() {
$x = Arr::merge(parent::rules(), array(
'url' => array(
array('not_empty'),
array('min_length', array(':value', 8)),
array('max_length', array(':value', 127)),
array('url'),
),
));
// This module doesnt use site_id.
unset($x['site_id']);
return $x;
}
/**
* Get/Set Module Configuration
*
* @param $key Module name.
* @param $value Values to store. If NULL, retrieves the value stored, otherwise stores value.
*/
public function module_config($key,array $value=NULL) {
// If we are not loaded, we dont have any config.
if (! $this->loaded() OR (is_null($value) AND ! $this->module_config))
return array();
$mo = ORM::factory('Module',array('name'=>$key));
if (! $mo->loaded())
throw new Kohana_Exception('Unknown module :name',array(':name'=>$key));
$mc = $this->module_config ? $this->module_config : array();
// If $value is NULL, we are a getter
if ($value === NULL)
return empty($mc[$mo->id]) ? array() : $mc[$mo->id];
// Store new value
$mc[$mo->id] = $value;
$this->module_config = $mc;
return $this;
}
public function module_config_id($key=NULL) {
$result = array();
foreach (array_keys($this->module_config) as $mid) {
if (is_null($key) OR $key == $mid) {
$result[$mid] = array(
'object'=>ORM::factory('Module',$mid),
'data'=>$this->module_config[$mid],
);
// If we are just after our key, we can continue here
if ($key AND $key==$mid)
break;
}
}
return $result;
}
2014-10-22 11:22:47 +00:00
public function open_days() {
$result = array();
//@todo this needs to change to node have any dates, since the current date may be closed, or a public holiday
foreach ($this->open_dates(Site::DateStartOfWeek(time()),7) as $date => $open)
$result[date('w',$date)] = $open;
ksort($result);
return $result;
}
public function open_dates($date,$days=0,$code='O') {
$result = array();
$date_end = $date+$days*86400;
2014-10-22 11:22:47 +00:00
foreach ($this->dates->where('code','=',$code)->where_startstop($date,$date_end)->find_all() as $o)
while ($date<$date_end) {
// If we havent made the start date yet, we need to advance
if ($o->date_start > $date AND $o->date_stop > $date_end) {
$result[$date] = FALSE;
$date += 86400;
continue;
}
// Check that this record covers our current date
if ($o->date_stop < $date)
break;
$result[$date] = $o->open(date('w',$date)) ? TRUE : FALSE;
$date += 86400;
}
// If we broke out and our date $days hasnt ben evaluated, we are closed
while ($date<$date_end) {
$result[$date] = FALSE;
$date += 86400;
}
return $result;
}
2014-07-09 03:04:56 +00:00
/**
* Get/Set our Site Configuration from the DB
*
* @param $key Key
* @param $value Values to store. If NULL, retrieves the value stored, otherwise stores value.
*/
public function site_details($key,array $value=NULL) {
if (! in_array($key,array('name','address1','address2','city','state','pcode','phone','fax','email','faqurl')))
throw new Kohana_Exception('Unknown Site Configuration Key :key',array(':key'=>$key));
// If $value is NULL, we are a getter
if ($value === NULL)
return empty($this->site_details[$key]) ? '' : $this->site_details[$key];
// Store new value
$sc[$key] = $value;
return $this;
}
2014-10-22 11:22:47 +00:00
public function total_places($date_start,$days=0) {
$result = array();
foreach ($this->rooms->find_all() as $ro) {
foreach ($ro->availability_dates($date_start,$days) as $date => $total) {
if (! isset($result[$date]))
$result[$date] = 0;
$result[$date] += $total;
}
}
return $result;
}
2014-07-09 03:04:56 +00:00
}
?>