r1370: - Add $show[update] for the mass update option (coming soon)
[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 'list_head',
27 'pagenav_bit',
28 'pagenav'
29 );
30
31 define('SVN', '$Id$');
32
33 $focus['search'] = 'focus';
34
35 require_once('./global.php');
36 require_once('./includes/functions_product.php');
37 require_once('./includes/class_sort.php');
38
39 if (!can_perform('cansearch'))
40 {
41 $message->error_permission();
42 }
43
44 define('MODE_ANY', ($bugsys->in['mode'] == 1));
45 define('MODE_ALL', ($bugsys->in['mode'] == 2));
46 define('MODE_RAW', ($bugsys->in['mode'] == 3));
47
48 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
49 define('SEARCH_WORD_MIN', $var['Value']);
50
51 $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = 0 AND dateline < " . (TIMENOW - 3600));
52
53 $show['search'] = true;
54
55 // ###################################################################
56
57 if (empty($_REQUEST['do']))
58 {
59 $_REQUEST['do'] = 'search';
60 }
61
62 // ###################################################################
63
64 if ($_REQUEST['do'] == 'process')
65 {
66 // -------------------------------------------------------------------
67 // handle keywords
68 if ($bugsys->in['summary'])
69 {
70 $keywords = preg_split('#\s+#', $bugsys->in['summary']);
71
72 // TODO - need to have some str to bool conversions
73
74 foreach ($keywords AS $word)
75 {
76 if (strlen($word) < SEARCH_WORD_MIN)
77 {
78 continue;
79 }
80
81 if (MODE_ALL)
82 {
83 $querybuild['text'] .= " +$word";
84 }
85 else
86 {
87 $querybuild['text'] .= " $word";
88 }
89
90 if (!preg_match('#-(.+?)#', trim($word)))
91 {
92 $hilight .= " $word";
93 }
94 }
95
96 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
97 $hilight = trim($hilight);
98 $hilight = preg_replace('#\s#', '+', $hilight);
99
100 $temp = trim($querybuild['text']);
101
102 if (MODE_ALL OR MODE_RAW)
103 {
104 $bool_flag = ' IN BOOLEAN MODE';
105 }
106
107 $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)";
108 }
109
110 // -------------------------------------------------------------------
111 // reporter
112 if ($bugsys->in['reporter'])
113 {
114 // force email or name?? make a distinction?
115 // more elegant way to do this? probably
116 $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')) . "%'");
117 if ($user['userid'])
118 {
119 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
120 }
121 }
122
123 // -------------------------------------------------------------------
124 // product/component/version stuff
125 if (is_array($bugsys->in['product']))
126 {
127 foreach ($bugsys->in['product'] AS $prod)
128 {
129 $product = explode(',', $prod);
130 $product = $bugsys->clean($product, TYPE_UINT);
131 $products[] = $product[0];
132 $components[] = $product[1];
133 $versions[] = $product[2];
134 }
135 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
136 }
137
138 // -------------------------------------------------------------------
139 // severity, priority, status, resolution, assignedto
140
141 // severity
142 if ($bugsys->in['severity'])
143 {
144 $bugsys->input_clean('severity', TYPE_UINT);
145 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in['severity']) . ")";
146 }
147
148 // priority
149 if ($bugsys->in['priority'])
150 {
151 $bugsys->input_clean('priority', TYPE_UINT);
152 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in['priority']) . ")";
153 }
154
155 // status
156 if ($bugsys->in['status'])
157 {
158 $bugsys->input_clean('status', TYPE_UINT);
159 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in['status']) . ")";
160 }
161
162 // resolution
163 if ($bugsys->in['resolution'])
164 {
165 $bugsys->input_clean('resolution', TYPE_UINT);
166 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in['resolution']) . ")";
167 }
168
169 // assignment
170 if ($bugsys->in['assignedto'])
171 {
172 $bugsys->input_clean('assignedto', TYPE_UINT);
173 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in['assignedto']) . ")";
174 }
175
176 // -------------------------------------------------------------------
177 // date
178 if ($bugsys->in['date'])
179 {
180 // now - (seconds/day * number of days)
181 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT) * 3600);
182 $querybuild['date'] = "AND bug.dateline >= $dateline";
183 }
184
185 // -------------------------------------------------------------------
186 // favorites
187 $bugsys->input_clean('favorite', TYPE_INT);
188 if ($bugsys->in['favorite'] != 0 AND $bugsys->userinfo['userid'])
189 {
190 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favorite WHERE userid = " . $bugsys->userinfo['userid']);
191 while ($favorite = $db->fetch_array($favorites))
192 {
193 $ids[] = $favorite['bugid'];
194 }
195 $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in['favorite'] > 0 ? "IN" : "NOT IN") . " (" . implode(', ', $ids) . ")";
196 }
197
198 // -------------------------------------------------------------------
199 // sort by
200 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
201 $orderby = array('ASC', 'DESC');
202 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
203 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
204 {
205 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
206 }
207 else if ($bugsys->in['sortby'] == 'relevance')
208 {
209 $sortclause = '';
210 }
211 else
212 {
213 $sortclause = '';
214 }
215
216 // -------------------------------------------------------------------
217 // custom fields
218 $fields_fetch = $bugsys->db->query("
219 SELECT bugfield.*
220 FROM " . TABLE_PREFIX . "bugfield AS bugfield
221 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
222 ON (bugfield.fieldid = permission.fieldid)
223 WHERE permission.mask <> 0
224 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
225 AND bugfield.cansearch = 1"
226 );
227 while ($field = $bugsys->db->fetch_array($fields_fetch))
228 {
229 if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"])))
230 {
231 if ($field['type'] == 'input_checkbox' AND $bugsys->input_clean("field$field[fieldid]", TYPE_INT) != 0)
232 {
233 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] > 0) ? 1 : 0);
234 }
235 else if ($field['type'] == 'input_text')
236 {
237 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'";
238 }
239 else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1)
240 {
241 $temp = unserialize($field['selects']);
242 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'";
243 }
244 }
245 }
246
247 // -------------------------------------------------------------------
248 // have to search something
249 if (sizeof($querybuild) < 1)
250 {
251 $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));
252 }
253
254 // -------------------------------------------------------------------
255 // do the search
256 $query = "
257 SELECT bug.*, comment.commentid
258 FROM " . TABLE_PREFIX . "bug AS bug
259 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
260 ON (bug.bugid = comment.bugid)
261 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
262 ON (bug.bugid = bugfieldvalue.bugid)
263 WHERE bug.bugid <> 0
264 AND bug.product IN (#<'ONBITS:VIEW'>#)
265 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'>#))" : "") . ")
266 " . implode("\n\t\t", $querybuild) . "
267 GROUP BY bug.bugid
268 $sortclause";
269
270 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewonhidden')), $query);
271
272 $search = $db->query($runquery);
273
274 $numrows = $db->num_rows($search);
275
276 if ($numrows < 1)
277 {
278 $message->error(_('No search results were returned that matched your criteria.'));
279 }
280
281 while ($result = $db->fetch_array($search))
282 {
283 $ids[] = $result['bugid'];
284 $results[] = $result;
285 }
286
287 if ($bugsys->userinfo['userid'])
288 {
289 $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid'] . " AND name IS NULL");
290 }
291
292 $db->query("
293 INSERT INTO " . TABLE_PREFIX . "search
294 (userid, dateline, query, ids, orderby, hilight, resultcount)
295 VALUES
296 (" . $bugsys->userinfo['userid'] . ",
297 " . TIMENOW . ", '" . $bugsys->escape($query) . "',
298 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
299 '" . $bugsys->escape($hilight) . "',
300 " . sizeof($results) . "
301 )"
302 );
303
304 $searchid = $db->insert_id();
305
306 $justprocess = true;
307 $search = array('ids' => implode(',', $ids), 'orderby' => $sortclause);
308
309 $_POST['do'] = 'results';
310 }
311
312 // ###################################################################
313
314 if ($_REQUEST['do'] == 'search')
315 {
316 if ($bugsys->in['searchid'] AND !$bugsys->in['new'])
317 {
318 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']))
319 {
320 $_POST['do'] = 'results';
321 $searchid = $cachedsearch['searchid'];
322 }
323 else
324 {
325 $newsearch = true;
326 }
327 }
328 else if ($bugsys->userinfo['userid'] AND !$bugsys->in['new'])
329 {
330 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . $bugsys->userinfo['userid']))
331 {
332 $_POST['do'] = 'results';
333 $searchid = $cachedsearch['searchid'];
334 }
335 else
336 {
337 $newsearch = true;
338 }
339 }
340 else
341 {
342 $newsearch = true;
343 }
344
345 if ($newsearch)
346 {
347 if (!is_array($bugsys->datastore['product']))
348 {
349 $message->error(_('No products are setup, therefore there can be no bugs and thus search cannot function.'));
350 }
351
352 if (!is_array($bugsys->datastore['version']))
353 {
354 $message->error(_('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
355 }
356
357 $productSelect = ConstructProductSelect();
358
359 // -------------------------------------------------------------------
360 // custom fields
361 $fields = construct_custom_fields(null, true, false, true);
362 $i = 0;
363 foreach ($fields AS $field)
364 {
365 if ($i % 2 == 0)
366 {
367 $customfields['left'] .= $field;
368 }
369 else
370 {
371 $customfields['right'] .= $field;
372 }
373 $i++;
374 }
375
376 // -------------------------------------------------------------------
377 // built-in fields
378 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
379 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
380 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
381 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
382
383 $searches = '';
384 if ($bugsys->userinfo['userid'])
385 {
386 $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NOT NULL AND userid = " . $bugsys->userinfo['userid']);
387 while ($search = $db->fetch_array($searchesFetch))
388 {
389 $value = $search['searchid'];
390 $label = $search['name'];
391 eval('$searches .= "' . $template->fetch('selectoption') . '";');
392 }
393 }
394
395 $select['dev'] = '';
396 foreach ($bugsys->datastore['assignto'] AS $dev)
397 {
398 $value = $dev['userid'];
399 $label = construct_user_display($dev, false);
400 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
401 }
402
403 eval('$template->flush("' . $template->fetch('search') . '");');
404 }
405 }
406
407 // ###################################################################
408
409 if ($_REQUEST['do'] == 'export')
410 {
411 if (!$bugsys->in['searchid'] AND $bugsys->userinfo['userid'])
412 {
413 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . $bugsys->userinfo['userid']);
414 }
415 else if ($bugsys->in['searchid'])
416 {
417 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
418 }
419 else
420 {
421 $message->error(_('The search results are trying to export are invalid. Please start over <a href="search.php?new=1">here</a> and try again.'));
422 }
423
424 if (!$search)
425 {
426 $message->error(_('Your search has expired because it is older than one hour. Please start over <a href="search.php?new=1">here</a>.'));
427 }
428
429 $bugs = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby]");
430
431 $xml = '<?xml version="1.0" encoding="' . $language['charset'] . '"?>
432
433 <bugdarExport user="' . $bugsys->unsanitize(construct_user_display($bugsys->userinfo, false)) . '" date="' . $datef->format('r', TIMENOW) . '" searchid="' . $search['searchid'] . '">';
434
435 while ($bug = $db->fetch_array($bugs))
436 {
437 $xml .= "\n\t<bug>";
438
439 ProcessBugDataForDisplay(&$bug);
440
441 $xml .= "\n\t\t<id>" . $bug['bugid'] . "</id>";
442 $xml .= "\n\t\t<dateReported>" . $datef->format('r', $bug['dateline']) . "</dateReported>";
443 if ($bug['userid'])
444 {
445 $xml .= "\n\t\t<reporter>" . construct_user_display($db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = $bug[userid]"), false) . "</reporter>";
446 }
447 $xml .= "\n\t\t<summary>" . $bug['summary'] . "</summary>";
448 $xml .= "\n\t\t<product>" . $bug['product'] . "</product>";
449 if ($bug['component'])
450 {
451 $xml .= "\n\t\t<component>" . $bug['component'] . "</component>";
452 }
453 $xml .= "\n\t\t<version>" . $bug['version'] . "</version>";
454 $xml .= "\n\t\t<status>" . $bug['status'] . "</status>";
455 if ($bug['assignedto'])
456 {
457 $xml .= "\n\t\t<assignedTo>" . construct_user_display($bugsys->datastore['assignto']["$bug[assignedto]"], false) . "</assignedTo>";
458 }
459 $xml .= "\n\t\t<resolution>" . $bug['resolution'] . "</resolution>";
460 $xml .= "\n\t\t<severity>" . $bug['severity'] . "</severity>";
461 $xml .= "\n\t\t<priority>" . $bug['priority'] . "</priority>";
462
463 $xml .= "\n\t</bug>";
464 }
465
466 $xml .= "\n</bugdarExport>";
467
468 $funct->download_file($xml, _('bugdar-search-' . $search['searchid'] . '.xml'), true);
469 }
470
471 // ###################################################################
472
473 if ($_POST['do'] == 'dosave')
474 {
475 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
476 if (!$search)
477 {
478 $message->add_error(_('The search does not exist.'));
479 }
480 if ($search['name'])
481 {
482 $message->add_error(sprintf(_('This search has already been named "%1$s".'), $search['name']));
483 }
484 if (empty($bugsys->in['name']))
485 {
486 $message->add_error(_('The name cannot be empty.'));
487 }
488
489 if (!$message->items)
490 {
491 $db->query("UPDATE " . TABLE_PREFIX . "search SET name = '" . $bugsys->input_escape('name') . "' WHERE searchid = " . $bugsys->in['searchid']);
492 $message->redirect(_('Your search has been saved.'), 'search.php?searchid=' . $search['searchid']);
493 }
494 else
495 {
496 $message->error_list_process();
497 $_REQUEST['do'] = 'save';
498 $show['errors'] = true;
499 }
500 }
501
502 // ###################################################################
503
504 if ($_REQUEST['do'] == 'save')
505 {
506 if (!$bugsys->userinfo['userid'])
507 {
508 $message->error_permission();
509 }
510
511 $message-
512
513 eval('$template->flush("' . $template->fetch('search_save') . '");');
514 }
515
516 // ###################################################################
517
518 if ($_POST['do'] == 'results')
519 {
520 $show['cached'] = false;
521 if ($searchid AND !$justprocess)
522 {
523 $search = $cachedsearch;
524 if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
525 {
526 $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']));
527 while ($bug = $db->fetch_array($research))
528 {
529 $ids[] = $bug['bugid'];
530 $results[] = $bug;
531 }
532 $search['ids'] = implode(',', $ids);
533 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . ", resultcount = " . sizeof($results) . " WHERE searchid = " . $search['searchid']);
534 }
535 $show['cached'] = true;
536 $hilight = $search['hilight'];
537 }
538
539 LoadPaginationFramework();
540 $pagination->setTotal($search['resultcount']);
541 $pagination->splitPages();
542
543 $sort = new ListSorter('search');
544
545 $show['save'] = ($bugsys->userinfo['userid'] AND !$search['name']);
546 $show['update'] = can_perform('caneditother');
547
548 $bugs = '';
549 $search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
550 while ($bug = $db->fetch_array($search))
551 {
552 $funct->exec_swap_bg($stylevar['alt_color'], '');
553 ProcessBugDataForDisplay(&$bug, $funct->bgcolour);
554 $bugs .= $sort->constructRow($bug, "&amp;hilight=$hilight");
555 }
556
557 $columnHeads = $sort->constructColumnHeaders(false);
558 $show['pagenav'] = ($pagination->getPageCount() > 1);
559 $pagenav = $pagination->constructPageNav('search.php?searchid=' . $searchid);
560
561 eval('$template->flush("' . $template->fetch('search_results') . '");');
562 }
563
564 /*=====================================================================*\
565 || ###################################################################
566 || # $HeadURL$
567 || # $Id$
568 || ###################################################################
569 \*=====================================================================*/
570 ?>