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