public function savestudy() { try { # check login if (!Check::digits($rid = $_SESSION['user']['researcher_id'])) { throw new Exception('invalid researcher id!'); } # check input values foreach ($_POST as $pfield => $pvalue) { $_POST[$pfield] = trim($pvalue); } if (empty($_POST['study_title'])) { throw new Exception('need a study title!'); } if (!Check::isdate($_POST['startdate'], false)) { throw new Exception('bad startdate format should be YYYY-MM-DD'); } if (!Check::isdate($_POST['enddate'], false)) { throw new Exception('bad enddate format should be YYYY-MM-DD'); } list($_POST['startdate'], $_POST['enddate']) = Check::order($_POST['startdate'], $_POST['enddate']); # first create or save the study $s = new Study(); if (Check::digits($_POST['study_id'], $empty = false)) { $study_id = $_POST['study_id']; unset($_POST['study_id']); if ($s->upd($study_id, $_POST) === false) { throw new Exception($s->err()); } } else { if ($s->ins($_POST) === false) { throw new Exception($s->err()); } $study_id = $s->getid(); } # associate the study with the researcher $r = new Research(); if ($r->ins(array('study_id' => $study_id, 'researcher_id' => $rid)) === false) { throw new Exception($r->err()); } View::assign('study_id', $study_id); return 'study.tpl'; } catch (Exception $e) { $this->err($e); View::assign('error', $this->error); return 'error.tpl'; } }