/** * Retrieves tasks from the database. * * @param integer $completed Which tasks to retrieve (1 = all tasks, * 0 = incomplete tasks, 2 = complete tasks, * 3 = future tasks, 4 = future and incomplete * tasks). * @param boolean $include_history Include created/changed data from * Horde_History. * * @throws Nag_Exception */ public function retrieve($completed = Nag::VIEW_ALL, $include_history = true) { /* Build the SQL query. */ $query = 'SELECT * FROM nag_tasks WHERE task_owner = ?'; $values = array($this->_tasklist); switch ($completed) { case Nag::VIEW_INCOMPLETE: $query .= ' AND task_completed = 0 AND (task_start IS NULL OR task_completions IS NOT NULL OR task_start = 0 OR task_start < ?)'; $values[] = $_SERVER['REQUEST_TIME']; break; case Nag::VIEW_COMPLETE: $query .= ' AND task_completed = 1'; break; case Nag::VIEW_FUTURE: $query .= ' AND task_completed = 0 AND (task_completions IS NOT NULL OR task_start > ?)'; $values[] = $_SERVER['REQUEST_TIME']; break; case Nag::VIEW_FUTURE_INCOMPLETE: $query .= ' AND task_completed = 0'; break; } try { $result = $this->_db->select($query, $values); } catch (Horde_Db_Exception $e) { throw new Nag_Exception($e->getMessage()); } /* Store the retrieved values in a fresh task list. */ $this->tasks = new Nag_Task(); $dict = array(); foreach ($result as $row) { $task = new Nag_Task($this, $this->_buildTask($row, $include_history)); if (($completed == Nag::VIEW_INCOMPLETE || $completed == Nag::VIEW_FUTURE) && $task->start && $task->recurs()) { $start = $task->getNextStart(); if ($completed == Nag::VIEW_INCOMPLETE && $start->after($_SERVER['REQUEST_TIME']) || $completed == Nag::VIEW_FUTURE && $start->before($_SERVER['REQUEST_TIME'])) { continue; } } /* Add task directly if it is a root task, otherwise store it in * the dictionary. */ if (empty($row['task_parent'])) { $this->tasks->add($task); } else { $dict[$row['task_id']] = $task; } } /* Build a tree from the subtasks. */ foreach (array_keys($dict) as $key) { $task = $this->tasks->get($dict[$key]->parent_id); if ($task) { $task->add($dict[$key]); } elseif (isset($dict[$dict[$key]->parent_id])) { $dict[$dict[$key]->parent_id]->add($dict[$key]); } else { $this->tasks->add($dict[$key]); } } }