Better handling of dates when registering children

This commit is contained in:
Deon George 2014-10-23 10:05:32 +11:00
parent 48debfbd6c
commit 6eb0b2f8dd
3 changed files with 63 additions and 8 deletions

View File

@ -13,6 +13,8 @@ class Controller_User_Child extends Controller_Child {
protected $auth_required = TRUE;
protected $secure_actions = array(
'add'=>TRUE,
'ajaxage'=>FALSE,
'ajaxagemax'=>FALSE,
'edit'=>TRUE,
);
@ -24,6 +26,18 @@ class Controller_User_Child extends Controller_Child {
->body($this->add_edit());
}
public function action_ajaxage() {
$x = ORM::factory('Child');
$x->dob = $this->request->query('date');
$this->template->content = $x->age();
}
public function action_ajaxagemax() {
$x = ORM::factory('Child');
$x->dob = $this->request->query('date');
$this->template->content = $x->date_enrol_max();
}
public function action_edit() {
Block::factory()
->type('form-horizontal')
@ -59,11 +73,48 @@ class Controller_User_Child extends Controller_Child {
Script::factory()
->type('stdin')
->data('
$(document).ready(function() {
$("#date_stop").datepicker({
endDate: new Date('.($co->date_enrol_max()*1000).')
});
})
$(document).ready(function() {
'.($co->loaded() ? '
$("#date_stop").datepicker({
endDate: new Date('.($co->date_enrol_max()*1000).')
});
' : '').'
$("#dob").datepicker().on("hide",function(e) {
var x = $("#dob").datepicker("getDate").getTime()/1000;
// Send the request and update sub category dropdown
$.ajax({
type: "GET",
data: "date="+x,
dataType: "html",
cache: false,
url: "'.URL::link('user','child/ajaxage',TRUE).'",
timeout: 2000,
error: function(x) {
alert("Failed to submit");
},
success: function(data) {
$("div[id=age]").empty().append(data);
}
});
$.ajax({
type: "GET",
data: "date="+x,
dataType: "html",
cache: false,
url: "'.URL::link('user','child/ajaxagemax',TRUE).'",
timeout: 2000,
error: function(x) {
alert("Failed to submit");
},
success: function(data) {
$("#date_stop").datepicker("setEndDate",new Date(data*1000));
}
});
});
});
');
return View::factory('child/user/add_edit')

View File

@ -21,7 +21,7 @@ class Model_Child extends ORM {
public function filters() {
return Arr::merge(parent::filters(),array(
'dob'=>array(array('strtotime', array(':value'))),
'dob'=>array(array(array($this,'date'), array(':value'))),
));
}
@ -53,6 +53,10 @@ class Model_Child extends ORM {
return $format == '%w' ? sprintf('%02dw',$dob->diff($today)->days/7) : $dob->diff($today)->format($format);
}
public function date($value) {
return is_numeric($value) ? $value : strtotime($value);
}
public function date_enrol_min() {
$dob = $this->_dob();
$dob->add(new DateInterval('P'.$this->_start));

View File

@ -22,7 +22,7 @@
</div>
</div>
<div class="col-md-3">
Age: <strong><?php echo $o->age(); ?></strong><br/>
Age: <strong><div id="age" style="display: inline;"><?php echo $o->dob ? $o->age() : ''; ?></div></strong><br/>
</div>
</div>
@ -60,7 +60,7 @@
</td>
<td>
<div class="input-group date" id="date_stop" data-date-format="dd-mm-yyyy" data-provide="datepicker" data-date-start-view="year" data-date-autoclose="true" data-date-today-highlight="true" data-date-start-date="0d">
<?php echo Form::input('room[date_stop]',$rco->date_stop ? $rco->display('date_stop') : $o->date_enrol_max(TRUE),array('class'=>'form-control','placeholder'=>'End','required','nocg'=>TRUE,'readonly')); ?>
<?php echo Form::input('room[date_stop]',$rco->date_stop ? $rco->display('date_stop') : '',array('class'=>'form-control','placeholder'=>'End','required','nocg'=>TRUE,'readonly')); ?>
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
</td>