r254: Searching of custom fields now works again
[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 // sort by
157 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
158 $orderby = array('ASC', 'DESC');
159 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
160 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
161 {
162 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
163 }
164 else if ($bugsys->in['sortby'] == 'relevance')
165 {
166 $sortclause = '';
167 }
168 else
169 {
170 $message->error('bad sort');
171 }
172
173 // -------------------------------------------------------------------
174 // custom fields
175 $fields_fetch = $bugsys->db->query("
176 SELECT bugfield.*
177 FROM " . TABLE_PREFIX . "bugfield AS bugfield
178 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
179 ON (bugfield.fieldid = permission.fieldid)
180 WHERE permission.mask <> 0
181 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
182 AND bugfield.cansearch = 1"
183 );
184 while ($field = $bugsys->db->fetch_array($fields_fetch))
185 {
186 if (!empty($bugsys->in['custom']["$field[fieldid]"]) OR $field['type'] == 'select_single')
187 {
188 if ($field['type'] == 'input_checkbox')
189 {
190 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in['custom']["$field[fieldid]"] == 1) ? 0 : 1);
191 }
192 else if ($field['type'] == 'input_text')
193 {
194 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in['custom']["$field[fieldid]"] . "%'";
195 }
196 else if ($field['type'] == 'select_single' AND $bugsys->in['custom']["$field[fieldid]"] != -1)
197 {
198 $temp = unserialize($field['selects']);
199 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in['custom']["$field[fieldid]"]) ]) . "'";
200 }
201 }
202 }
203
204 // -------------------------------------------------------------------
205 // have to search something
206 if (count($querybuild) < 1)
207 {
208 $message->error('you need to search for something!');
209 }
210
211 // -------------------------------------------------------------------
212 // do the search
213 $search = $db->query("
214 SELECT bug.*, comment.commentid,
215 user1.displayname AS firstreport,
216 user2.displayname AS lastpost
217 FROM " . TABLE_PREFIX . "bug AS bug
218 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
219 ON (bug.bugid = comment.bugid)
220 LEFT JOIN " . TABLE_PREFIX . "user AS user1
221 ON (bug.userid = user1.userid)
222 LEFT JOIN " . TABLE_PREFIX . "user AS user2
223 ON (bug.lastpostby = user2.userid)
224 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
225 ON (bug.bugid = bugfieldvalue.bugid)
226 WHERE bug.bugid <> 0
227 " . implode("\n\t\t\t", $querybuild) . ((!can_perform('canviewhidden')) ? "
228 AND !bug.hidden
229 AND !comment.hidden" : "") . "
230 $sortclause
231 GROUP BY bug.bugid"
232 );
233
234 $numrows = $db->num_rows($search);
235
236 if ($numrows < 1)
237 {
238 $message->error('no results found');
239 }
240
241 while ($bug = $db->fetch_array($search))
242 {
243 $bug['bgcolour'] = $bugsys->datastore['status']["$bug[status]"]['color'];
244 $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
245 $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
246 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
247 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
248 $bug['lastpostinfo'] = datelike('standard', $bug['lastposttime']) . ' by ' . $bug['lastpost'];
249 $bug['urladd'] = "&amp;hilight=$hilight";
250 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
251 }
252
253 eval('$template->flush("' . $template->fetch('search_results') . '");');
254 }
255
256 // ###################################################################
257
258 if ($_REQUEST['do'] == 'search')
259 {
260 $pcv_select = construct_pcv_select('radio', '--');
261
262 // -------------------------------------------------------------------
263 // custom fields
264 $fields_fetch = $bugsys->db->query("
265 SELECT bugfield.*
266 FROM " . TABLE_PREFIX . "bugfield AS bugfield
267 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
268 ON (bugfield.fieldid = permission.fieldid)
269 WHERE permission.mask <> 0
270 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
271 AND bugfield.cansearch = 1"
272 );
273 while ($field = $bugsys->db->fetch_array($fields_fetch))
274 {
275 switch ($field['type'])
276 {
277 case 'input_text':
278 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_input_text') . '";');
279 break;
280
281 case 'input_checkbox':
282 $selected = (($value) ? ' checked="checked"' : '');
283
284 // ignore
285 $value = 0;
286 $label = '';
287 $selected = true;
288 eval('$options = "' . $bugsys->template->fetch('selectoption') . '";');
289
290 // on
291 $value = 1;
292 $label = 'Checked';
293 $selected = false;
294 eval('$options .= "' . $bugsys->template->fetch('selectoption') . '";');
295
296 // off
297 $value = 2;
298 $label = 'Un-Checked';
299 $selected = false;
300 eval('$options .= "' . $bugsys->template->fetch('selectoption') . '";');
301
302 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
303 break;
304
305 case 'select_single':
306 $selectopts = unserialize($field['selects']);
307 $value = trim($value);
308 $options = '';
309
310 $id = -1;
311 $select = '';
312 $selected = ' selected="selected"';
313 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
314 $selected = '';
315
316 foreach ($selectopts AS $id => $select)
317 {
318 $select = trim($select);
319 eval('$options .= "' . $bugsys->template->fetch('bugfield_select_single_option') . '";');
320 }
321 eval('$tempfield = "' . $bugsys->template->fetch('bugfield_select_single') . '";');
322 break;
323 }
324 $fieldbits .= $tempfield;
325 }
326 unset($select);
327
328 // -------------------------------------------------------------------
329 // built-in fields
330 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', $selectedvalue = 0, $includeblank = true);
331 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', $selectedvalue = 0, $includeblank = true);
332 $select['status'] = construct_datastore_select('status', 'status', 'statusid', $selectedvalue = 0, $includeblank = true);
333 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', $selectedvalue = 0, $includeblank = true);
334
335 $select['dev'] = '<option value="0" selected="selected"></option>';
336 foreach ($bugsys->datastore['assignto'] AS $dev)
337 {
338 $value = $dev['userid'];
339 $label = construct_user_display($dev, false);
340 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
341 }
342
343 eval('$template->flush("' . $template->fetch('search') . '");');
344 }
345
346 /*=====================================================================*\
347 || ###################################################################
348 || # $HeadURL$
349 || # $Id$
350 || ###################################################################
351 \*=====================================================================*/
352 ?>