2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright 2002-[#]year[#] Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
22 $fetchtemplates = array(
30 define('SVN', '$Id$');
32 $focus['search'] = 'focus';
34 require_once('./global.php');
35 require_once('./includes/functions_product.php');
37 if (!can_perform('cansearch'))
39 $message->error_permission();
42 define('MODE_ANY', ($bugsys->in
['mode'] == 1));
43 define('MODE_ALL', ($bugsys->in
['mode'] == 2));
44 define('MODE_RAW', ($bugsys->in
['mode'] == 3));
46 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
47 define('SEARCH_WORD_MIN', $var['Value']);
49 $show['search'] = true
;
51 // ###################################################################
53 if (empty($_REQUEST['do']))
55 $_REQUEST['do'] = 'search';
58 // ###################################################################
60 if ($_REQUEST['do'] == 'process')
62 // -------------------------------------------------------------------
64 if ($bugsys->in
['summary'])
66 $keywords = preg_split('#\s+#', $bugsys->in
['summary']);
68 // TODO - need to have some str to bool conversions
70 foreach ($keywords AS $word)
72 if (strlen($word) < SEARCH_WORD_MIN
)
79 $querybuild['text'] .= " +$word";
83 $querybuild['text'] .= " $word";
86 if (!preg_match('#-(.+?)#', trim($word)))
92 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
93 $hilight = trim($hilight);
94 $hilight = preg_replace('#\s#', '+', $hilight);
96 $temp = trim($querybuild['text']);
98 if (MODE_ALL
OR MODE_RAW
)
100 $bool_flag = ' IN BOOLEAN MODE';
103 $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)";
106 // -------------------------------------------------------------------
108 if ($bugsys->in
['reporter'])
110 // force email or name?? make a distinction?
111 // more elegant way to do this? probably
112 $user = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE email LIKE '%" . str_replace('%', '\%', $bugsys->input_escape('reporter')) . "' OR displayname LIKE '%" . str_replace('%', '\%', $bugsys->input_escape('reporter')) . "%'");
115 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
119 // -------------------------------------------------------------------
120 // product/component/version stuff
121 if (is_array($bugsys->in
['pcv_select']))
123 foreach ($bugsys->in
['pcv_select'] AS $pcv)
125 $pcv = parse_pcv_select($pcv, true
);
126 $products[] = $pcv['product'];
127 $components[] = $pcv['component'];
128 $versions[] = $pcv['version'];
130 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
133 // -------------------------------------------------------------------
134 // severity, priority, status, resolution, assignedto
137 if ($bugsys->in
['severity'])
139 $bugsys->input_clean('severity', TYPE_UINT
);
140 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in
['severity']) . ")";
144 if ($bugsys->in
['priority'])
146 $bugsys->input_clean('priority', TYPE_UINT
);
147 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in
['priority']) . ")";
151 if ($bugsys->in
['status'])
153 $bugsys->input_clean('status', TYPE_UINT
);
154 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in
['status']) . ")";
158 if ($bugsys->in
['resolution'])
160 $bugsys->input_clean('resolution', TYPE_UINT
);
161 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in
['resolution']) . ")";
165 if ($bugsys->in
['assignedto'])
167 $bugsys->input_clean('assignedto', TYPE_UINT
);
168 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in
['assignedto']) . ")";
171 // -------------------------------------------------------------------
173 if ($bugsys->in
['date'])
175 // now - (seconds/day * number of days)
176 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT
) * 3600);
177 $querybuild['date'] = "AND bug.dateline >= $dateline";
180 // -------------------------------------------------------------------
182 if (($bugsys->in
['favorite'] == 1 OR $bugsys->in
['favorite'] == 0) AND $bugsys->userinfo
['userid'])
184 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX
. "favorite WHERE userid = " . $bugsys->userinfo
['userid']);
185 while ($favorite = $db->fetch_array($favorites))
187 $ids[] = $favorite['bugid'];
189 $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in
['favorite'] == 0 ?
"NOT IN" : "IN") . " (" . implode(', ', $ids) . ")";
192 // -------------------------------------------------------------------
194 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
195 $orderby = array('ASC', 'DESC');
196 $bugsys->in
['orderby'] = strtoupper($bugsys->in
['orderby']);
197 if (in_array($bugsys->in
['sortby'], $sortby) AND in_array($bugsys->in
['orderby'], $orderby))
199 $sortclause = "ORDER BY " . $bugsys->in
['sortby'] . ' ' . $bugsys->in
['orderby'];
201 else if ($bugsys->in
['sortby'] == 'relevance')
210 // -------------------------------------------------------------------
212 $fields_fetch = $bugsys->db
->query("
214 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
215 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
216 ON (bugfield.fieldid = permission.fieldid)
217 WHERE permission.mask <> 0
218 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
219 AND bugfield.cansearch = 1"
221 while ($field = $bugsys->db
->fetch_array($fields_fetch))
223 if (!empty($bugsys->in
["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in
["field$field[fieldid]"])))
225 if ($field['type'] == 'input_checkbox')
227 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in
["field$field[fieldid]"] == 1) ?
0 : 1);
229 else if ($field['type'] == 'input_text')
231 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in
["field$field[fieldid]"] . "%'";
233 else if ($field['type'] == 'select_single' AND $bugsys->in
["field$field[fieldid]"] != -1)
235 $temp = unserialize($field['selects']);
236 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in
["field$field[fieldid]"]) ]) . "'";
241 // -------------------------------------------------------------------
242 // have to search something
243 if (sizeof($querybuild) < 1)
245 $message->error(sprintf(_('You have to enter some criteria to search for. Note that words less than %1$d characters are ignored by the search engine (and some other very common words, too).'), SEARCH_WORD_MIN
));
248 // -------------------------------------------------------------------
251 SELECT bug.*, comment.commentid
252 FROM " . TABLE_PREFIX
. "bug AS bug
253 LEFT JOIN " . TABLE_PREFIX
. "comment AS comment
254 ON (bug.bugid = comment.bugid)
255 LEFT JOIN " . TABLE_PREFIX
. "bugvaluefill AS bugfieldvalue
256 ON (bug.bugid = bugfieldvalue.bugid)
258 AND bug.product IN (#<'ONBITS:VIEW'>#)
259 AND (!bug.hidden OR (bug.hidden AND bug.product IN (#<'ONBITS:HIDDEN'>#))" . (can_perform('canviewownhidden') ?
" OR (bug.hidden AND bug.userid = " . $bugsys->userinfo
['userid'] . " AND bug.product IN (#<'ONBITS:OWNHIDDEN'>#))" : "") . ")
260 " . implode("\n\t\t", $querybuild) . "
264 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewonhidden')), $query);
266 $search = $db->query($runquery);
268 $numrows = $db->num_rows($search);
272 $message->error(_('No search results were returned that matched your criteria.'));
275 while ($result = $db->fetch_array($search))
277 $ids[] = $result['bugid'];
278 $results[] = $result;
281 if ($bugsys->userinfo
['userid'])
284 REPLACE INTO " . TABLE_PREFIX
. "search
285 (userid, dateline, query, ids, orderby, hilight, resultcount)
287 (" . $bugsys->userinfo
['userid'] . ",
288 " . TIMENOW
. ", '" . $bugsys->escape($query) . "',
289 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
290 '" . $bugsys->escape($hilight) . "',
291 " . sizeof($results) . "
297 $search = array('ids' => implode(',', $ids), 'orderby' => $sortclause);
299 $_REQUEST['do'] = 'results';
302 // ###################################################################
304 if ($_REQUEST['do'] == 'search')
306 if ($bugsys->userinfo
['userid'] AND !$bugsys->in
['new'])
308 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE userid = " . $bugsys->userinfo
['userid']))
310 $_REQUEST['do'] = 'results';
324 if (!is_array($bugsys->datastore
['product']))
326 $message->error(_('No products are setup, therefore there can be no bugs and thus search cannot function.'));
329 if (!is_array($bugsys->datastore
['version']))
331 $message->error(_('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
334 $pcv_select = construct_pcv_select();
336 // -------------------------------------------------------------------
338 $fields = construct_custom_fields(null
, true
);
340 foreach ($fields AS $field)
344 $customfields['left'] .= $field;
348 $customfields['right'] .= $field;
353 // -------------------------------------------------------------------
355 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
356 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
357 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
358 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
361 foreach ($bugsys->datastore
['assignto'] AS $dev)
363 $value = $dev['userid'];
364 $label = construct_user_display($dev, false
);
365 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
368 eval('$template->flush("' . $template->fetch('search') . '");');
372 // ###################################################################
374 if ($_REQUEST['do'] == 'results')
376 $show['cached'] = false
;
377 if ($bugsys->userinfo
['userid'] AND !$justprocess)
379 $search = $cachedsearch;
380 if ($search['dateline'] < TIMENOW
- 900 OR $bugsys->in
['rerun'])
382 $research = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewownhidden')), $search['query']));
383 while ($bug = $db->fetch_array($research))
385 $ids[] = $bug['bugid'];
388 $search['ids'] = implode(',', $ids);
389 $db->query("UPDATE " . TABLE_PREFIX
. "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW
. ", resultcount = " . sizeof($results) . " WHERE userid = " . $bugsys->userinfo
['userid']);
391 $show['cached'] = true
;
392 $hilight = $search['hilight'];
395 LoadPaginationFramework();
396 $pagination->setTotal($search['resultcount']);
397 $pagination->splitPages();
399 $search = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
400 while ($bug = $db->fetch_array($search))
402 $funct->exec_swap_bg($stylevar['alt_color'], '');
403 ProcessBugDataForDisplay(&$bug, $funct->bgcolour
);
404 $bug['urladd'] = "&hilight=$hilight";
405 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
408 $show['pagenav'] = ($pagination->getPageCount() > 1);
409 $pagenav = $pagination->constructPageNav('search.php');
411 eval('$template->flush("' . $template->fetch('search_results') . '");');
414 /*=====================================================================*\
415 || ###################################################################
418 || ###################################################################
419 \*=====================================================================*/