r958: Adding pagination to search results
[bugdar.git] / search.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 $fetchtemplates = array(
23 'search',
24 'search_results',
25 'trackerhome_bits'
26 );
27
28 define('SVN', '$Id$');
29
30 $focus['search'] = 'focus';
31
32 require_once('./global.php');
33 require_once('./includes/functions_product.php');
34 require_once('./includes/class_pagination.php');
35
36 if (!can_perform('cansearch'))
37 {
38 $message->error_permission();
39 }
40
41 define('MODE_ANY', ($bugsys->in['mode'] == 1));
42 define('MODE_ALL', ($bugsys->in['mode'] == 2));
43 define('MODE_RAW', ($bugsys->in['mode'] == 3));
44
45 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
46 define('SEARCH_WORD_MIN', $var['Value']);
47
48 $show['search'] = true;
49
50 // ###################################################################
51
52 if (empty($_REQUEST['do']))
53 {
54 $_REQUEST['do'] = 'search';
55 }
56
57 // ###################################################################
58
59 if ($_REQUEST['do'] == 'process')
60 {
61 // -------------------------------------------------------------------
62 // handle keywords
63 if ($bugsys->in['summary'])
64 {
65 $keywords = preg_split('#\s+#', $bugsys->in['summary']);
66
67 // #*# need to have some str to bool conversions
68
69 foreach ($keywords AS $word)
70 {
71 if (strlen($word) < SEARCH_WORD_MIN)
72 {
73 continue;
74 }
75
76 if (MODE_ALL)
77 {
78 $querybuild['text'] .= " +$word";
79 }
80 else
81 {
82 $querybuild['text'] .= " $word";
83 }
84
85 if (!preg_match('#-(.+?)#', trim($word)))
86 {
87 $hilight .= " $word";
88 }
89 }
90
91 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
92 $hilight = trim($hilight);
93 $hilight = preg_replace('#\s#', '+', $hilight);
94
95 $temp = trim($querybuild['text']);
96
97 if (MODE_ALL OR MODE_RAW)
98 {
99 $bool_flag = ' IN BOOLEAN MODE';
100 }
101
102 $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)";
103 }
104
105 // -------------------------------------------------------------------
106 // reporter
107 if ($bugsys->in['reporter'])
108 {
109 // force email or name?? make a distinction?
110 // more elegant way to do this? probably
111 $user = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email LIKE '%" . str_replace('%', '\%', $bugsys->in['reporter']) . "' OR displayname LIKE '%" . str_replace('%', '\%', $bugsys->in['reporter']) . "%'");
112 if ($user['userid'])
113 {
114 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
115 }
116 }
117
118 // -------------------------------------------------------------------
119 // product/component/version stuff
120 if (is_array($bugsys->in['pcv_select']))
121 {
122 foreach ($bugsys->in['pcv_select'] AS $pcv)
123 {
124 $pcv = parse_pcv_select($pcv, true);
125 $products[] = $pcv['product'];
126 $components[] = $pcv['component'];
127 $versions[] = $pcv['version'];
128 }
129 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
130 }
131
132 // -------------------------------------------------------------------
133 // severity, priority, status, resolution, assignedto
134
135 // severity
136 if ($bugsys->in['severity'])
137 {
138 $bugsys->input_clean('severity', TYPE_UINT);
139 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in['severity']) . ")";
140 }
141
142 // priority
143 if ($bugsys->in['priority'])
144 {
145 $bugsys->input_clean('priority', TYPE_UINT);
146 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in['priority']) . ")";
147 }
148
149 // status
150 if ($bugsys->in['status'])
151 {
152 $bugsys->input_clean('status', TYPE_UINT);
153 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in['status']) . ")";
154 }
155
156 // resolution
157 if ($bugsys->in['resolution'])
158 {
159 $bugsys->input_clean('resolution', TYPE_UINT);
160 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in['resolution']) . ")";
161 }
162
163 // assignment
164 if ($bugsys->in['assignedto'])
165 {
166 $bugsys->input_clean('assignedto', TYPE_UINT);
167 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in['assignedto']) . ")";
168 }
169
170 // -------------------------------------------------------------------
171 // date
172 if ($bugsys->in['date'])
173 {
174 // now - (seconds/day * number of days)
175 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT) * 3600);
176 $querybuild['date'] = "AND bug.dateline >= $dateline";
177 }
178
179 // -------------------------------------------------------------------
180 // favourites
181 if (($bugsys->in['favourite'] == 1 OR $bugsys->in['favourite'] == 0) AND $bugsys->userinfo['userid'])
182 {
183 $favourites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $bugsys->userinfo['userid']);
184 while ($favourite = $db->fetch_array($favourites))
185 {
186 $ids[] = $favourite['bugid'];
187 }
188 $querybuild['favourites'] = "AND bug.bugid " . ($bugsys->in['favourite'] == 0 ? "NOT IN" : "IN") . " (" . implode(', ', $ids) . ")";
189 }
190
191 // -------------------------------------------------------------------
192 // sort by
193 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
194 $orderby = array('ASC', 'DESC');
195 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
196 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
197 {
198 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
199 }
200 else if ($bugsys->in['sortby'] == 'relevance')
201 {
202 $sortclause = '';
203 }
204 else
205 {
206 $sortclause = '';
207 }
208
209 // -------------------------------------------------------------------
210 // custom fields
211 $fields_fetch = $bugsys->db->query("
212 SELECT bugfield.*
213 FROM " . TABLE_PREFIX . "bugfield AS bugfield
214 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
215 ON (bugfield.fieldid = permission.fieldid)
216 WHERE permission.mask <> 0
217 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
218 AND bugfield.cansearch = 1"
219 );
220 while ($field = $bugsys->db->fetch_array($fields_fetch))
221 {
222 if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"])))
223 {
224 if ($field['type'] == 'input_checkbox')
225 {
226 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] == 1) ? 0 : 1);
227 }
228 else if ($field['type'] == 'input_text')
229 {
230 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'";
231 }
232 else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1)
233 {
234 $temp = unserialize($field['selects']);
235 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'";
236 }
237 }
238 }
239
240 // -------------------------------------------------------------------
241 // have to search something
242 if (sizeof($querybuild) < 1)
243 {
244 $message->error(sprintf($lang->string('You have to enter some criteria to search for. Node that words less than %1$d characters are ignored by the search engine (and some other very common words, too).'), SEARCH_WORD_MIN));
245 }
246
247 // -------------------------------------------------------------------
248 // do the search
249 $query = "
250 SELECT bug.*, comment.commentid
251 FROM " . TABLE_PREFIX . "bug AS bug
252 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
253 ON (bug.bugid = comment.bugid)
254 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
255 ON (bug.bugid = bugfieldvalue.bugid)
256 WHERE bug.bugid <> 0
257 AND bug.product IN (#<'ONBITS:VIEW'>#)
258 AND (!bug.hidden OR (bug.hidden AND bug.product IN (#<'ONBITS:HIDDEN'>#)))
259 " . implode("\n\t\t", $querybuild) . "
260 GROUP BY bug.bugid
261 $sortclause";
262
263 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $query);
264
265 $search = $db->query($runquery);
266
267 $numrows = $db->num_rows($search);
268
269 if ($numrows < 1)
270 {
271 $message->error($lang->string('No search results were returned that matched your criteria.'));
272 }
273
274 while ($result = $db->fetch_array($search))
275 {
276 $ids[] = $result['bugid'];
277 $results[] = $result;
278 }
279
280 if ($bugsys->userinfo['userid'])
281 {
282 $db->query("
283 REPLACE INTO " . TABLE_PREFIX . "search
284 (userid, dateline, query, ids, orderby, hilight, resultcount)
285 VALUES
286 (" . $bugsys->userinfo['userid'] . ",
287 " . TIMENOW . ", '" . $bugsys->escape($query) . "',
288 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
289 '" . $bugsys->escape($hilight) . "',
290 " . sizeof($results) . "
291 )"
292 );
293 }
294
295 $justprocess = true;
296
297 $_REQUEST['do'] = 'results';
298 }
299
300 // ###################################################################
301
302 if ($_REQUEST['do'] == 'search')
303 {
304 if ($bugsys->userinfo['userid'] AND !$bugsys->in['new'])
305 {
306 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid']))
307 {
308 $_REQUEST['do'] = 'results';
309 }
310 else
311 {
312 $newsearch = true;
313 }
314 }
315 else
316 {
317 $newsearch = true;
318 }
319
320 if ($newsearch)
321 {
322 $pcv_select = construct_pcv_select();
323
324 // -------------------------------------------------------------------
325 // custom fields
326 $fields = construct_custom_fields(null, true);
327 $i = 0;
328 foreach ($fields AS $field)
329 {
330 if ($i % 2 == 0)
331 {
332 $customfields['left'] .= $field;
333 }
334 else
335 {
336 $customfields['right'] .= $field;
337 }
338 $i++;
339 }
340
341 // -------------------------------------------------------------------
342 // built-in fields
343 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
344 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
345 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
346 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
347
348 $select['dev'] = '';
349 foreach ($bugsys->datastore['assignto'] AS $dev)
350 {
351 $value = $dev['userid'];
352 $label = construct_user_display($dev, false);
353 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
354 }
355
356 eval('$template->flush("' . $template->fetch('search') . '");');
357 }
358 }
359
360 // ###################################################################
361
362 if ($_REQUEST['do'] == 'results')
363 {
364 $show['cached'] = false;
365 if ($bugsys->userinfo['userid'] AND !$justprocess)
366 {
367 $search = $cachedsearch;
368 if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
369 {
370 $research = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $search['query']));
371 while ($bug = $db->fetch_array($research))
372 {
373 $ids[] = $bug['bugid'];
374 $results[] = $bug;
375 }
376 $search['ids'] = implode(',', $ids);
377 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . ", resultcount = " . sizeof($results) . " WHERE userid = " . $bugsys->userinfo['userid']);
378 }
379 $show['cached'] = true;
380 $hilight = $search['hilight'];
381 }
382
383 $pagination = new Pagination('p', 'pp');
384 $pagination->total = $search['resultcount'];
385 $pagination->split_pages();
386
387 $search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetch_limit($pagination->page - 1) . ", " . $pagination->perpage);
388 while ($bug = $db->fetch_array($search))
389 {
390 $funct->exec_swap_bg($stylevar['alt_colour'], '');
391 $bug['bgcolour'] = ($bugsys->userinfo['showcolours'] ? $bugsys->datastore['status']["$bug[status]"]['color'] : $funct->bgcolour);
392 $bug['product'] = $bugsys->datastore['product']["$bug[product]"]['title'];
393 $bug['version'] = $bugsys->datastore['version']["$bug[version]"]['version'];
394 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
395 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
396 $bug['priority'] = $bugsys->datastore['priority']["$bug[priority]"]['priority'];
397 $bug['severity'] = $bugsys->datastore['severity']["$bug[severity]"]['severity'];
398 $bug['lastposttime'] = ($bug['hiddendisplay'] ? $bug['hiddenlastposttime'] : $bug['lastposttime']);
399 $bug['lastpost'] = ($bug['hiddendisplay'] ? $bug['hiddenlastpostbyname'] : $bug['lastpostbyname']);
400 $bug['lastposttime'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']);
401 $bug['urladd'] = "&amp;hilight=$hilight";
402 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
403 }
404
405 $show['pagenav'] = ($pagination->pagecount > 1 ? true : false);
406 $pagenav = $pagination->construct_page_nav('search.php');
407
408 eval('$template->flush("' . $template->fetch('search_results') . '");');
409 }
410
411 /*=====================================================================*\
412 || ###################################################################
413 || # $HeadURL$
414 || # $Id$
415 || ###################################################################
416 \*=====================================================================*/
417 ?>