r672: Make sure we construct editable-only custom fields
[bugdar.git] / search.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Bugdar [#]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 'trackerhome_bits'
17 );
18
19 define('SVN', '$Id$');
20
21 $focus['search'] = 'focus';
22
23 require_once('./global.php');
24 require_once('./includes/functions_product.php');
25
26 if (!can_perform('cansearch'))
27 {
28 $message->error_permission();
29 }
30
31 define('MODE_ANY', ($bugsys->in['mode'] == 1));
32 define('MODE_ALL', ($bugsys->in['mode'] == 2));
33 define('MODE_RAW', ($bugsys->in['mode'] == 3));
34
35 define('SEARCH_WORD_MIN', 3);
36
37 $show['search'] = true;
38
39 // ###################################################################
40
41 if (empty($_REQUEST['do']))
42 {
43 $_REQUEST['do'] = 'search';
44 }
45
46 // ###################################################################
47
48 if ($_REQUEST['do'] == 'process')
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 LIKE '%" . str_replace('%', '\%', $bugsys->in['reporter']) . "' OR displayname LIKE '%" . str_replace('%', '\%', $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_select']))
110 {
111 foreach ($bugsys->in['pcv_select'] AS $pcv)
112 {
113 $pcv = parse_pcv_select($pcv, true);
114 $products[] = $pcv['product'];
115 $components[] = $pcv['component'];
116 $versions[] = $pcv['version'];
117 }
118 $querybuild['pcv'] = "AND bug.productid IN (" . implode(',', $products) . ") AND bug.componentid IN (" . implode(',', $components) . ") AND bug.versionid IN (" . implode(',', $versions) . ")";
119 }
120
121 // -------------------------------------------------------------------
122 // severity, priority, status, resolution, assignedto
123
124 // severity
125 if ($bugsys->in['severity'])
126 {
127 $querybuild['severity'] = "AND bug.severity = " . intval($bugsys->in['severity']);
128 }
129
130 // priority
131 if ($bugsys->in['priority'])
132 {
133 $querybuild['priority'] = "AND bug.priority = " . intval($bugsys->in['priority']);
134 }
135
136 // status
137 if ($bugsys->in['status'])
138 {
139 $querybuild['status'] = "AND bug.status = " . intval($bugsys->in['status']);
140 }
141
142 // resolution
143 if ($bugsys->in['resolution'])
144 {
145 $querybuild['resolution'] = "AND bug.resolution = " . intval($bugsys->in['resolution']);
146 }
147
148 // assignment
149 if ($bugsys->in['assignedto'])
150 {
151 $querybuild['assignedto'] = "AND bug.assignedto = " . intval($bugsys->in['assignedto']);
152 }
153
154 // -------------------------------------------------------------------
155 // date
156 if ($bugsys->in['date'])
157 {
158 // now - (seconds/day * number of days)
159 $dateline = time() - (intval($bugsys->in['date']) * 3600);
160 $querybuild['date'] = "AND bug.dateline >= $dateline";
161 }
162
163 // -------------------------------------------------------------------
164 // favourites
165 if ($bugsys->in['favourite'] AND $bugsys->userinfo['userid'])
166 {
167 $favourites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $bugsys->userinfo['userid']);
168 while ($favourite = $db->fetch_array($favourites))
169 {
170 $ids[] = $favourite['bugid'];
171 }
172 $querybuild['favourites'] = "AND bug.bugid IN (" . implode(', ', $ids) . ")";
173 }
174
175 // -------------------------------------------------------------------
176 // sort by
177 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
178 $orderby = array('ASC', 'DESC');
179 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
180 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
181 {
182 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
183 }
184 else if ($bugsys->in['sortby'] == 'relevance')
185 {
186 $sortclause = '';
187 }
188 else
189 {
190 $sortclause = '';
191 }
192
193 // -------------------------------------------------------------------
194 // custom fields
195 $fields_fetch = $bugsys->db->query("
196 SELECT bugfield.*
197 FROM " . TABLE_PREFIX . "bugfield AS bugfield
198 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
199 ON (bugfield.fieldid = permission.fieldid)
200 WHERE permission.mask <> 0
201 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
202 AND bugfield.cansearch = 1"
203 );
204 while ($field = $bugsys->db->fetch_array($fields_fetch))
205 {
206 if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"])))
207 {
208 if ($field['type'] == 'input_checkbox')
209 {
210 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] == 1) ? 0 : 1);
211 }
212 else if ($field['type'] == 'input_text')
213 {
214 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'";
215 }
216 else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1)
217 {
218 $temp = unserialize($field['selects']);
219 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'";
220 }
221 }
222 }
223
224 // -------------------------------------------------------------------
225 // have to search something
226 if (count($querybuild) < 1)
227 {
228 $message->error($lang->string('You have to enter some criteria to search for'));
229 }
230
231 // -------------------------------------------------------------------
232 // do the search
233 $query = "
234 SELECT bug.*, comment.commentid,
235 user1.displayname AS firstreport,
236 user2.displayname AS lastpost
237 FROM " . TABLE_PREFIX . "bug AS bug
238 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
239 ON (bug.bugid = comment.bugid)
240 LEFT JOIN " . TABLE_PREFIX . "user AS user1
241 ON (bug.userid = user1.userid)
242 LEFT JOIN " . TABLE_PREFIX . "user AS user2
243 ON (bug.lastpostby = user2.userid)
244 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
245 ON (bug.bugid = bugfieldvalue.bugid)
246 WHERE bug.bugid <> 0
247 AND bug.productid IN (#<'ONBITS:VIEW'>#)
248 AND (!bug.hidden OR (bug.hidden AND bug.productid IN (#<'ONBITS:HIDDEN'>#)))
249 " . implode("\n\t\t", $querybuild) . "
250 GROUP BY bug.bugid
251 $sortclause";
252
253 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $query);
254
255 $search = $db->query($runquery);
256
257 $numrows = $db->num_rows($search);
258
259 if ($numrows < 1)
260 {
261 $message->error($lang->string('No search results were returned that matched your criteria.'));
262 }
263
264 while ($result = $db->fetch_array($search))
265 {
266 $ids[] = $result['bugid'];
267 $results[] = $result;
268 }
269
270 if ($bugsys->userinfo['userid'])
271 {
272 $db->query("
273 REPLACE INTO " . TABLE_PREFIX . "search
274 (userid, dateline, query, ids, orderby, hilight)
275 VALUES
276 (" . $bugsys->userinfo['userid'] . ",
277 " . TIMENOW . ", '" . $bugsys->escape($query) . "',
278 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
279 '" . $bugsys->escape($hilight) . "'
280 )"
281 );
282 }
283
284 $justprocess = true;
285
286 $_REQUEST['do'] = 'results';
287 }
288
289 // ###################################################################
290
291 if ($_REQUEST['do'] == 'search')
292 {
293 if ($bugsys->userinfo['userid'] AND !$bugsys->in['new'])
294 {
295 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid']))
296 {
297 $_REQUEST['do'] = 'results';
298 }
299 else
300 {
301 $newsearch = true;
302 }
303 }
304 else
305 {
306 $newsearch = true;
307 }
308
309 if ($newsearch)
310 {
311 $pcv_select = construct_pcv_select();
312
313 // -------------------------------------------------------------------
314 // custom fields
315 $fields = construct_custom_fields($bug, true);
316 $show['customfields'] = ($fields ? true : false);
317
318 $bugsys->debug('fields modulo: ' . count($fields) % 2);
319 $i = 1;
320 foreach ($fields AS $field)
321 {
322 $bugsys->debug("i = $i");
323 if ($i == 1)
324 {
325 $customfields['right'] .= $field;
326 }
327 else if ($i == 2)
328 {
329 $customfields['left'] .= $field;
330 $i = 0;
331 }
332 $i++;
333 }
334
335 // -------------------------------------------------------------------
336 // built-in fields
337 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, true);
338 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, true);
339 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, true);
340 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, true);
341
342 $select['dev'] = '<option value="0" selected="selected"></option>';
343 foreach ($bugsys->datastore['assignto'] AS $dev)
344 {
345 $value = $dev['userid'];
346 $label = construct_user_display($dev, false);
347 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
348 }
349
350 eval('$template->flush("' . $template->fetch('search') . '");');
351 }
352 }
353
354 // ###################################################################
355
356 if ($_REQUEST['do'] == 'results')
357 {
358 $show['cached'] = false;
359 if ($bugsys->userinfo['userid'] AND !$justprocess)
360 {
361 $search = $cachedsearch;
362 if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
363 {
364 $search = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $search['query']));
365 while ($bug = $db->fetch_array($search))
366 {
367 $ids[] = $bug['bugid'];
368 $results[] = $bug;
369 }
370 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . " WHERE userid = " . $bugsys->userinfo['userid']);
371 }
372 else
373 {
374 $search = $db->query("
375 SELECT bug.*, user1.displayname AS firstreport,
376 user2.displayname AS lastpost
377 FROM " . TABLE_PREFIX . "bug AS bug
378 LEFT JOIN " . TABLE_PREFIX . "user AS user1
379 ON (bug.userid = user1.userid)
380 LEFT JOIN " . TABLE_PREFIX . "user AS user2
381 ON (bug.lastpostby = user2.userid)
382 WHERE bug.bugid IN ($search[ids])
383 $search[orderby]"
384 );
385 while ($bug = $db->fetch_array($search))
386 {
387 $results[] = $bug;
388 }
389 }
390 $show['cached'] = true;
391 $hilight = $search['hilight'];
392 }
393
394 foreach ($results AS $bug)
395 {
396 $funct->exec_swap_bg($stylevar['alt_colour'], '');
397 $bug['bgcolour'] = ($bugsys->userinfo['showcolours'] ? $bugsys->datastore['status']["$bug[status]"]['color'] : $funct->bgcolour);
398 $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
399 $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
400 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
401 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
402 $bug['lastposttime'] = (($bug['hiddendisplay']) ? $bug['hiddenlastposttime'] : $bug['lastposttime']);
403 $bug['lastpost'] = (($bug['hiddendisplay']) ? $bug['hiddenlastpost'] : $bug['lastpost']);
404 $bug['lastposttime'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']);
405 $bug['urladd'] = "&amp;hilight=$hilight";
406 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
407 }
408
409 eval('$template->flush("' . $template->fetch('search_results') . '");');
410 }
411
412 /*=====================================================================*\
413 || ###################################################################
414 || # $HeadURL$
415 || # $Id$
416 || ###################################################################
417 \*=====================================================================*/
418 ?>