protected function _search($page, $perpage)
{
global $injector, $prefs;
if (!empty($this->_due)) {
$parser = Horde_Date_Parser::factory(array('locale' => $GLOBALS['prefs']->getValue('language')));
$date = $parser->parse($this->_due[1]);
$date->mday += $this->_due[0];
$date = $date->timestamp();
} else {
$date = false;
}
// Get the full, sorted task list.
$tasks = Nag::listTasks(array('tasklists' => $this->_tasklists, 'completed' => $this->_completed, 'include_history' => false));
if (!empty($this->_search)) {
$pattern = '/' . preg_quote($this->_search, '/') . '/i';
}
$search_results = new Nag_Task();
if (!empty($this->_tags)) {
$tagged_tasks = $injector->getInstance('Nag_Tagger')->search($this->_tags, array('list' => $GLOBALS['display_tasklists']));
}
$tasks->reset();
while ($task = $tasks->each()) {
// Need to empty the children since they might not be in the results
$task = clone $task;
$task->orphan();
if (!empty($date)) {
if (empty($task->due) || $task->due > $date) {
continue;
}
}
// If we have a search string and it doesn't match name|desc continue
if (!empty($this->_search) && !($this->_mask & self::MASK_NAME && preg_match($pattern, $task->name)) && !($this->_mask & self::MASK_DESC && preg_match($pattern, $task->desc))) {
continue;
}
// No tags to search? Add it to results. Otherwise, make sure it
// has the requested tags.
if (empty($this->_tags) || in_array($task->uid, $tagged_tasks)) {
$search_results->add($task);
}
}
// Now try to maintain parent/child relationships when they are both
// in the result set.
$search_results->reset();
$search_results_copy = clone $search_results;
$processed_results = new Nag_Task();
while ($result = $search_results_copy->each()) {
if ($result->parent_id && ($parent_task = $search_results->hasTask($result->parent_id))) {
$parent_task->add($result, true);
$processed_results->add($parent_task, true);
} else {
$result->parent_id = '';
$processed_results->add($result);
}
}
// Now that we have filtered results, load all tags at once.
$processed_results->loadTags();
$processed_results->process();
return $processed_results;
}