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

100 lines
2.6 KiB
PHP
Raw Normal View History

2014-09-05 12:19:22 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
2014-10-22 11:22:47 +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(
'room'=>array('model'=>'Room_Children','foreign_key'=>'child_id','far_key'=>'id'),
);
public function filters() {
return Arr::merge(parent::filters(),array(
'dob'=>array(array('strtotime', array(':value'))),
));
}
protected $_display_filters = array(
'dob'=>array(
array('Site::Date',array(':value')),
),
);
protected $_sub_items_load = array(
'room'=>'date_start,date_stop',
);
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_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');
}
public function save(Validation $validation=NULL) {
$changed = $this->changed();
parent::save($validation);
// Insert into waitlist
$rco = ORM::factory('Room_Children',array('child_id'=>$this,'code'=>$_POST['room']['code']));
$rco->values($_POST['room']);
$rco->child_id = (string)$this;
foreach ($_POST['room']['R'] as $k => $v) {
if (! $v OR isset($_POST['room']['d_'.$k]))
continue;
$rco->{'d_'.$k} = NULL;
}
if ($rco->changed() AND (! $rco->save()))
$rco->reload()->values($_POST['room']);
return $this->reload();
}
2014-09-05 12:19:22 +00:00
}
?>