diff --git a/application/classes/Controller/User/Child.php b/application/classes/Controller/User/Child.php index fa9775a..76785bb 100644 --- a/application/classes/Controller/User/Child.php +++ b/application/classes/Controller/User/Child.php @@ -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') diff --git a/application/classes/Model/Child.php b/application/classes/Model/Child.php index 15dc3d8..3a8d1c3 100644 --- a/application/classes/Model/Child.php +++ b/application/classes/Model/Child.php @@ -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)); diff --git a/application/views/child/user/add_edit.php b/application/views/child/user/add_edit.php index 56b32f3..447dcd1 100644 --- a/application/views/child/user/add_edit.php +++ b/application/views/child/user/add_edit.php @@ -22,7 +22,7 @@
- Age: age(); ?>
+ Age:
dob ? $o->age() : ''; ?>

@@ -60,7 +60,7 @@
- date_stop ? $rco->display('date_stop') : $o->date_enrol_max(TRUE),array('class'=>'form-control','placeholder'=>'End','required','nocg'=>TRUE,'readonly')); ?> + date_stop ? $rco->display('date_stop') : '',array('class'=>'form-control','placeholder'=>'End','required','nocg'=>TRUE,'readonly')); ?>