r1325: Switch from our custom class_pagination.php to ISSO's Pagination framework
[bugdar.git] / search.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright 2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 $fetchtemplates = array(
23 'search',
24 'search_results',
25 'trackerhome_bits',
26 'pagenav_bit',
27 'pagenav'
28 );
29
30 define('SVN', '$Id$');
31
32 $focus['search'] = 'focus';
33
34 require_once('./global.php');
35 require_once('./includes/functions_product.php');
36
37 if (!can_perform('cansearch'))
38 {
39 $message->error_permission();
40 }
41
42 define('MODE_ANY', ($bugsys->in['mode'] == 1));
43 define('MODE_ALL', ($bugsys->in['mode'] == 2));
44 define('MODE_RAW', ($bugsys->in['mode'] == 3));
45
46 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
47 define('SEARCH_WORD_MIN', $var['Value']);
48
49 $show['search'] = true;
50
51 // ###################################################################
52
53 if (empty($_REQUEST['do']))
54 {
55 $_REQUEST['do'] = 'search';
56 }
57
58 // ###################################################################
59
60 if ($_REQUEST['do'] == 'process')
61 {
62 // -------------------------------------------------------------------
63 // handle keywords
64 if ($bugsys->in['summary'])
65 {
66 $keywords = preg_split('#\s+#', $bugsys->in['summary']);
67
68 // TODO - need to have some str to bool conversions
69
70 foreach ($keywords AS $word)
71 {
72 if (strlen($word) < SEARCH_WORD_MIN)
73 {
74 continue;
75 }
76
77 if (MODE_ALL)
78 {
79 $querybuild['text'] .= " +$word";
80 }
81 else
82 {
83 $querybuild['text'] .= " $word";
84 }
85
86 if (!preg_match('#-(.+?)#', trim($word)))
87 {
88 $hilight .= " $word";
89 }
90 }
91
92 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
93 $hilight = trim($hilight);
94 $hilight = preg_replace('#\s#', '+', $hilight);
95
96 $temp = trim($querybuild['text']);
97
98 if (MODE_ALL OR MODE_RAW)
99 {
100 $bool_flag = ' IN BOOLEAN MODE';
101 }
102
103 $querybuild['text'] = "AND\n\t\t\t(\n\t\t\t\tMATCH (bug.summary) AGAINST ('$temp'$bool_flag)\n\t\t\t\tOR MATCH (comment.comment) AGAINST ('$temp'$bool_flag)\n\t\t\t)";
104 }
105
106 // -------------------------------------------------------------------
107 // reporter
108 if ($bugsys->in['reporter'])
109 {
110 // force email or name?? make a distinction?
111 // more elegant way to do this? probably
112 $user = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email LIKE '%" . str_replace('%', '\%', $bugsys->input_escape('reporter')) . "' OR displayname LIKE '%" . str_replace('%', '\%', $bugsys->input_escape('reporter')) . "%'");
113 if ($user['userid'])
114 {
115 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
116 }
117 }
118
119 // -------------------------------------------------------------------
120 // product/component/version stuff
121 if (is_array($bugsys->in['pcv_select']))
122 {
123 foreach ($bugsys->in['pcv_select'] AS $pcv)
124 {
125 $pcv = parse_pcv_select($pcv, true);
126 $products[] = $pcv['product'];
127 $components[] = $pcv['component'];
128 $versions[] = $pcv['version'];
129 }
130 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
131 }
132
133 // -------------------------------------------------------------------
134 // severity, priority, status, resolution, assignedto
135
136 // severity
137 if ($bugsys->in['severity'])
138 {
139 $bugsys->input_clean('severity', TYPE_UINT);
140 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in['severity']) . ")";
141 }
142
143 // priority
144 if ($bugsys->in['priority'])
145 {
146 $bugsys->input_clean('priority', TYPE_UINT);
147 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in['priority']) . ")";
148 }
149
150 // status
151 if ($bugsys->in['status'])
152 {
153 $bugsys->input_clean('status', TYPE_UINT);
154 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in['status']) . ")";
155 }
156
157 // resolution
158 if ($bugsys->in['resolution'])
159 {
160 $bugsys->input_clean('resolution', TYPE_UINT);
161 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in['resolution']) . ")";
162 }
163
164 // assignment
165 if ($bugsys->in['assignedto'])
166 {
167 $bugsys->input_clean('assignedto', TYPE_UINT);
168 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in['assignedto']) . ")";
169 }
170
171 // -------------------------------------------------------------------
172 // date
173 if ($bugsys->in['date'])
174 {
175 // now - (seconds/day * number of days)
176 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT) * 3600);
177 $querybuild['date'] = "AND bug.dateline >= $dateline";
178 }
179
180 // -------------------------------------------------------------------
181 // favoritess
182 if (($bugsys->in['favorite'] == 1 OR $bugsys->in['favorite'] == 0) AND $bugsys->userinfo['userid'])
183 {
184 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favorite WHERE userid = " . $bugsys->userinfo['userid']);
185 while ($favorite = $db->fetch_array($favorites))
186 {
187 $ids[] = $favorite['bugid'];
188 }
189 $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in['favorite'] == 0 ? "NOT IN" : "IN") . " (" . implode(', ', $ids) . ")";
190 }
191
192 // -------------------------------------------------------------------
193 // sort by
194 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
195 $orderby = array('ASC', 'DESC');
196 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
197 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
198 {
199 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
200 }
201 else if ($bugsys->in['sortby'] == 'relevance')
202 {
203 $sortclause = '';
204 }
205 else
206 {
207 $sortclause = '';
208 }
209
210 // -------------------------------------------------------------------
211 // custom fields
212 $fields_fetch = $bugsys->db->query("
213 SELECT bugfield.*
214 FROM " . TABLE_PREFIX . "bugfield AS bugfield
215 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
216 ON (bugfield.fieldid = permission.fieldid)
217 WHERE permission.mask <> 0
218 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
219 AND bugfield.cansearch = 1"
220 );
221 while ($field = $bugsys->db->fetch_array($fields_fetch))
222 {
223 if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"])))
224 {
225 if ($field['type'] == 'input_checkbox')
226 {
227 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] == 1) ? 0 : 1);
228 }
229 else if ($field['type'] == 'input_text')
230 {
231 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'";
232 }
233 else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1)
234 {
235 $temp = unserialize($field['selects']);
236 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'";
237 }
238 }
239 }
240
241 // -------------------------------------------------------------------
242 // have to search something
243 if (sizeof($querybuild) < 1)
244 {
245 $message->error(sprintf(_('You have to enter some criteria to search for. Note that words less than %1$d characters are ignored by the search engine (and some other very common words, too).'), SEARCH_WORD_MIN));
246 }
247
248 // -------------------------------------------------------------------
249 // do the search
250 $query = "
251 SELECT bug.*, comment.commentid
252 FROM " . TABLE_PREFIX . "bug AS bug
253 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
254 ON (bug.bugid = comment.bugid)
255 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
256 ON (bug.bugid = bugfieldvalue.bugid)
257 WHERE bug.bugid <> 0
258 AND bug.product IN (#<'ONBITS:VIEW'>#)
259 AND (!bug.hidden OR (bug.hidden AND bug.product IN (#<'ONBITS:HIDDEN'>#))" . (can_perform('canviewownhidden') ? " OR (bug.hidden AND bug.userid = " . $bugsys->userinfo['userid'] . " AND bug.product IN (#<'ONBITS:OWNHIDDEN'>#))" : "") . ")
260 " . implode("\n\t\t", $querybuild) . "
261 GROUP BY bug.bugid
262 $sortclause";
263
264 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewonhidden')), $query);
265
266 $search = $db->query($runquery);
267
268 $numrows = $db->num_rows($search);
269
270 if ($numrows < 1)
271 {
272 $message->error(_('No search results were returned that matched your criteria.'));
273 }
274
275 while ($result = $db->fetch_array($search))
276 {
277 $ids[] = $result['bugid'];
278 $results[] = $result;
279 }
280
281 if ($bugsys->userinfo['userid'])
282 {
283 $db->query("
284 REPLACE INTO " . TABLE_PREFIX . "search
285 (userid, dateline, query, ids, orderby, hilight, resultcount)
286 VALUES
287 (" . $bugsys->userinfo['userid'] . ",
288 " . TIMENOW . ", '" . $bugsys->escape($query) . "',
289 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
290 '" . $bugsys->escape($hilight) . "',
291 " . sizeof($results) . "
292 )"
293 );
294 }
295
296 $justprocess = true;
297 $search = array('ids' => implode(',', $ids), 'orderby' => $sortclause);
298
299 $_REQUEST['do'] = 'results';
300 }
301
302 // ###################################################################
303
304 if ($_REQUEST['do'] == 'search')
305 {
306 if ($bugsys->userinfo['userid'] AND !$bugsys->in['new'])
307 {
308 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid']))
309 {
310 $_REQUEST['do'] = 'results';
311 }
312 else
313 {
314 $newsearch = true;
315 }
316 }
317 else
318 {
319 $newsearch = true;
320 }
321
322 if ($newsearch)
323 {
324 if (!is_array($bugsys->datastore['product']))
325 {
326 $message->error(_('No products are setup, therefore there can be no bugs and thus search cannot function.'));
327 }
328
329 if (!is_array($bugsys->datastore['version']))
330 {
331 $message->error(_('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
332 }
333
334 $pcv_select = construct_pcv_select();
335
336 // -------------------------------------------------------------------
337 // custom fields
338 $fields = construct_custom_fields(null, true);
339 $i = 0;
340 foreach ($fields AS $field)
341 {
342 if ($i % 2 == 0)
343 {
344 $customfields['left'] .= $field;
345 }
346 else
347 {
348 $customfields['right'] .= $field;
349 }
350 $i++;
351 }
352
353 // -------------------------------------------------------------------
354 // built-in fields
355 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
356 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
357 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
358 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
359
360 $select['dev'] = '';
361 foreach ($bugsys->datastore['assignto'] AS $dev)
362 {
363 $value = $dev['userid'];
364 $label = construct_user_display($dev, false);
365 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
366 }
367
368 eval('$template->flush("' . $template->fetch('search') . '");');
369 }
370 }
371
372 // ###################################################################
373
374 if ($_REQUEST['do'] == 'results')
375 {
376 $show['cached'] = false;
377 if ($bugsys->userinfo['userid'] AND !$justprocess)
378 {
379 $search = $cachedsearch;
380 if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
381 {
382 $research = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewownhidden')), $search['query']));
383 while ($bug = $db->fetch_array($research))
384 {
385 $ids[] = $bug['bugid'];
386 $results[] = $bug;
387 }
388 $search['ids'] = implode(',', $ids);
389 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . ", resultcount = " . sizeof($results) . " WHERE userid = " . $bugsys->userinfo['userid']);
390 }
391 $show['cached'] = true;
392 $hilight = $search['hilight'];
393 }
394
395 LoadPaginationFramework();
396 $pagination->setTotal($search['resultcount']);
397 $pagination->splitPages();
398
399 $search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
400 while ($bug = $db->fetch_array($search))
401 {
402 $funct->exec_swap_bg($stylevar['alt_color'], '');
403 ProcessBugDataForDisplay(&$bug, $funct->bgcolour);
404 $bug['urladd'] = "&amp;hilight=$hilight";
405 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
406 }
407
408 $show['pagenav'] = ($pagination->getPageCount() > 1);
409 $pagenav = $pagination->constructPageNav('search.php');
410
411 eval('$template->flush("' . $template->fetch('search_results') . '");');
412 }
413
414 /*=====================================================================*\
415 || ###################################################################
416 || # $HeadURL$
417 || # $Id$
418 || ###################################################################
419 \*=====================================================================*/
420 ?>