r304: Added tab navigation system.
[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 $focus['search'] = 'focus';
21
22 require_once('./global.php');
23
24 if (!can_perform('cansearch'))
25 {
26 $message->error_permission();
27 }
28
29 define('MODE', intval($bugsys->in['mode']));
30 define('MODE_ANY', ((MODE == 1) ? 1 : 0));
31 define('MODE_ALL', ((MODE == 2) ? 1 : 0));
32 define('MODE_RAW', ((MODE == 3) ? 1 : 0));
33
34 define('SEARCH_WORD_MIN', 3);
35
36 $show['search'] = true;
37
38 // ###################################################################
39
40 if (empty($_REQUEST['do']))
41 {
42 $_REQUEST['do'] = 'search';
43 }
44
45 // ###################################################################
46
47 if ($_REQUEST['do'] == 'results')
48 {
49 // -------------------------------------------------------------------
50 // parse out our product/component/version
51 $pcv = parse_pcv_select($bugsys->in['pcv_select']);
52
53 // -------------------------------------------------------------------
54 // handle keywords
55 if ($bugsys->in['summary'])
56 {
57 $keywords = preg_split('#\s+#', $bugsys->in['summary']);
58
59 // #*# need to have some str to bool conversions
60
61 foreach ($keywords AS $word)
62 {
63 if (strlen($word) < SEARCH_WORD_MIN)
64 {
65 continue;
66 }
67
68 if (MODE_ALL)
69 {
70 $querybuild['text'] .= " +$word";
71 }
72 else
73 {
74 $querybuild['text'] .= " $word";
75 }
76
77 if (!preg_match('#-(.+?)#', trim($word)))
78 {
79 $hilight .= " $word";
80 }
81 }
82
83 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
84 $hilight = trim($hilight);
85 $hilight = preg_replace('#\s#', '+', $hilight);
86
87 $temp = trim($querybuild['text']);
88
89 if (MODE_ALL OR MODE_RAW)
90 {
91 $bool_flag = ' IN BOOLEAN MODE';
92 }
93
94 $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)";
95 }
96
97 // -------------------------------------------------------------------
98 // reporter
99 if ($bugsys->in['reporter'])
100 {
101 // force email or name?? make a distinction?
102 // more elegant way to do this? probably
103 $user = $db->query_first("SELECT * FROM user WHERE email = '" . $bugsys->in['reporter'] . "' OR MATCH (displayname) AGAINST ('" . $bugsys->in['reporter'] . "')");
104 if ($user['userid'])
105 {
106 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
107 }
108 }
109
110 // -------------------------------------------------------------------
111 // product/component/version stuff
112 if (is_array($bugsys->in['pcv']))
113 {
114 $querybuild['pcv'] = "AND bug.productid = $pcv[product] AND bug.componentid = $pcv[component] AND bug.versionid = $pcv[version]";
115 }
116
117 // -------------------------------------------------------------------
118 // severity, priority, status, resolution, assignedto
119
120 // severity
121 if ($bugsys->in['severity'])
122 {
123 $querybuild['severity'] = "AND bug.severity = " . intval($bugsys->in['severity']);
124 }
125
126 // priority
127 if ($bugsys->in['priority'])
128 {
129 $querybuild['priority'] = "AND bug.priority = " . intval($bugsys->in['priority']);
130 }
131
132 // status
133 if ($bugsys->in['status'])
134 {
135 $querybuild['status'] = "AND bug.status = " . intval($bugsys->in['status']);
136 }
137
138 // resolution
139 if ($bugsys->in['resolution'])
140 {
141 $querybuild['resolution'] = "AND bug.resolution = " . intval($bugsys->in['resolution']);
142 }
143
144 // assignment
145 if ($bugsys->in['assignedto'])
146 {
147 $querybuild['assignedto'] = "AND bug.assignedto = " . intval($bugsys->in['assignedto']);
148 }
149
150 // -------------------------------------------------------------------
151 // date
152 if ($bugsys->in['date'])
153 {
154 // now - (seconds/day * number of days)
155 $dateline = time() - (intval($bugsys->in['date']) * 3600);
156 $querybuild['date'] = "AND bug.dateline >= $dateline";
157 }
158
159 // -------------------------------------------------------------------
160 // favourites
161 if ($bugsys->in['favourite'] AND $bugsys->userinfo['userid'])
162 {
163 $favourites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $bugsys->userinfo['userid']);
164 while ($favourite = $db->fetch_array($favourites))
165 {
166 $ids[] = $favourite['bugid'];
167 }
168 $querybuild['favourites'] = "AND bug.bugid IN (" . implode(', ', $ids) . ")";
169 }
170
171 // -------------------------------------------------------------------
172 // sort by
173 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
174 $orderby = array('ASC', 'DESC');
175 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
176 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
177 {
178 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
179 }
180 else if ($bugsys->in['sortby'] == 'relevance')
181 {
182 $sortclause = '';
183 }
184 else
185 {
186 $sortclause = '';
187 }
188
189 // -------------------------------------------------------------------
190 // custom fields
191 $fields_fetch = $bugsys->db->query("
192 SELECT bugfield.*
193 FROM " . TABLE_PREFIX . "bugfield AS bugfield
194 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
195 ON (bugfield.fieldid = permission.fieldid)
196 WHERE permission.mask <> 0
197 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
198 AND bugfield.cansearch = 1"
199 );
200 while ($field = $bugsys->db->fetch_array($fields_fetch))
201 {
202 if (!empty($bugsys->in['custom']["$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in['custom']["$field[fieldid]"])))
203 {
204 if ($field['type'] == 'input_checkbox')
205 {
206 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in['custom']["$field[fieldid]"] == 1) ? 0 : 1);
207 }
208 else if ($field['type'] == 'input_text')
209 {
210 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in['custom']["$field[fieldid]"] . "%'";
211 }
212 else if ($field['type'] == 'select_single' AND $bugsys->in['custom']["$field[fieldid]"] != -1)
213 {
214 $temp = unserialize($field['selects']);
215 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in['custom']["$field[fieldid]"]) ]) . "'";
216 }
217 }
218 }
219
220 // -------------------------------------------------------------------
221 // have to search something
222 if (count($querybuild) < 1)
223 {
224 $message->error('you need to search for something!');
225 }
226
227 // -------------------------------------------------------------------
228 // do the search
229 $search = $db->query("
230 SELECT bug.*, comment.commentid,
231 user1.displayname AS firstreport,
232 user2.displayname AS lastpost
233 FROM " . TABLE_PREFIX . "bug AS bug
234 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
235 ON (bug.bugid = comment.bugid)
236 LEFT JOIN " . TABLE_PREFIX . "user AS user1
237 ON (bug.userid = user1.userid)
238 LEFT JOIN " . TABLE_PREFIX . "user AS user2
239 ON (bug.lastpostby = user2.userid)
240 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
241 ON (bug.bugid = bugfieldvalue.bugid)
242 WHERE bug.bugid <> 0
243 " . implode("\n\t\t", $querybuild) . ((!can_perform('canviewhidden')) ? "
244 AND !bug.hidden
245 AND !comment.hidden" : "") . "
246 GROUP BY bug.bugid
247 $sortclause"
248 );
249
250 $numrows = $db->num_rows($search);
251
252 if ($numrows < 1)
253 {
254 $message->error('no results found');
255 }
256
257 while ($bug = $db->fetch_array($search))
258 {
259 $bug['bgcolour'] = $bugsys->datastore['status']["$bug[status]"]['color'];
260 $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
261 $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
262 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
263 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
264 $bug['lastpostinfo'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']) . ' by ' . $bug['lastpost'];
265 $bug['urladd'] = "&amp;hilight=$hilight";
266 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
267 }
268
269 eval('$template->flush("' . $template->fetch('search_results') . '");');
270 }
271
272 // ###################################################################
273
274 if ($_REQUEST['do'] == 'search')
275 {
276 $pcv_select = construct_pcv_select('radio', '--');
277
278 // -------------------------------------------------------------------
279 // custom fields
280 $fields_fetch = $bugsys->db->query("
281 SELECT bugfield.*
282 FROM " . TABLE_PREFIX . "bugfield AS bugfield
283 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
284 ON (bugfield.fieldid = permission.fieldid)
285 WHERE permission.mask <> 0
286 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
287 AND bugfield.cansearch = 1"
288 );
289 while ($field = $bugsys->db->fetch_array($fields_fetch))
290 {
291 switch ($field['type'])
292 {
293 case 'input_text':
294 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
295 break;
296
297 case 'input_checkbox':
298 $selected = (($value) ? ' checked="checked"' : '');
299
300 // ignore
301 $value = 0;
302 $label = '';
303 $selected = true;
304 eval('$options = "' . $bugsys->template->fetch('selectoption') . '";');
305
306 // on
307 $value = 1;
308 $label = 'Checked';
309 $selected = false;
310 eval('$options .= "' . $bugsys->template->fetch('selectoption') . '";');
311
312 // off
313 $value = 2;
314 $label = 'Un-Checked';
315 $selected = false;
316 eval('$options .= "' . $bugsys->template->fetch('selectoption') . '";');
317
318 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
319 break;
320
321 case 'select_single':
322 $selectopts = unserialize($field['selects']);
323 $value = trim($value);
324 $options = '';
325
326 $id = -1;
327 $select = '';
328 $selected = ' selected="selected"';
329 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
330 $selected = '';
331
332 foreach ($selectopts AS $id => $select)
333 {
334 $select = trim($select);
335 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
336 }
337 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
338 break;
339 }
340 $fieldbits .= $tempfield;
341 }
342 unset($select);
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 || # $HeadURL$
365 || # $Id$
366 || ###################################################################
367 \*=====================================================================*/
368 ?>