3
* Copyright 2001-2013 Horde LLC (http://www.horde.org/)
5
* See the enclosed file LICENSE for license information (BSD). If you did not
6
* did not receive this file, see http://www.horde.org/licenses/bsdl.php.
8
* @author Michael J Rubinsky <mrubinsk@horde.org>
10
* @license http://www.horde.org/licenses/gpl GPL
14
* Nag_Search:: Interface for performing task searches.
16
* Copyright 2001-2013 Horde LLC (http://www.horde.org/)
18
* See the enclosed file LICENSE for license information (BSD). If you did not
19
* did not receive this file, see http://www.horde.org/licenses/bsdl.php.
21
* @author Michael J Rubinsky <mrubinsk@horde.org>
23
* @license http://www.horde.org/licenses/gpl GPL
26
class Nag_Search implements Serializable
51
* The completed/view value.
55
protected $_completed;
58
* The tasks lists to search.
62
protected $_tasklists;
81
* @param string $search The search string.
82
* @param integer $mask A bitmask to indicate the fields to search.
83
* @param array $options Additional options:
84
* - completed: (integer) Which tasks to include. A NAG::VIEW_* constant.
85
* DEFAULT: Nag::VIEW_INCOMPLETE
87
* - due: (array) An array describing the due date portion of the search.
88
* EXAMPLE: array('5', 'tomorrow') would be all tasks due within 5
90
* DEFAULT: No date filters.
92
* - tags: (array) An array of tags to filter on.
93
* - tasklists: (array) An array of tasklist ids to filter on.
94
* DEFAULT: The current display_tasklists value is used.
98
public function __construct($search, $mask, array $options = array())
100
$options = array_merge(
105
'tasklists' => $GLOBALS['display_tasklists']),
108
$this->_search = $search;
109
$this->_mask = $mask;
110
$this->_completed = $options['completed'];
111
$this->_due = $options['due'];
112
$this->_tags = $options['tags'];
113
$this->_tasklists = $options['tasklists'];
117
* Get a result slice.
119
* @param integer $page The page number
120
* @param integer $perpage The number of results per page.
122
* @return Nag_Task The result list.
124
public function getSlice($page = 0, $perpage = 0)
126
return $this->_search($page, $perpage);
132
* @param integer $page The page number
133
* @param integer $perpage The number of results per page.
137
protected function _search($page, $perpage)
139
global $injector, $prefs;
141
if (!empty($this->_due)) {
142
$parser = Horde_Date_Parser::factory(array('locale' => $GLOBALS['prefs']->getValue('language')));
143
$date = $parser->parse($this->_due[1]);
144
$date->mday += $this->_due[0];
145
$date = $date->timestamp();
150
// Get the full, sorted task list.
151
$tasks = Nag::listTasks(array(
152
'tasklists' => $this->_tasklists,
153
'completed' => $this->_completed,
154
'include_history' => false)
156
if (!empty($this->_search)) {
157
$pattern = '/' . preg_quote($this->_search, '/') . '/i';
159
$search_results = new Nag_Task();
160
if (!empty($this->_tags)) {
161
$tagged_tasks = $injector->getInstance('Nag_Tagger')->search(
163
array('list' => $GLOBALS['display_tasklists'])
167
while ($task = $tasks->each()) {
169
if (empty($task->due) || $task->due > $date) {
174
// If we have a search string and it doesn't match name|desc continue
175
if (!empty($this->_search) &&
176
!($this->_mask & self::MASK_NAME && preg_match($pattern, $task->name)) &&
177
!($this->_mask & self::MASK_DESC && preg_match($pattern, $task->desc))) {
182
// No tags to search? Add it to results. Otherwise, make sure it
183
// has the requested tags.
184
if (empty($this->_tags) || in_array($task->uid, $tagged_tasks)) {
185
$search_results->add($task);
189
// Now that we have filtered results, load all tags at once.
190
$search_results->loadTags();
192
return $search_results;
196
* Populate a Horde_Variables instance with the search values for this
199
* @param Horde_Variables $vars The Horde_Variables object.
201
public function getVars(Horde_Variables &$vars)
203
$vars->set('search_pattern', $this->_search);
204
$vars->set('search_tags', implode(',', $this->_tags));
205
$vars->set('search_completed', $this->_completed);
206
$vars->set('due_within', $this->_due[0]);
207
$vars->set('due_of', $this->_due[1]);
210
if ($this->_mask & self::MASK_NAME) {
211
$mask[] = 'search_name';
213
if ($this->_mask & self::MASK_DESC) {
214
$mask[] = 'search_desc';
217
$vars->set('search_in', $mask);
224
* @return array The unserialized data.
226
public function serialize()
228
return serialize(array(
229
'search' => $this->_search,
230
'mask' => $this->_mask,
231
'completed' => $this->_completed,
232
'due' => $this->_due,
233
'tags' => $this->_tags));
239
* @param string $data The serialized data.
241
public function unserialize($data)
243
$data = unserialize($data);
244
$this->_search = $data['search'];
245
$this->_mask = $data['mask'];
246
$this->_completed = $data['completed'];
247
$this->_due = $data['due'];
248
$this->_tags = $data['tags'];