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