예제 #1
0
 /**
  * Find {@link Todo} by identifier.
  * @return Todo Todo or <i>null</i> if not found
  */
 public function findById($id)
 {
     $row = $this->query('SELECT * FROM todo WHERE deleted = 0 and id = ' . (int) $id)->fetch();
     if (!$row) {
         return null;
     }
     $todo = new Todo();
     TodoMapper::map($todo, $row);
     return $todo;
 }
예제 #2
0
$edit = array_key_exists('id', $_GET);
if ($edit) {
    $todo = Utils::getTodoByGetId();
} else {
    // set defaults
    $todo = new Todo();
    $todo->setPriority(Todo::PRIORITY_MEDIUM);
    $dueOn = new DateTime("+1 day");
    $dueOn->setTime(0, 0, 0);
    $todo->setDueOn($dueOn);
}
if (array_key_exists('cancel', $_POST)) {
    // redirect
    Utils::redirect('detail', array('id' => $todo->getId()));
} elseif (array_key_exists('save', $_POST)) {
    // for security reasons, do not map the whole $_POST['todo']
    $data = array('title' => $_POST['todo']['title'], 'due_on' => $_POST['todo']['due_on_date'] . ' ' . $_POST['todo']['due_on_hour'] . ':' . $_POST['todo']['due_on_minute'] . ':00', 'priority' => $_POST['todo']['priority'], 'description' => $_POST['todo']['description'], 'comment' => $_POST['todo']['comment']);
    // map
    TodoMapper::map($todo, $data);
    // validate
    $errors = TodoValidator::validate($todo);
    // validate
    if (empty($errors)) {
        // save
        $dao = new TodoDao();
        $todo = $dao->save($todo);
        Flash::addFlash('TODO saved successfully.');
        // redirect
        Utils::redirect('detail', array('id' => $todo->getId()));
    }
}