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