~ubuntu-branches/ubuntu/vivid/php-horde-nag/vivid

« back to all changes in this revision

Viewing changes to nag-4.1.2/lib/Search.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-10-29 22:01:43 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20131029220143-k16crjhccwr56jxq
Tags: 4.1.3-1
New upstream version 4.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Copyright 2001-2013 Horde LLC (http://www.horde.org/)
4
 
 *
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.
7
 
 *
8
 
 * @author  Michael J Rubinsky <mrubinsk@horde.org>
9
 
 * @category Horde
10
 
 * @license  http://www.horde.org/licenses/gpl GPL
11
 
 * @package Nag
12
 
 */
13
 
/**
14
 
 * Nag_Search:: Interface for performing task searches.
15
 
 *
16
 
 * Copyright 2001-2013 Horde LLC (http://www.horde.org/)
17
 
 *
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.
20
 
 *
21
 
 * @author  Michael J Rubinsky <mrubinsk@horde.org>
22
 
 * @category Horde
23
 
 * @license  http://www.horde.org/licenses/gpl GPL
24
 
 * @package Nag
25
 
 */
26
 
class Nag_Search implements Serializable
27
 
{
28
 
    /**
29
 
     * Search bit masks
30
 
     */
31
 
    const MASK_NAME      = 1;
32
 
    const MASK_DESC      = 2;
33
 
    const MASK_TAGS      = 4;
34
 
    const MASK_ALL       = 7;
35
 
 
36
 
    /**
37
 
     * Search criteria
38
 
     *
39
 
     * @var array
40
 
     */
41
 
    protected $_search;
42
 
 
43
 
    /**
44
 
     * The search mask
45
 
     *
46
 
     * @var integer
47
 
     */
48
 
    protected $_mask;
49
 
 
50
 
    /**
51
 
     * The completed/view value.
52
 
     *
53
 
     * @var integer
54
 
     */
55
 
    protected $_completed;
56
 
 
57
 
    /**
58
 
     * The tasks lists to search.
59
 
     *
60
 
     * @var array
61
 
     */
62
 
    protected $_tasklists;
63
 
 
64
 
    /**
65
 
     * Duedate criteria
66
 
     *
67
 
     * @var array
68
 
     */
69
 
    protected $_due;
70
 
 
71
 
    /**
72
 
     * Tag search criteria
73
 
     *
74
 
     * @var array
75
 
     */
76
 
    protected $_tags;
77
 
 
78
 
    /**
79
 
     * Constructor
80
 
     *
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
86
 
     *
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
89
 
     *          days of tomorrow.
90
 
     *          DEFAULT: No date filters.
91
 
     *
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.
95
 
     *
96
 
     * @return Nag_Search
97
 
     */
98
 
    public function __construct($search, $mask, array $options = array())
99
 
    {
100
 
        $options = array_merge(
101
 
            array(
102
 
                'completed' => 0,
103
 
                'due' => array(),
104
 
                'tags' => array(),
105
 
                'tasklists' => $GLOBALS['display_tasklists']),
106
 
            $options);
107
 
 
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'];
114
 
    }
115
 
 
116
 
    /**
117
 
     * Get a result slice.
118
 
     *
119
 
     * @param integer $page     The page number
120
 
     * @param integer $perpage  The number of results per page.
121
 
     *
122
 
     * @return Nag_Task  The result list.
123
 
     */
124
 
    public function getSlice($page = 0, $perpage = 0)
125
 
    {
126
 
        return $this->_search($page, $perpage);
127
 
    }
128
 
 
129
 
    /**
130
 
     * Perform the search
131
 
     *
132
 
     * @param integer $page     The page number
133
 
     * @param integer $perpage  The number of results per page.
134
 
     *
135
 
     * @return Nag_Task
136
 
     */
137
 
    protected function _search($page, $perpage)
138
 
    {
139
 
        global $injector, $prefs;
140
 
 
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();
146
 
        } else {
147
 
            $date = false;
148
 
        }
149
 
 
150
 
        // Get the full, sorted task list.
151
 
        $tasks = Nag::listTasks(array(
152
 
            'tasklists' => $this->_tasklists,
153
 
            'completed' => $this->_completed,
154
 
            'include_history' => false)
155
 
        );
156
 
        if (!empty($this->_search)) {
157
 
            $pattern = '/' . preg_quote($this->_search, '/') . '/i';
158
 
        }
159
 
        $search_results = new Nag_Task();
160
 
        if (!empty($this->_tags)) {
161
 
            $tagged_tasks = $injector->getInstance('Nag_Tagger')->search(
162
 
                $this->_tags,
163
 
                array('list' => $GLOBALS['display_tasklists'])
164
 
            );
165
 
        }
166
 
        $tasks->reset();
167
 
        while ($task = $tasks->each()) {
168
 
            if (!empty($date)) {
169
 
                if (empty($task->due) || $task->due > $date) {
170
 
                    continue;
171
 
                }
172
 
            }
173
 
 
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))) {
178
 
 
179
 
                continue;
180
 
            }
181
 
 
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);
186
 
            }
187
 
        }
188
 
 
189
 
        // Now that we have filtered results, load all tags at once.
190
 
        $search_results->loadTags();
191
 
 
192
 
        return $search_results;
193
 
    }
194
 
 
195
 
    /**
196
 
     * Populate a Horde_Variables instance with the search values for this
197
 
     * search.
198
 
     *
199
 
     * @param Horde_Variables $vars  The Horde_Variables object.
200
 
     */
201
 
    public function getVars(Horde_Variables &$vars)
202
 
    {
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]);
208
 
 
209
 
        $mask = array();
210
 
        if ($this->_mask & self::MASK_NAME) {
211
 
            $mask[] = 'search_name';
212
 
        }
213
 
        if ($this->_mask & self::MASK_DESC) {
214
 
            $mask[] = 'search_desc';
215
 
        }
216
 
        if (!empty($mask)) {
217
 
            $vars->set('search_in', $mask);
218
 
        }
219
 
    }
220
 
 
221
 
    /**
222
 
     * Serialize method
223
 
     *
224
 
     * @return array  The unserialized data.
225
 
     */
226
 
    public function serialize()
227
 
    {
228
 
        return serialize(array(
229
 
            'search' => $this->_search,
230
 
            'mask' => $this->_mask,
231
 
            'completed' => $this->_completed,
232
 
            'due' => $this->_due,
233
 
            'tags' => $this->_tags));
234
 
    }
235
 
 
236
 
    /**
237
 
     * Unserialize method
238
 
     *
239
 
     * @param string $data  The serialized data.
240
 
     */
241
 
    public function unserialize($data)
242
 
    {
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'];
249
 
    }
250
 
 
251
 
}