r661: Remove else branch on PCV selection
[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
122 // -------------------------------------------------------------------
123 // severity, priority, status, resolution, assignedto
124
125 // severity
126 if ($bugsys->in['severity'])
127 {
128 $querybuild['severity'] = "AND bug.severity = " . intval($bugsys->in['severity']);
129 }
130
131 // priority
132 if ($bugsys->in['priority'])
133 {
134 $querybuild['priority'] = "AND bug.priority = " . intval($bugsys->in['priority']);
135 }
136
137 // status
138 if ($bugsys->in['status'])
139 {
140 $querybuild['status'] = "AND bug.status = " . intval($bugsys->in['status']);
141 }
142
143 // resolution
144 if ($bugsys->in['resolution'])
145 {
146 $querybuild['resolution'] = "AND bug.resolution = " . intval($bugsys->in['resolution']);
147 }
148
149 // assignment
150 if ($bugsys->in['assignedto'])
151 {
152 $querybuild['assignedto'] = "AND bug.assignedto = " . intval($bugsys->in['assignedto']);
153 }
154
155 // -------------------------------------------------------------------
156 // date
157 if ($bugsys->in['date'])
158 {
159 // now - (seconds/day * number of days)
160 $dateline = time() - (intval($bugsys->in['date']) * 3600);
161 $querybuild['date'] = "AND bug.dateline >= $dateline";
162 }
163
164 // -------------------------------------------------------------------
165 // favourites
166 if ($bugsys->in['favourite'] AND $bugsys->userinfo['userid'])
167 {
168 $favourites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $bugsys->userinfo['userid']);
169 while ($favourite = $db->fetch_array($favourites))
170 {
171 $ids[] = $favourite['bugid'];
172 }
173 $querybuild['favourites'] = "AND bug.bugid IN (" . implode(', ', $ids) . ")";
174 }
175
176 // -------------------------------------------------------------------
177 // sort by
178 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
179 $orderby = array('ASC', 'DESC');
180 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
181 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
182 {
183 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
184 }
185 else if ($bugsys->in['sortby'] == 'relevance')
186 {
187 $sortclause = '';
188 }
189 else
190 {
191 $sortclause = '';
192 }
193
194 // -------------------------------------------------------------------
195 // custom fields
196 $fields_fetch = $bugsys->db->query("
197 SELECT bugfield.*
198 FROM " . TABLE_PREFIX . "bugfield AS bugfield
199 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
200 ON (bugfield.fieldid = permission.fieldid)
201 WHERE permission.mask <> 0
202 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
203 AND bugfield.cansearch = 1"
204 );
205 while ($field = $bugsys->db->fetch_array($fields_fetch))
206 {
207 if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"])))
208 {
209 if ($field['type'] == 'input_checkbox')
210 {
211 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] == 1) ? 0 : 1);
212 }
213 else if ($field['type'] == 'input_text')
214 {
215 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'";
216 }
217 else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1)
218 {
219 $temp = unserialize($field['selects']);
220 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'";
221 }
222 }
223 }
224
225 // -------------------------------------------------------------------
226 // have to search something
227 if (count($querybuild) < 1)
228 {
229 $message->error($lang->string('You have to enter some criteria to search for'));
230 }
231
232 // -------------------------------------------------------------------
233 // do the search
234 $query = "
235 SELECT bug.*, comment.commentid,
236 user1.displayname AS firstreport,
237 user2.displayname AS lastpost
238 FROM " . TABLE_PREFIX . "bug AS bug
239 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
240 ON (bug.bugid = comment.bugid)
241 LEFT JOIN " . TABLE_PREFIX . "user AS user1
242 ON (bug.userid = user1.userid)
243 LEFT JOIN " . TABLE_PREFIX . "user AS user2
244 ON (bug.lastpostby = user2.userid)
245 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
246 ON (bug.bugid = bugfieldvalue.bugid)
247 WHERE bug.bugid <> 0
248 AND bug.productid IN (#<'ONBITS:VIEW'>#)
249 AND (!bug.hidden OR (bug.hidden AND bug.productid IN (#<'ONBITS:HIDDEN'>#)))
250 " . implode("\n\t\t", $querybuild) . "
251 GROUP BY bug.bugid
252 $sortclause";
253
254 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $query);
255
256 $search = $db->query($runquery);
257
258 $numrows = $db->num_rows($search);
259
260 if ($numrows < 1)
261 {
262 $message->error($lang->string('No search results were returned that matched your criteria.'));
263 }
264
265 while ($result = $db->fetch_array($search))
266 {
267 $ids[] = $result['bugid'];
268 $results[] = $result;
269 }
270
271 if ($bugsys->userinfo['userid'])
272 {
273 $db->query("
274 REPLACE INTO " . TABLE_PREFIX . "search
275 (userid, dateline, query, ids, orderby, hilight)
276 VALUES
277 (" . $bugsys->userinfo['userid'] . ",
278 " . TIMENOW . ", '" . $bugsys->escape($query) . "',
279 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
280 '" . $bugsys->escape($hilight) . "'
281 )"
282 );
283 }
284
285 $justprocess = true;
286
287 $_REQUEST['do'] = 'results';
288 }
289
290 // ###################################################################
291
292 if ($_REQUEST['do'] == 'search')
293 {
294 if ($bugsys->userinfo['userid'] AND !$bugsys->in['new'])
295 {
296 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid']))
297 {
298 $_REQUEST['do'] = 'results';
299 }
300 else
301 {
302 $newsearch = true;
303 }
304 }
305 else
306 {
307 $newsearch = true;
308 }
309
310 if ($newsearch)
311 {
312 $pcv_select = construct_pcv_select();
313
314 // -------------------------------------------------------------------
315 // custom fields
316 $fields = construct_custom_fields($bug);
317 $show['customfields'] = ($fields ? true : false);
318
319 $bugsys->debug('fields modulo: ' . count($fields) % 2);
320 $i = 1;
321 foreach ($fields AS $field)
322 {
323 $bugsys->debug("i = $i");
324 if ($i == 1)
325 {
326 $customfields['right'] .= $field;
327 }
328 else if ($i == 2)
329 {
330 $customfields['left'] .= $field;
331 $i = 0;
332 }
333 $i++;
334 }
335
336 // -------------------------------------------------------------------
337 // built-in fields
338 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, true);
339 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, true);
340 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, true);
341 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, true);
342
343 $select['dev'] = '<option value="0" selected="selected"></option>';
344 foreach ($bugsys->datastore['assignto'] AS $dev)
345 {
346 $value = $dev['userid'];
347 $label = construct_user_display($dev, false);
348 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
349 }
350
351 eval('$template->flush("' . $template->fetch('search') . '");');
352 }
353 }
354
355 // ###################################################################
356
357 if ($_REQUEST['do'] == 'results')
358 {
359 $show['cached'] = false;
360 if ($bugsys->userinfo['userid'] AND !$justprocess)
361 {
362 $search = $cachedsearch;
363 if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
364 {
365 $search = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $search['query']));
366 while ($bug = $db->fetch_array($search))
367 {
368 $ids[] = $bug['bugid'];
369 $results[] = $bug;
370 }
371 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . " WHERE userid = " . $bugsys->userinfo['userid']);
372 }
373 else
374 {
375 $search = $db->query("
376 SELECT bug.*, user1.displayname AS firstreport,
377 user2.displayname AS lastpost
378 FROM " . TABLE_PREFIX . "bug AS bug
379 LEFT JOIN " . TABLE_PREFIX . "user AS user1
380 ON (bug.userid = user1.userid)
381 LEFT JOIN " . TABLE_PREFIX . "user AS user2
382 ON (bug.lastpostby = user2.userid)
383 WHERE bug.bugid IN ($search[ids])
384 $search[orderby]"
385 );
386 while ($bug = $db->fetch_array($search))
387 {
388 $results[] = $bug;
389 }
390 }
391 $show['cached'] = true;
392 $hilight = $search['hilight'];
393 }
394
395 foreach ($results AS $bug)
396 {
397 $funct->exec_swap_bg($stylevar['alt_colour'], '');
398 $bug['bgcolour'] = ($bugsys->userinfo['showcolours'] ? $bugsys->datastore['status']["$bug[status]"]['color'] : $funct->bgcolour);
399 $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
400 $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
401 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
402 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
403 $bug['lastposttime'] = (($bug['hiddendisplay']) ? $bug['hiddenlastposttime'] : $bug['lastposttime']);
404 $bug['lastpost'] = (($bug['hiddendisplay']) ? $bug['hiddenlastpost'] : $bug['lastpost']);
405 $bug['lastposttime'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']);
406 $bug['urladd'] = "&amp;hilight=$hilight";
407 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
408 }
409
410 eval('$template->flush("' . $template->fetch('search_results') . '");');
411 }
412
413 /*=====================================================================*\
414 || ###################################################################
415 || # $HeadURL$
416 || # $Id$
417 || ###################################################################
418 \*=====================================================================*/
419 ?>