/**
* Retrieves the current user's task list from storage.
*
* This function will also sort the resulting list, if requested.
*
* @param arary $options Options array:
* - altsortby: (string) The secondary sort field. Same values as sortdir.
* DEFAULT: altsortby pref is used.
* - completed: (integer) Which task to retrieve. A Nag::VIEW_* constant.
* DEFAULT: show_completed pref is used.
* - external: (boolean) Whether to include tasks from other applications
* too.
* DEFAULT: true.
* - include_history: (boolean) Autoload created/modified data from
* Horde_History.
* DEFAULT: true (Automatically load history data).
* - include_tags: (boolean) Autoload all tags.
* DEFAULT: false (Tags are lazy loaded as needed.)
* - sortby: (string) A Nag::SORT_* constant for the field to sort by.
* DEFAULT: sortby pref is used.
* - sortdir: (string) Direction of sort. NAG::SORT_ASCEND or
* NAG::SORT_DESCEND.
* DEFAULT: sortdir pref is used.
* - tasklists: (array) An array of tasklists to include.
* DEFAULT: Use $GLOBALS['display_tasklists'];
*
* @return Nag_Task A list of the requested tasks.
*/
public static function listTasks(array $options = array())
{
global $prefs, $registry;
// Prevent null tasklists value from obscuring the default value.
if (array_key_exists('tasklists', $options) && empty($options['tasklists'])) {
unset($options['tasklists']);
}
$options = array_merge(array('sortby' => $prefs->getValue('sortby'), 'sortdir' => $prefs->getValue('sortdir'), 'altsortby' => $prefs->getValue('altsortby'), 'tasklists' => $GLOBALS['display_tasklists'], 'completed' => $prefs->getValue('show_completed'), 'include_tags' => false, 'external' => true, 'include_history' => true), $options);
if (!is_array($options['tasklists'])) {
$options['tasklists'] = array($options['tasklists']);
}
$tasks = new Nag_Task();
foreach ($options['tasklists'] as $tasklist) {
$storage = $GLOBALS['injector']->getInstance('Nag_Factory_Driver')->create($tasklist);
// Retrieve the tasklist from storage.
$storage->retrieve($options['completed'], $options['include_history']);
$tasks->mergeChildren($storage->tasks->children);
}
// Process all tasks.
$tasks->process();
if ($options['external'] && ($apps = @unserialize($prefs->getValue('show_external'))) && is_array($apps)) {
foreach ($apps as $app) {
// We look for registered apis that support listAs(taskHash).
if ($app == 'nag' || !$registry->hasMethod('getListTypes', $app)) {
continue;
}
try {
$types = $registry->callByPackage($app, 'getListTypes');
} catch (Horde_Exception $e) {
continue;
}
if (empty($types['taskHash'])) {
continue;
}
try {
$newtasks = $registry->callByPackage($app, 'listAs', array('taskHash'));
foreach ($newtasks as $task) {
if (!isset($task['priority'])) {
$task['priority'] = 3;
}
$task['tasklist_id'] = '**EXTERNAL**';
$task['tasklist_name'] = $registry->get('name', $app);
$task = new Nag_Task(null, $task);
if ($options['completed'] == Nag::VIEW_INCOMPLETE && ($task->completed || $task->start > $_SERVER['REQUEST_TIME']) || $options['completed'] == Nag::VIEW_COMPLETE && !$task->completed || $options['completed'] == Nag::VIEW_FUTURE && ($task->completed || !$task->start || $task->start < $_SERVER['REQUEST_TIME']) || $options['completed'] == Nag::VIEW_FUTURE_INCOMPLETE && $task->completed) {
continue;
}
$tasks->add($task);
}
} catch (Horde_Exception $e) {
Horde::log($e);
}
}
}
// Sort the array.
$tasks->sort($options['sortby'], $options['sortdir'], $options['altsortby']);
// Preload tags if requested.
if ($options['include_tags']) {
$tasks->loadTags();
}
return $tasks;
}