public function saveschedule() { try { if (!Check::digits($_POST['study_id'], $empty = false)) { throw new Exception("bad study id!"); } else { $study_id = $_POST['study_id']; } if (!Check::digits($_POST['task_id'])) { throw new Exception("bad task id!"); } else { $task_id = $_POST['task_id']; } // start and end date are taken from the study itself if (Check::isdate($_POST['startdate'])) { $startdate = $_POST['startdate']; } else { throw new Exception("bad startdate"); } if (Check::isdate($_POST['enddate'])) { $enddate = $_POST['enddate']; } else { throw new Exception("bad enddate"); } list($startdate, $enddate) = Check::order($startdate, $enddate); $st = new Study(); $study = $st->getone($study_id); if ($startdate < $study['startdate']) { $startdate = $study['startdate']; } if ($enddate > $study['enddate']) { $enddate = $study['enddate']; } $timesofday = trim($_POST['timesofday']); $timesofday = preg_replace('#\\s#', '', $timesofday); $timesofday = preg_replace('#,#', ';', $timesofday); if (!preg_match('#^(?:\\d\\d?\\:\\d\\d;|\\d\\d?\\:\\d\\d$)*$#', $timesofday)) { throw new Exception("bad timesofday - format HH:MM;..."); } $tsod = array(); foreach (explode(";", $timesofday) as $tod) { if (empty($tod)) { continue; } list($hour, $min) = explode(":", $tod); if ($hour >= 0 and $hour <= 23 and $min >= 0 and $min <= 59) { $tsod[] = sprintf("%02d:%02d", $hour, $min); } } $timesofday = implode(";", $tsod); $daysofweek = $_POST['daysofweek']; $daysofweek = preg_replace('#\\s#', '', $daysofweek); $daysofweek = preg_replace('#;#', ',', $daysofweek); if (!preg_match('#^(?:\\w+(?:,|$))*#', $daysofweek)) { throw new Exception("bad daysofweek should be: Mon,Tue,..."); } $dsow = array(); foreach (explode(",", $daysofweek) as $dow) { if (!preg_match('#^(mon|tue|wed|thu|fri|sat|sun)?#i', $dow, $m)) { continue; } if ($m[1] == "") { continue; } $dsow[] = ucfirst(strtolower($m[1])); } $daysofweek = implode(",", $dsow); $s = new Schedule(); if ($s->upd(array('task_id' => $task_id, 'study_id' => $study_id), array('startdate' => $startdate, 'enddate' => $enddate, 'timesofday' => $timesofday, 'daysofweek' => $daysofweek)) === false) { throw new Exception($s->err()); } View::assign('task_id', $task_id); View::assign('study_id', $study_id); return 'task.tpl'; } catch (Exception $e) { $this->err($e); View::assign('error', $this->error); return 'error.tpl'; } }