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