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