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