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