r660: Fixed PCV searching
[bugdar.git] / search.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # BugStrike [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 $fetchtemplates = array(
14 'search',
15 'search_results',
16 'trackerhome_bits'
17 );
18
19 define('SVN', '$Id$');
20
21 $focus['search'] = 'focus';
22
23 require_once('./global.php');
24 require_once('./includes/functions_product.php');
25
26 if (!can_perform('cansearch'))
27 {
28 $message->error_permission();
29 }
30
31 define('MODE', intval($bugsys->in['mode']));
32 define('MODE_ANY', (MODE == 1));
33 define('MODE_ALL', (MODE == 2));
34 define('MODE_RAW', (MODE == 3));
35
36 define('SEARCH_WORD_MIN', 3);
37
38 $show['search'] = true;
39
40 // ###################################################################
41
42 if (empty($_REQUEST['do']))
43 {
44 $_REQUEST['do'] = 'search';
45 }
46
47 // ###################################################################
48
49 if ($_REQUEST['do'] == 'process')
50 {
51 // -------------------------------------------------------------------
52 // handle keywords
53 if ($bugsys->in['summary'])
54 {
55 $keywords = preg_split('#\s+#', $bugsys->in['summary']);
56
57 // #*# need to have some str to bool conversions
58
59 foreach ($keywords AS $word)
60 {
61 if (strlen($word) < SEARCH_WORD_MIN)
62 {
63 continue;
64 }
65
66 if (MODE_ALL)
67 {
68 $querybuild['text'] .= " +$word";
69 }
70 else
71 {
72 $querybuild['text'] .= " $word";
73 }
74
75 if (!preg_match('#-(.+?)#', trim($word)))
76 {
77 $hilight .= " $word";
78 }
79 }
80
81 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
82 $hilight = trim($hilight);
83 $hilight = preg_replace('#\s#', '+', $hilight);
84
85 $temp = trim($querybuild['text']);
86
87 if (MODE_ALL OR MODE_RAW)
88 {
89 $bool_flag = ' IN BOOLEAN MODE';
90 }
91
92 $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)";
93 }
94
95 // -------------------------------------------------------------------
96 // reporter
97 if ($bugsys->in['reporter'])
98 {
99 // force email or name?? make a distinction?
100 // more elegant way to do this? probably
101 $user = $db->query_first("SELECT * FROM user WHERE email LIKE '%" . str_replace('%', '\%', $bugsys->in['reporter']) . "' OR displayname LIKE '%" . str_replace('%', '\%', $bugsys->in['reporter']) . "%'");
102 if ($user['userid'])
103 {
104 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
105 }
106 }
107
108 // -------------------------------------------------------------------
109 // product/component/version stuff
110 if (is_array($bugsys->in['pcv_select']))
111 {
112 foreach ($bugsys->in['pcv_select'] AS $pcv)
113 {
114 $pcv = parse_pcv_select($pcv, true);
115 $products[] = $pcv['product'];
116 $components[] = $pcv['component'];
117 $versions[] = $pcv['version'];
118 }
119 $querybuild['pcv'] = "AND bug.productid IN (" . implode(',', $products) . ") AND bug.componentid IN (" . implode(',', $components) . ") AND bug.versionid IN (" . implode(',', $versions) . ")";
120 }
121 else
122 {
123 $pcv = parse_pcv_select($bugsys->in['pcv_select']);
124 if (is_array($pcv))
125 {
126 $querybuild['pcv'] = "AND bug.productid = $pcv[product] AND bug.componentid = $pcv[component] AND bug.versionid = $pcv[version]";
127 }
128 }
129
130 // -------------------------------------------------------------------
131 // severity, priority, status, resolution, assignedto
132
133 // severity
134 if ($bugsys->in['severity'])
135 {
136 $querybuild['severity'] = "AND bug.severity = " . intval($bugsys->in['severity']);
137 }
138
139 // priority
140 if ($bugsys->in['priority'])
141 {
142 $querybuild['priority'] = "AND bug.priority = " . intval($bugsys->in['priority']);
143 }
144
145 // status
146 if ($bugsys->in['status'])
147 {
148 $querybuild['status'] = "AND bug.status = " . intval($bugsys->in['status']);
149 }
150
151 // resolution
152 if ($bugsys->in['resolution'])
153 {
154 $querybuild['resolution'] = "AND bug.resolution = " . intval($bugsys->in['resolution']);
155 }
156
157 // assignment
158 if ($bugsys->in['assignedto'])
159 {
160 $querybuild['assignedto'] = "AND bug.assignedto = " . intval($bugsys->in['assignedto']);
161 }
162
163 // -------------------------------------------------------------------
164 // date
165 if ($bugsys->in['date'])
166 {
167 // now - (seconds/day * number of days)
168 $dateline = time() - (intval($bugsys->in['date']) * 3600);
169 $querybuild['date'] = "AND bug.dateline >= $dateline";
170 }
171
172 // -------------------------------------------------------------------
173 // favourites
174 if ($bugsys->in['favourite'] AND $bugsys->userinfo['userid'])
175 {
176 $favourites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $bugsys->userinfo['userid']);
177 while ($favourite = $db->fetch_array($favourites))
178 {
179 $ids[] = $favourite['bugid'];
180 }
181 $querybuild['favourites'] = "AND bug.bugid IN (" . implode(', ', $ids) . ")";
182 }
183
184 // -------------------------------------------------------------------
185 // sort by
186 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
187 $orderby = array('ASC', 'DESC');
188 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
189 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
190 {
191 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
192 }
193 else if ($bugsys->in['sortby'] == 'relevance')
194 {
195 $sortclause = '';
196 }
197 else
198 {
199 $sortclause = '';
200 }
201
202 // -------------------------------------------------------------------
203 // custom fields
204 $fields_fetch = $bugsys->db->query("
205 SELECT bugfield.*
206 FROM " . TABLE_PREFIX . "bugfield AS bugfield
207 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
208 ON (bugfield.fieldid = permission.fieldid)
209 WHERE permission.mask <> 0
210 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
211 AND bugfield.cansearch = 1"
212 );
213 while ($field = $bugsys->db->fetch_array($fields_fetch))
214 {
215 if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"])))
216 {
217 if ($field['type'] == 'input_checkbox')
218 {
219 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] == 1) ? 0 : 1);
220 }
221 else if ($field['type'] == 'input_text')
222 {
223 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'";
224 }
225 else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1)
226 {
227 $temp = unserialize($field['selects']);
228 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'";
229 }
230 }
231 }
232
233 // -------------------------------------------------------------------
234 // have to search something
235 if (count($querybuild) < 1)
236 {
237 $message->error($lang->string('You have to enter some criteria to search for'));
238 }
239
240 // -------------------------------------------------------------------
241 // do the search
242 $query = "
243 SELECT bug.*, comment.commentid,
244 user1.displayname AS firstreport,
245 user2.displayname AS lastpost
246 FROM " . TABLE_PREFIX . "bug AS bug
247 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
248 ON (bug.bugid = comment.bugid)
249 LEFT JOIN " . TABLE_PREFIX . "user AS user1
250 ON (bug.userid = user1.userid)
251 LEFT JOIN " . TABLE_PREFIX . "user AS user2
252 ON (bug.lastpostby = user2.userid)
253 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
254 ON (bug.bugid = bugfieldvalue.bugid)
255 WHERE bug.bugid <> 0
256 AND bug.productid IN (#<'ONBITS:VIEW'>#)
257 AND (!bug.hidden OR (bug.hidden AND bug.productid IN (#<'ONBITS:HIDDEN'>#)))
258 " . implode("\n\t\t", $querybuild) . "
259 GROUP BY bug.bugid
260 $sortclause";
261
262 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $query);
263
264 $search = $db->query($runquery);
265
266 $numrows = $db->num_rows($search);
267
268 if ($numrows < 1)
269 {
270 $message->error($lang->string('No search results were returned that matched your criteria.'));
271 }
272
273 while ($result = $db->fetch_array($search))
274 {
275 $ids[] = $result['bugid'];
276 $results[] = $result;
277 }
278
279 if ($bugsys->userinfo['userid'])
280 {
281 $db->query("
282 REPLACE INTO " . TABLE_PREFIX . "search
283 (userid, dateline, query, ids, orderby, hilight)
284 VALUES
285 (" . $bugsys->userinfo['userid'] . ",
286 " . TIMENOW . ", '" . $bugsys->escape($query) . "',
287 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
288 '" . $bugsys->escape($hilight) . "'
289 )"
290 );
291 }
292
293 $justprocess = true;
294
295 $_REQUEST['do'] = 'results';
296 }
297
298 // ###################################################################
299
300 if ($_REQUEST['do'] == 'search')
301 {
302 if ($bugsys->userinfo['userid'] AND !$bugsys->in['new'])
303 {
304 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid']))
305 {
306 $_REQUEST['do'] = 'results';
307 }
308 else
309 {
310 $newsearch = true;
311 }
312 }
313 else
314 {
315 $newsearch = true;
316 }
317
318 if ($newsearch)
319 {
320 $pcv_select = construct_pcv_select();
321
322 // -------------------------------------------------------------------
323 // custom fields
324 $fields = construct_custom_fields($bug);
325 $show['customfields'] = ($fields ? true : false);
326
327 $bugsys->debug('fields modulo: ' . count($fields) % 2);
328 $i = 1;
329 foreach ($fields AS $field)
330 {
331 $bugsys->debug("i = $i");
332 if ($i == 1)
333 {
334 $customfields['right'] .= $field;
335 }
336 else if ($i == 2)
337 {
338 $customfields['left'] .= $field;
339 $i = 0;
340 }
341 $i++;
342 }
343
344 // -------------------------------------------------------------------
345 // built-in fields
346 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, true);
347 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, true);
348 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, true);
349 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, true);
350
351 $select['dev'] = '<option value="0" selected="selected"></option>';
352 foreach ($bugsys->datastore['assignto'] AS $dev)
353 {
354 $value = $dev['userid'];
355 $label = construct_user_display($dev, false);
356 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
357 }
358
359 eval('$template->flush("' . $template->fetch('search') . '");');
360 }
361 }
362
363 // ###################################################################
364
365 if ($_REQUEST['do'] == 'results')
366 {
367 $show['cached'] = false;
368 if ($bugsys->userinfo['userid'] AND !$justprocess)
369 {
370 $search = $cachedsearch;
371 if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
372 {
373 $search = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $search['query']));
374 while ($bug = $db->fetch_array($search))
375 {
376 $ids[] = $bug['bugid'];
377 $results[] = $bug;
378 }
379 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . " WHERE userid = " . $bugsys->userinfo['userid']);
380 }
381 else
382 {
383 $search = $db->query("
384 SELECT bug.*, user1.displayname AS firstreport,
385 user2.displayname AS lastpost
386 FROM " . TABLE_PREFIX . "bug AS bug
387 LEFT JOIN " . TABLE_PREFIX . "user AS user1
388 ON (bug.userid = user1.userid)
389 LEFT JOIN " . TABLE_PREFIX . "user AS user2
390 ON (bug.lastpostby = user2.userid)
391 WHERE bug.bugid IN ($search[ids])
392 $search[orderby]"
393 );
394 while ($bug = $db->fetch_array($search))
395 {
396 $results[] = $bug;
397 }
398 }
399 $show['cached'] = true;
400 $hilight = $search['hilight'];
401 }
402
403 foreach ($results AS $bug)
404 {
405 $funct->exec_swap_bg($stylevar['alt_colour'], '');
406 $bug['bgcolour'] = ($bugsys->userinfo['showcolours'] ? $bugsys->datastore['status']["$bug[status]"]['color'] : $funct->bgcolour);
407 $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
408 $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
409 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
410 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
411 $bug['lastposttime'] = (($bug['hiddendisplay']) ? $bug['hiddenlastposttime'] : $bug['lastposttime']);
412 $bug['lastpost'] = (($bug['hiddendisplay']) ? $bug['hiddenlastpost'] : $bug['lastpost']);
413 $bug['lastposttime'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']);
414 $bug['urladd'] = "&amp;hilight=$hilight";
415 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
416 }
417
418 eval('$template->flush("' . $template->fetch('search_results') . '");');
419 }
420
421 /*=====================================================================*\
422 || ###################################################################
423 || # $HeadURL$
424 || # $Id$
425 || ###################################################################
426 \*=====================================================================*/
427 ?>