r1470: Fix two bugs with searching that would:
[bugdar.git] / search.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright 2002-[#]year[#] Blue Static
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 'search_update',
26 'search_save',
27 'trackerhome_bits',
28 'list_head',
29 'pagenav_bit',
30 'pagenav'
31 );
32
33 define('SVN', '$Id$');
34
35 $focus['search'] = 'focus';
36
37 require_once('./global.php');
38 require_once('./includes/functions_product.php');
39 require_once('./includes/class_sort.php');
40 require_once('./includes/class_logging.php');
41 require_once('./includes/api_bug.php');
42 require_once('./includes/class_api_error.php');
43
44 APIError(array($message, 'error'));
45
46 if (!can_perform('cansearch'))
47 {
48 $message->errorPermission();
49 }
50
51 define('MODE_ANY', 1);
52 define('MODE_ALL', 2);
53 define('MODE_RAW', 3);
54
55 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
56 define('SEARCH_WORD_MIN', $var['Value']);
57
58 $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = 0 AND dateline < " . (TIMENOW - 3600));
59
60 $show['search'] = true;
61
62 // ###################################################################
63
64 if (empty($_REQUEST['do']))
65 {
66 $_REQUEST['do'] = 'search';
67 }
68
69 // ###################################################################
70
71 if ($_REQUEST['do'] == 'search')
72 {
73 if ($bugsys->in['new'])
74 {
75 $newsearch = true;
76 }
77 else if ($bugsys->in['searchid'])
78 {
79 $cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
80 }
81 else if ($bugsys->userinfo['userid'])
82 {
83 $cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . $bugsys->userinfo['userid']);
84 }
85 else
86 {
87 $newsearch = true;
88 }
89
90 if ($cachedsearch)
91 {
92 $show['cached'] = true;
93 if ($cachedsearch['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
94 {
95 $_REQUEST['do'] = 'process';
96 $bugsys->in = array_merge(unserialize($cachedsearch['query']), $bugsys->in);
97 $bugsys->debug('rerunning the search');
98 }
99 else
100 {
101 $search = $cachedsearch;
102 $_POST['do'] = 'results';
103 }
104 }
105 else
106 {
107 $newsearch = true;
108 }
109
110 if ($newsearch)
111 {
112 if (!is_array($bugsys->datastore['product']))
113 {
114 $message->error(_('No products are setup, therefore there can be no bugs and thus search cannot function.'));
115 }
116
117 if (!is_array($bugsys->datastore['version']))
118 {
119 $message->error(_('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
120 }
121
122 $productSelect = ConstructProductSelect();
123
124 // -------------------------------------------------------------------
125 // custom fields
126 $fields = construct_custom_fields(null, true, false, true);
127 $i = 0;
128 foreach ($fields AS $field)
129 {
130 if ($i % 2 == 0)
131 {
132 $customfields['left'] .= $field;
133 }
134 else
135 {
136 $customfields['right'] .= $field;
137 }
138 $i++;
139 }
140
141 // -------------------------------------------------------------------
142 // built-in fields
143 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
144 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
145 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
146 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
147
148 $searches = '';
149 if ($bugsys->userinfo['userid'])
150 {
151 $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NOT NULL AND userid = " . $bugsys->userinfo['userid']);
152 while ($search = $db->fetch_array($searchesFetch))
153 {
154 $value = $search['searchid'];
155 $label = $search['name'];
156 eval('$searches .= "' . $template->fetch('selectoption') . '";');
157 }
158 }
159
160 $select['dev'] = '';
161 foreach ($bugsys->datastore['assignto'] AS $dev)
162 {
163 $value = $dev['userid'];
164 $label = construct_user_display($dev, false);
165 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
166 }
167
168 eval('$template->flush("' . $template->fetch('search') . '");');
169 }
170 }
171
172 // ###################################################################
173
174 if ($_REQUEST['do'] == 'process')
175 {
176 // -------------------------------------------------------------------
177 // handle keywords
178 if ($bugsys->in['summary'])
179 {
180 $keywords = preg_split('#\s+#', $bugsys->in['summary']);
181
182 // TODO - need to have some str to bool conversions
183
184 foreach ($keywords AS $word)
185 {
186 if (strlen($word) < SEARCH_WORD_MIN)
187 {
188 continue;
189 }
190
191 if ($bugsys->in['mode'] == MODE_ALL)
192 {
193 $querybuild['text'] .= " +$word";
194 }
195 else
196 {
197 $querybuild['text'] .= " $word";
198 }
199
200 if (!preg_match('#-(.+?)#', trim($word)))
201 {
202 $hilight .= " $word";
203 }
204 }
205
206 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
207 $hilight = trim($hilight);
208 $hilight = preg_replace('#\s#', '+', $hilight);
209
210 $temp = trim($querybuild['text']);
211
212 if ($bugsys->in['mode'] == MODE_ALL OR $bugsys->in['mode'] == MODE_RAW)
213 {
214 $bool_flag = ' IN BOOLEAN MODE';
215 }
216
217 $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)";
218 }
219
220 // -------------------------------------------------------------------
221 // reporter
222 if ($bugsys->in['reporter'])
223 {
224 // force email or name?? make a distinction?
225 // more elegant way to do this? probably
226 $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')) . "%'");
227 if ($user['userid'])
228 {
229 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
230 }
231 }
232
233 // -------------------------------------------------------------------
234 // product/component/version stuff
235 if (is_array($bugsys->in['product']))
236 {
237 foreach ($bugsys->in['product'] AS $prod)
238 {
239 $product = explode(',', $prod);
240 $product = $bugsys->clean($product, TYPE_UINT);
241 $products[] = $product[0];
242 $components[] = $product[1];
243 $versions[] = $product[2];
244 }
245 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
246 }
247
248 // -------------------------------------------------------------------
249 // severity, priority, status, resolution, assignedto
250
251 // severity
252 if ($bugsys->in['severity'])
253 {
254 $bugsys->input_clean('severity', TYPE_UINT);
255 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in['severity']) . ")";
256 }
257
258 // priority
259 if ($bugsys->in['priority'])
260 {
261 $bugsys->input_clean('priority', TYPE_UINT);
262 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in['priority']) . ")";
263 }
264
265 // status
266 if ($bugsys->in['status'])
267 {
268 $bugsys->input_clean('status', TYPE_UINT);
269 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in['status']) . ")";
270 }
271
272 // resolution
273 if ($bugsys->in['resolution'])
274 {
275 $bugsys->input_clean('resolution', TYPE_UINT);
276 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in['resolution']) . ")";
277 }
278
279 // assignment
280 if ($bugsys->in['assignedto'])
281 {
282 $bugsys->input_clean('assignedto', TYPE_UINT);
283 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in['assignedto']) . ")";
284 }
285
286 // -------------------------------------------------------------------
287 // date
288 if ($bugsys->in['date'])
289 {
290 // now - (seconds/day * number of days)
291 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT) * 3600);
292 $querybuild['date'] = "AND bug.dateline >= $dateline";
293 }
294
295 // -------------------------------------------------------------------
296 // favorites
297 $bugsys->input_clean('favorite', TYPE_INT);
298 if ($bugsys->in['favorite'] != 0 AND $bugsys->userinfo['userid'])
299 {
300 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favorite WHERE userid = " . $bugsys->userinfo['userid']);
301 while ($favorite = $db->fetch_array($favorites))
302 {
303 $ids[] = $favorite['bugid'];
304 }
305 $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in['favorite'] > 0 ? "IN" : "NOT IN") . " (" . implode(', ', $ids) . ")";
306 }
307
308 // -------------------------------------------------------------------
309 // sort by
310 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
311 $orderby = array('ASC', 'DESC');
312 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
313 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
314 {
315 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
316 }
317 else if ($bugsys->in['sortby'] == 'relevance')
318 {
319 $sortclause = '';
320 }
321 else
322 {
323 $sortclause = '';
324 }
325
326 // -------------------------------------------------------------------
327 // custom fields
328 $fields_fetch = $bugsys->db->query("
329 SELECT bugfield.*
330 FROM " . TABLE_PREFIX . "bugfield AS bugfield
331 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
332 ON (bugfield.fieldid = permission.fieldid)
333 WHERE permission.mask <> 0
334 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
335 AND bugfield.cansearch = 1"
336 );
337 while ($field = $bugsys->db->fetch_array($fields_fetch))
338 {
339 if (!empty($bugsys->in["custom$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["custom$field[fieldid]"])))
340 {
341 if ($field['type'] == 'input_checkbox' AND $bugsys->input_clean("custom$field[fieldid]", TYPE_INT) != 0)
342 {
343 $querybuild[] = "AND bug.custom$field[fieldid] = " . ($bugsys->in["custom$field[fieldid]"] > 0 ? 1 : 0);
344 }
345 else if ($field['type'] == 'input_text')
346 {
347 $querybuild[] = "AND bug.custom$field[fieldid] LIKE '%" . $bugsys->in["custom$field[fieldid]"] . "%'";
348 }
349 else if ($field['type'] == 'select_single' AND $bugsys->in["custom$field[fieldid]"] != -1)
350 {
351 $temp = unserialize($field['selects']);
352 $querybuild[] = "AND bug.custom$field[fieldid] = '" . trim($temp[ intval($bugsys->in["custom$field[fieldid]"]) ]) . "'";
353 }
354 }
355 }
356
357 // -------------------------------------------------------------------
358 // have to search something
359 if (sizeof($querybuild) < 1)
360 {
361 $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));
362 }
363
364 // -------------------------------------------------------------------
365 // do the search
366
367 $search = $db->query("
368 SELECT bug.*, comment.commentid
369 FROM " . TABLE_PREFIX . "bug AS bug
370 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
371 ON (bug.bugid = comment.bugid)
372 WHERE bug.bugid <> 0
373 AND bug.product IN (" . fetch_on_bits('canviewbugs') . ")
374 AND
375 (
376 !bug.hidden
377 OR
378 (bug.hidden AND bug.product IN (" . fetch_on_bits('canviewhidden') . "))" . (can_perform('canviewownhidden') ? "
379 OR
380 (bug.hidden AND bug.userid = " . $bugsys->userinfo['userid'] . " AND bug.product IN (" . fetch_on_bits('canviewonhidden') . "))" : "") . "
381 )
382 " . implode("\n\t\t", $querybuild) . "
383 GROUP BY bug.bugid
384 $sortclause
385 ");
386
387 $numrows = $db->num_rows($search);
388
389 if ($numrows < 1)
390 {
391 $message->error(_('No search results were returned that matched your criteria.'));
392 }
393
394 while ($result = $db->fetch_array($search))
395 {
396 $ids[] = $result['bugid'];
397 $results[] = $result;
398 }
399
400 if ($bugsys->userinfo['userid'] AND !$cachedsearch AND !$bugsys->in['rerun'])
401 {
402 $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid'] . " AND name IS NULL");
403 }
404
405 // store the search params
406 $params = $bugsys->in;
407 foreach ($_COOKIE AS $key => $value)
408 {
409 unset($params["$key"]);
410 }
411
412 if ($cachedsearch)
413 {
414 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', resultcount = " . sizeof($results) . ", dateline = " . TIMENOW . " WHERE searchid = " . $cachedsearch['searchid']);
415 $search = $cachedsearch;
416 }
417 else
418 {
419 $db->query("
420 INSERT INTO " . TABLE_PREFIX . "search
421 (userid, dateline, query, ids, orderby, hilight, resultcount)
422 VALUES
423 (" . $bugsys->userinfo['userid'] . ",
424 " . TIMENOW . ", '" . $bugsys->escape(serialize($params)) . "',
425 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
426 '" . $bugsys->escape($hilight) . "',
427 " . sizeof($results) . "
428 )"
429 );
430 $search = array('searchid' => $db->insert_id(), 'ids' => implode(',', $ids), 'orderby' => $sortclause, 'hilight' => $hilight, 'resultcount' => sizeof($results));
431 }
432
433 $_POST['do'] = 'results';
434 }
435
436 // ###################################################################
437
438 if ($_REQUEST['do'] == 'update')
439 {
440 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
441 if (!$search OR !can_perform('caneditother'))
442 {
443 $message->errorPermission();
444 }
445
446 $productSelect = ConstructProductSelect();
447
448 $show['update'] = true;
449
450 // -------------------------------------------------------------------
451 // custom fields
452 $fields = construct_custom_fields(null, true, false, true);
453 $i = 0;
454 foreach ($fields AS $field)
455 {
456 if ($i % 2 == 0)
457 {
458 $customfields['left'] .= $field;
459 }
460 else
461 {
462 $customfields['right'] .= $field;
463 }
464 $i++;
465 }
466
467 // -------------------------------------------------------------------
468 // built-in fields
469 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, 0);
470 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, 0);
471 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, 0);
472 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, 0);
473
474 $select['dev'] = '';
475 $value = '0';
476 $label = '';
477 $selected = true;
478 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
479 $selected = false;
480 foreach ($bugsys->datastore['assignto'] AS $dev)
481 {
482 $value = $dev['userid'];
483 $label = construct_user_display($dev, false);
484 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
485 }
486
487 eval('$template->flush("' . $template->fetch('search_update') . '");');
488 }
489
490 // ###################################################################
491
492 if ($_POST['do'] == 'doupdate')
493 {
494 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
495 if (!$search OR !can_perform('caneditother'))
496 {
497 $message->errorPermission();
498 }
499
500 // find all the bugs that we can edit
501 $bugs = $db->query("
502 SELECT * FROM " . TABLE_PREFIX . "bug
503 WHERE bugid IN ($search[ids])
504 AND product IN (" . fetch_on_bits('canviewbugs') . ")
505 AND
506 (
507 product IN (" . fetch_on_bits('caneditother') . ")
508 OR
509 (userid = " . $bugsys->userinfo['userid'] . " AND product IN (" . fetch_on_bits('caneditown') . "))
510 )
511 ");
512 while ($bug = $db->fetch_array($bugs))
513 {
514 if (!((can_perform('caneditown', $bug['product']) AND $bugsys->userinfo['userid'] == $bug['userid']) OR (can_perform('caneditother', $bug['product']) AND $bugsys->userinfo['userid'] != $bug['userid'])) AND !can_perform('canpostcomments', $bug['product']))
515 {
516 continue;
517 }
518
519 $api = new BugApi($bugsys);
520 $api->set('bugid', $bug['bugid']);
521 $api->set_condition();
522 $api->values = $bug;
523
524 $log = new Logging();
525 $log->set_bugid($bug['bugid']);
526 $log->add_data(true, $bug, $log->getCommonFields(), true);
527
528 if ($bugsys->in['status'] AND can_perform('canchangestatus', $bug['product']))
529 {
530 $api->set('status', $bugsys->in['status']);
531 }
532 if ($bugsys->in['priority'] AND can_perform('canchangestatus', $bug['product']))
533 {
534 $api->set('priority', $bugsys->in['priority']);
535 }
536 if ($bugsys->in['severity'])
537 {
538 $api->set('severity', $bugsys->in['severity']);
539 }
540 if ($bugsys->in['resolution'] AND can_perform('canchangestatus', $bug['product']))
541 {
542 $api->set('resolution', $bugsys->in['resolution']);
543 }
544 if ($bugsys->in['assignedto'] AND can_perform('canassign', $bug['product']))
545 {
546 $api->set('assignedto', $bugsys->in['assignedto']);
547 }
548 if ($bugsys->in['product'])
549 {
550 $product = explode(',', $bugsys->in['product']);
551 $api->set('product', $product[0]);
552 $api->set('component', $product[1]);
553 $api->set('version', $product[2]);
554 }
555
556 process_custom_fields($api, $message, false, true);
557
558 $log->add_data(false, $api->values, $log->getCommonFields(), true);
559
560 $api->update();
561 $log->update_history();
562 }
563
564 $message->redirect(_('The specified bugs have been updated and you will now return to your search results.'), 'search.php?searchid=' . $bugsys->in['searchid']);
565 }
566
567 // ###################################################################
568
569 if ($_REQUEST['do'] == 'export')
570 {
571 if (!$bugsys->in['searchid'] AND $bugsys->userinfo['userid'])
572 {
573 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . $bugsys->userinfo['userid']);
574 }
575 else if ($bugsys->in['searchid'])
576 {
577 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
578 }
579 else
580 {
581 $message->error(_('The search results are trying to export are invalid. Please start over <a href="search.php?new=1">here</a> and try again.'));
582 }
583
584 if (!$search)
585 {
586 $message->error(_('Your search has expired because it is older than one hour. Please start over <a href="search.php?new=1">here</a>.'));
587 }
588
589 $bugs = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby]");
590
591 $xml = '<?xml version="1.0" encoding="' . $language['charset'] . '"?>
592
593 <bugdarExport user="' . $bugsys->unsanitize(construct_user_display($bugsys->userinfo, false)) . '" date="' . $datef->format('r', TIMENOW) . '" searchid="' . $search['searchid'] . '">';
594
595 while ($bug = $db->fetch_array($bugs))
596 {
597 $xml .= "\n\t<bug>";
598
599 ProcessBugDataForDisplay(&$bug);
600
601 $xml .= "\n\t\t<id>" . $bug['bugid'] . "</id>";
602 $xml .= "\n\t\t<dateReported>" . $datef->format('r', $bug['dateline']) . "</dateReported>";
603 if ($bug['userid'])
604 {
605 $xml .= "\n\t\t<reporter>" . construct_user_display($db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = $bug[userid]"), false) . "</reporter>";
606 }
607 $xml .= "\n\t\t<summary>" . $bug['summary'] . "</summary>";
608 $xml .= "\n\t\t<product>" . $bug['product'] . "</product>";
609 if ($bug['component'])
610 {
611 $xml .= "\n\t\t<component>" . $bug['component'] . "</component>";
612 }
613 $xml .= "\n\t\t<version>" . $bug['version'] . "</version>";
614 $xml .= "\n\t\t<status>" . $bug['status'] . "</status>";
615 if ($bug['assignedto'])
616 {
617 $xml .= "\n\t\t<assignedTo>" . construct_user_display($bugsys->datastore['assignto']["$bug[assignedto]"], false) . "</assignedTo>";
618 }
619 $xml .= "\n\t\t<resolution>" . $bug['resolution'] . "</resolution>";
620 $xml .= "\n\t\t<severity>" . $bug['severity'] . "</severity>";
621 $xml .= "\n\t\t<priority>" . $bug['priority'] . "</priority>";
622
623 $xml .= "\n\t</bug>";
624 }
625
626 $xml .= "\n</bugdarExport>";
627
628 $funct->download_file($xml, _('bugdar-search-' . $search['searchid'] . '.xml'), true);
629 }
630
631 // ###################################################################
632
633 if ($_POST['do'] == 'dosave')
634 {
635 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
636 if (!$search)
637 {
638 $message->addError(_('The search does not exist.'));
639 }
640 if ($search['name'])
641 {
642 $message->addError(sprintf(_('This search has already been named "%1$s".'), $search['name']));
643 }
644 if (empty($bugsys->in['name']))
645 {
646 $message->addError(_('The name cannot be empty.'));
647 }
648
649 if (!$message->hasErrors())
650 {
651 $db->query("UPDATE " . TABLE_PREFIX . "search SET name = '" . $bugsys->input_escape('name') . "' WHERE searchid = " . $bugsys->in['searchid']);
652 $message->redirect(_('Your search has been saved.'), 'search.php?searchid=' . $search['searchid']);
653 }
654 else
655 {
656 $_REQUEST['do'] = 'save';
657 $show['errors'] = true;
658 }
659 }
660
661 // ###################################################################
662
663 if ($_REQUEST['do'] == 'save')
664 {
665 if (!$bugsys->userinfo['userid'])
666 {
667 $message->errorPermission();
668 }
669
670 eval('$template->flush("' . $template->fetch('search_save') . '");');
671 }
672
673 // ###################################################################
674
675 if ($_POST['do'] == 'results')
676 {
677 if (!$search['ids'])
678 {
679 $message->error(_('No bugs matched your search criteria. Please <a href="search.php?new=1">try again</a> with different search requirements.'));
680 }
681
682 $searchid = $search['searchid'];
683 $hilight = $search['hilight'];
684
685 LoadPaginationFramework();
686 $pagination->setTotal($search['resultcount']);
687 $pagination->splitPages();
688
689 $sort = new ListSorter('search');
690
691 $show['save'] = ($bugsys->userinfo['userid'] AND !$search['name']);
692 $show['update'] = can_perform('caneditother');
693
694 $bugs = '';
695 $search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
696 while ($bug = $db->fetch_array($search))
697 {
698 $funct->exec_swap_bg($stylevar['alt_color'], '');
699 ProcessBugDataForDisplay(&$bug, $funct->bgcolour);
700 $bugs .= $sort->constructRow($bug, "&amp;hilight=$hilight");
701 }
702
703 $columnHeads = $sort->constructColumnHeaders(false);
704 $show['pagenav'] = ($pagination->getPageCount() > 1);
705 $pagenav = $pagination->constructPageNav('search.php?searchid=' . $searchid);
706
707 eval('$template->flush("' . $template->fetch('search_results') . '");');
708 }
709
710 /*=====================================================================*\
711 || ###################################################################
712 || # $HeadURL$
713 || # $Id$
714 || ###################################################################
715 \*=====================================================================*/
716 ?>