r303: Added SVN constant to user files; now ISSO debug information shows SVN details.
[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 );
17
18 define('SVN', '$Id$');
19
20 require_once('./global.php');
21
22 if (!can_perform('cansearch'))
23 {
24 $message->error_permission();
25 }
26
27 define('MODE', intval($bugsys->in['mode']));
28 define('MODE_ANY', ((MODE == 1) ? 1 : 0));
29 define('MODE_ALL', ((MODE == 2) ? 1 : 0));
30 define('MODE_RAW', ((MODE == 3) ? 1 : 0));
31
32 define('SEARCH_WORD_MIN', 3);
33
34 $show['search'] = true;
35
36 // ###################################################################
37
38 if (empty($_REQUEST['do']))
39 {
40 $_REQUEST['do'] = 'search';
41 }
42
43 // ###################################################################
44
45 if ($_REQUEST['do'] == 'results')
46 {
47 // -------------------------------------------------------------------
48 // parse out our product/component/version
49 $pcv = parse_pcv_select($bugsys->in['pcv_select']);
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 = '" . $bugsys->in['reporter'] . "' OR MATCH (displayname) AGAINST ('" . $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']))
111 {
112 $querybuild['pcv'] = "AND bug.productid = $pcv[product] AND bug.componentid = $pcv[component] AND bug.versionid = $pcv[version]";
113 }
114
115 // -------------------------------------------------------------------
116 // severity, priority, status, resolution, assignedto
117
118 // severity
119 if ($bugsys->in['severity'])
120 {
121 $querybuild['severity'] = "AND bug.severity = " . intval($bugsys->in['severity']);
122 }
123
124 // priority
125 if ($bugsys->in['priority'])
126 {
127 $querybuild['priority'] = "AND bug.priority = " . intval($bugsys->in['priority']);
128 }
129
130 // status
131 if ($bugsys->in['status'])
132 {
133 $querybuild['status'] = "AND bug.status = " . intval($bugsys->in['status']);
134 }
135
136 // resolution
137 if ($bugsys->in['resolution'])
138 {
139 $querybuild['resolution'] = "AND bug.resolution = " . intval($bugsys->in['resolution']);
140 }
141
142 // assignment
143 if ($bugsys->in['assignedto'])
144 {
145 $querybuild['assignedto'] = "AND bug.assignedto = " . intval($bugsys->in['assignedto']);
146 }
147
148 // -------------------------------------------------------------------
149 // date
150 if ($bugsys->in['date'])
151 {
152 // now - (seconds/day * number of days)
153 $dateline = time() - (intval($bugsys->in['date']) * 3600);
154 $querybuild['date'] = "AND bug.dateline >= $dateline";
155 }
156
157 // -------------------------------------------------------------------
158 // favourites
159 if ($bugsys->in['favourite'] AND $bugsys->userinfo['userid'])
160 {
161 $favourites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $bugsys->userinfo['userid']);
162 while ($favourite = $db->fetch_array($favourites))
163 {
164 $ids[] = $favourite['bugid'];
165 }
166 $querybuild['favourites'] = "AND bug.bugid IN (" . implode(', ', $ids) . ")";
167 }
168
169 // -------------------------------------------------------------------
170 // sort by
171 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
172 $orderby = array('ASC', 'DESC');
173 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
174 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
175 {
176 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
177 }
178 else if ($bugsys->in['sortby'] == 'relevance')
179 {
180 $sortclause = '';
181 }
182 else
183 {
184 $sortclause = '';
185 }
186
187 // -------------------------------------------------------------------
188 // custom fields
189 $fields_fetch = $bugsys->db->query("
190 SELECT bugfield.*
191 FROM " . TABLE_PREFIX . "bugfield AS bugfield
192 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
193 ON (bugfield.fieldid = permission.fieldid)
194 WHERE permission.mask <> 0
195 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
196 AND bugfield.cansearch = 1"
197 );
198 while ($field = $bugsys->db->fetch_array($fields_fetch))
199 {
200 if (!empty($bugsys->in['custom']["$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in['custom']["$field[fieldid]"])))
201 {
202 if ($field['type'] == 'input_checkbox')
203 {
204 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in['custom']["$field[fieldid]"] == 1) ? 0 : 1);
205 }
206 else if ($field['type'] == 'input_text')
207 {
208 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in['custom']["$field[fieldid]"] . "%'";
209 }
210 else if ($field['type'] == 'select_single' AND $bugsys->in['custom']["$field[fieldid]"] != -1)
211 {
212 $temp = unserialize($field['selects']);
213 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in['custom']["$field[fieldid]"]) ]) . "'";
214 }
215 }
216 }
217
218 // -------------------------------------------------------------------
219 // have to search something
220 if (count($querybuild) < 1)
221 {
222 $message->error('you need to search for something!');
223 }
224
225 // -------------------------------------------------------------------
226 // do the search
227 $search = $db->query("
228 SELECT bug.*, comment.commentid,
229 user1.displayname AS firstreport,
230 user2.displayname AS lastpost
231 FROM " . TABLE_PREFIX . "bug AS bug
232 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
233 ON (bug.bugid = comment.bugid)
234 LEFT JOIN " . TABLE_PREFIX . "user AS user1
235 ON (bug.userid = user1.userid)
236 LEFT JOIN " . TABLE_PREFIX . "user AS user2
237 ON (bug.lastpostby = user2.userid)
238 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
239 ON (bug.bugid = bugfieldvalue.bugid)
240 WHERE bug.bugid <> 0
241 " . implode("\n\t\t", $querybuild) . ((!can_perform('canviewhidden')) ? "
242 AND !bug.hidden
243 AND !comment.hidden" : "") . "
244 GROUP BY bug.bugid
245 $sortclause"
246 );
247
248 $numrows = $db->num_rows($search);
249
250 if ($numrows < 1)
251 {
252 $message->error('no results found');
253 }
254
255 while ($bug = $db->fetch_array($search))
256 {
257 $bug['bgcolour'] = $bugsys->datastore['status']["$bug[status]"]['color'];
258 $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
259 $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
260 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
261 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
262 $bug['lastpostinfo'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']) . ' by ' . $bug['lastpost'];
263 $bug['urladd'] = "&amp;hilight=$hilight";
264 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
265 }
266
267 eval('$template->flush("' . $template->fetch('search_results') . '");');
268 }
269
270 // ###################################################################
271
272 if ($_REQUEST['do'] == 'search')
273 {
274 $pcv_select = construct_pcv_select('radio', '--');
275
276 // -------------------------------------------------------------------
277 // custom fields
278 $fields_fetch = $bugsys->db->query("
279 SELECT bugfield.*
280 FROM " . TABLE_PREFIX . "bugfield AS bugfield
281 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
282 ON (bugfield.fieldid = permission.fieldid)
283 WHERE permission.mask <> 0
284 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
285 AND bugfield.cansearch = 1"
286 );
287 while ($field = $bugsys->db->fetch_array($fields_fetch))
288 {
289 switch ($field['type'])
290 {
291 case 'input_text':
292 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
293 break;
294
295 case 'input_checkbox':
296 $selected = (($value) ? ' checked="checked"' : '');
297
298 // ignore
299 $value = 0;
300 $label = '';
301 $selected = true;
302 eval('$options = "' . $bugsys->template->fetch('selectoption') . '";');
303
304 // on
305 $value = 1;
306 $label = 'Checked';
307 $selected = false;
308 eval('$options .= "' . $bugsys->template->fetch('selectoption') . '";');
309
310 // off
311 $value = 2;
312 $label = 'Un-Checked';
313 $selected = false;
314 eval('$options .= "' . $bugsys->template->fetch('selectoption') . '";');
315
316 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
317 break;
318
319 case 'select_single':
320 $selectopts = unserialize($field['selects']);
321 $value = trim($value);
322 $options = '';
323
324 $id = -1;
325 $select = '';
326 $selected = ' selected="selected"';
327 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
328 $selected = '';
329
330 foreach ($selectopts AS $id => $select)
331 {
332 $select = trim($select);
333 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
334 }
335 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
336 break;
337 }
338 $fieldbits .= $tempfield;
339 }
340 unset($select);
341
342 // -------------------------------------------------------------------
343 // built-in fields
344 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, true);
345 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, true);
346 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, true);
347 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, true);
348
349 $select['dev'] = '<option value="0" selected="selected"></option>';
350 foreach ($bugsys->datastore['assignto'] AS $dev)
351 {
352 $value = $dev['userid'];
353 $label = construct_user_display($dev, false);
354 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
355 }
356
357 eval('$template->flush("' . $template->fetch('search') . '");');
358 }
359
360 /*=====================================================================*\
361 || ###################################################################
362 || # $HeadURL$
363 || # $Id$
364 || ###################################################################
365 \*=====================================================================*/
366 ?>