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/Child.php

166 lines
4.8 KiB
PHP
Raw Normal View History

2014-09-05 12:19:22 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
2014-12-01 03:01:02 +00:00
* This class supports Children
2014-09-05 12:19:22 +00:00
*
* @package Membership Database
* @category Models
* @author Deon George
* @copyright (c) 2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Child extends ORM {
2014-10-22 11:22:47 +00:00
protected $_start = '42D'; // Minimum age to be accepted (42D = 6 Weeks)
protected $_max = '4Y'; // Maximum age
protected $_max_date = '04-30'; // Date Maximum age must be met, eg: 5Y on April 30
protected $_max_return = '12-31'; // Date to leave the center when max date reached, eg: Jan 1
protected $_has_many = array(
2016-08-30 16:13:05 +00:00
'rooms'=>array('through'=>'room_children'),
2014-10-22 11:22:47 +00:00
);
public function filters() {
return Arr::merge(parent::filters(),array(
'dob'=>array(array(array($this,'date'), array(':value'))),
2014-10-22 11:22:47 +00:00
));
}
protected $_display_filters = array(
2014-12-01 03:01:02 +00:00
'date_orig'=>array(
array('Site::Date',array(':value')),
),
'date_last'=>array(
array('Site::Date',array(':value')),
),
2014-10-22 11:22:47 +00:00
'dob'=>array(
array('Site::Date',array(':value')),
),
);
protected $_sub_items_load = array(
2016-08-30 16:13:05 +00:00
'rooms'=>array('date_start','date_stop'),
2014-10-22 11:22:47 +00:00
);
private function _dob() {
$x = new DateTime();
return $x->setTimestamp($this->dob);
}
public function age($asat=NULL,$format='%Yy%Mm') {
if (is_null($asat))
$asat = time();
$dob = $this->_dob();
$today = new DateTime();
$today->setTimestamp($asat);
return $format == '%w' ? sprintf('%02dw',$dob->diff($today)->days/7) : $dob->diff($today)->format($format);
}
public function date($value) {
2014-12-01 03:01:02 +00:00
return is_numeric($value) ? $value : strtotime($value);
}
2014-10-22 11:22:47 +00:00
public function date_enrol_min() {
$dob = $this->_dob();
$dob->add(new DateInterval('P'.$this->_start));
return $format ? $result->format(Kohana::$config->load('config')->date_format) : $result->format('U');
}
public function date_enrol_max($format=FALSE) {
$dob = $this->_dob();
$dob->add(new DateInterval('P'.$this->_max));
$last = new DateTime(sprintf('%s-%s',$dob->format('Y'),$this->_max_date));
$x = $dob->diff($last);
$result = new DateTime(sprintf('%s-%s',$dob->format('Y')+($x->invert ? 1 : 0),$this->_max_return));
return $format ? $result->format(Company::instance()->date_format()) : $result->format('U');
}
2014-12-01 03:01:02 +00:00
public function days($room_id,$date_start,$days,$code,$markup=FALSE) {
$result = '';
$date_end = $date_start+$days*86400;
$open_dates = Company::instance()->so()->open_dates($date_start,$days);
$o = $this->room->where('code','=',$code);
$o->where_startstop($date_start,$date_end);
if (! is_null($room_id))
$o->where('room_id','=',$room_id);
// Set all open dayes to 0
foreach ($open_dates as $k=>$v)
if ($v === TRUE)
$open_dates[$k] = 0;
foreach ($o->find_all() as $date => $rco) {
foreach ($open_dates as $day=>$open) {
if ($open === FALSE or $rco->date_start > $day OR $rco->date_stop < $day)
continue;
$open_dates[$day] = $rco->day(date('w',$day));
}
}
foreach ($open_dates as $day=>$open) {
$dayname = substr(date('D',$day),0,1);
if ($open === FALSE)
$result .= $markup ? sprintf('<span class="fa-stack text-danger"><i class="fa fa-square fa-stack-2x"></i><span class="fa-stack-1x" style="color: white;">%s</span></span>',$dayname) : '-';
elseif ($open)
$result .= $markup ? sprintf('<span class="fa-stack text-success"><i class="fa %s fa-stack-2x"></i><strong class="fa-stack-1x" style="color: %s;">%s</strong></span>',($code=='P' ? 'fa-circle' : 'fa-circle-o'),($code=='P' ? 'white' : 'black'),$dayname) : $dayname;
else
$result .= $markup ? sprintf('<span class="fa-stack"><i class="fa fa-square-o fa-stack-2x"></i><strong class="fa-stack-1x">%s</strong></span>',$dayname) : strtolower($dayname);
}
return $result;
}
public function name() {
return sprintf('%s, %s',strtoupper($this->family_name),$this->first_name);
}
2014-10-22 11:22:47 +00:00
public function save(Validation $validation=NULL) {
$changed = $this->changed();
parent::save($validation);
2014-12-01 03:01:02 +00:00
// Process our Sub-Items and Validate them.
Sort::MASort($this->_sub_items,array('code','room_id','date_start','date_stop'));
$last = NULL;
foreach ($this->_sub_items as $rco) {
// If no dates are selected, clear this record.
if (! $rco->have_days()) {
if ($rco->loaded())
$rco->delete();
continue;
}
2014-10-22 11:22:47 +00:00
2014-12-01 03:01:02 +00:00
// If there is no last item, we'll accept this as is.
if (is_null($last) OR ($last->date_stop <= $rco->date_start)) {
$rco->save($validation);
$last = $rco;
2014-10-22 11:22:47 +00:00
continue;
2014-12-01 03:01:02 +00:00
}
2014-10-22 11:22:47 +00:00
2014-12-01 03:01:02 +00:00
// @todo: If our dates overlap, fix that
// @todo: Check that casual days dont overlap with permanent days
// @todo: Check that absent days are attending days
// @todo: Check that waitlist days dont overlap with permanent days
2014-10-22 11:22:47 +00:00
2014-12-01 03:01:02 +00:00
if ($rco->changed() AND (! $rco->save()))
$rco->reload();
}
2014-10-22 11:22:47 +00:00
return $this->reload();
}
2014-09-05 12:19:22 +00:00
}
?>