r172: Removing from our echo; exit; error reporting system to Error::throw()
[bugdar.git] / search.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # BugStrike [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 $fetchtemplates = array(
14 'search',
15 'search_results',
16 //'search_results_bit'
17 );
18
19 require_once('./global.php');
20
21 if (!can_perform('cansearch'))
22 {
23 $error->throw_permission();
24 }
25
26 define('MODE', intval($bugsys->in['mode']));
27 define('MODE_ANY', ((MODE == 1) ? 1 : 0));
28 define('MODE_ALL', ((MODE == 2) ? 1 : 0));
29 define('MODE_RAW', ((MODE == 3) ? 1 : 0));
30
31 define('SEARCH_WORD_MIN', 3);
32
33 // ###################################################################
34
35 if (empty($_REQUEST['do']))
36 {
37 $_REQUEST['do'] = 'search';
38 }
39
40 // ###################################################################
41
42 if ($_REQUEST['do'] == 'results')
43 {
44 // -------------------------------------------------------------------
45 // parse out our product/component/version
46 $pcv = parse_pcv_select($bugsys->in['pcv_select']);
47
48 // -------------------------------------------------------------------
49 // handle keywords
50 if ($bugsys->in['summary'])
51 {
52 $keywords = preg_split('#\s+#', $bugsys->in['summary']);
53
54 // #*# need to have some str to bool conversions
55
56 foreach ($keywords AS $word)
57 {
58 if (strlen($word) < SEARCH_WORD_MIN)
59 {
60 continue;
61 }
62
63 if (MODE_ALL)
64 {
65 $querybuild['text'] .= " +$word";
66 }
67 else
68 {
69 $querybuild['text'] .= " $word";
70 }
71
72 if (!preg_match('#-(.+?)#', trim($word)))
73 {
74 $hilight .= " $word";
75 }
76 }
77
78 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
79 $hilight = trim($hilight);
80 $hilight = preg_replace('#\s#', '+', $hilight);
81
82 $temp = trim($querybuild['text']);
83
84 if (MODE_ALL OR MODE_RAW)
85 {
86 $bool_flag = ' IN BOOLEAN MODE';
87 }
88
89 $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)";
90 }
91
92 // -------------------------------------------------------------------
93 // reporter
94 if ($bugsys->in['reporter'])
95 {
96 // force email or name?? make a distinction?
97 // more elegant way to do this? probably
98 $user = $db->query_first("SELECT * FROM user WHERE email = '" . $bugsys->in['reporter'] . "' OR MATCH (displayname) AGAINST ('" . $bugsys->in['reporter'] . "')");
99 if ($user['userid'])
100 {
101 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
102 }
103 }
104
105 // -------------------------------------------------------------------
106 // product/component/version stuff
107 if (is_array($bugsys->in['pcv']))
108 {
109 $querybuild['pcv'] = "AND bug.productid = $pcv[product] AND bug.componentid = $pcv[component] AND bug.versionid = $pcv[version]";
110 }
111
112 // -------------------------------------------------------------------
113 // severity, priority, status, resolution, assignedto
114
115 // severity
116 if ($bugsys->in['severity'])
117 {
118 $querybuild['severity'] = "AND bug.severity = " . intval($bugsys->in['severity']);
119 }
120
121 // priority
122 if ($bugsys->in['priority'])
123 {
124 $querybuild['priority'] = "AND bug.priority = " . intval($bugsys->in['priority']);
125 }
126
127 // status
128 if ($bugsys->in['status'])
129 {
130 $querybuild['status'] = "AND bug.status = " . intval($bugsys->in['status']);
131 }
132
133 // resolution
134 if ($bugsys->in['resolution'])
135 {
136 $querybuild['resolution'] = "AND bug.resolution = " . intval($bugsys->in['resolution']);
137 }
138
139 // assignment
140 if ($bugsys->in['assignedto'])
141 {
142 $querybuild['assignedto'] = "AND bug.assignedto = " . intval($bugsys->in['assignedto']);
143 }
144
145 // -------------------------------------------------------------------
146 // date
147 if ($bugsys->in['date'])
148 {
149 // now - (seconds/day * number of days)
150 $dateline = time() - (intval($bugsys->in['date']) * 3600);
151 $querybuild['date'] = "AND bug.dateline >= $dateline";
152 }
153
154 // -------------------------------------------------------------------
155 // sort by
156 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
157 $orderby = array('ASC', 'DESC');
158 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
159 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
160 {
161 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
162 }
163 else if ($bugsys->in['sortby'] == 'relevance')
164 {
165 $sortclause = '';
166 }
167 else
168 {
169 $error->throw('bad sort');
170 }
171
172 // -------------------------------------------------------------------
173 // do the search
174 $search = $db->query("
175 SELECT bug.*, comment.commentid,
176 user1.displayname AS firstreport,
177 user2.displayname AS lastpost
178 FROM " . TABLE_PREFIX . "bug AS bug
179 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
180 ON (bug.bugid = comment.bugid)
181 LEFT JOIN user AS user1
182 ON (bug.userid = user1.userid)
183 LEFT JOIN user AS user2
184 ON (bug.lastpostby = user2.userid)
185 WHERE bug.bugid <> 0
186 " . implode("\n\t\t\t", $querybuild) . "
187 $sortclause"
188 );
189
190 $numrows = $db->num_rows($search);
191
192 if ($numrows < 1)
193 {
194 $error->throw('no results found');
195 }
196
197 while ($bug = $db->fetch_array($search))
198 {
199 $bug['bgcolour'] = $bugsys->datastore['status']["$bug[status]"]['color'];
200 $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
201 $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
202 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
203 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
204 $bug['lastpostinfo'] = datelike('standard', $bug['lastposttime']) . ' by ' . $bug['lastpost'];
205 $bug['urladd'] = "&amp;hilight=$hilight";
206 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
207 }
208
209 eval('$template->flush("' . $template->fetch('search_results') . '");');
210
211 // print_r($querybuild);
212 }
213
214 // ###################################################################
215
216 if ($_REQUEST['do'] == 'search')
217 {
218 $pcv_select = construct_pcv_select('radio', '--');
219
220 $blankselect = '<option value="0">- Choose -</option>';
221
222 $select['severity'] = $blankselect;
223 foreach ($bugsys->datastore['severity'] AS $severity)
224 {
225 $value = $severity['severityid'];
226 $label = $severity['severity'];
227 eval('$select[severity] .= "' . $template->fetch('selectoption') . '";');
228 }
229
230 $select['priority'] = $blankselect;
231 foreach ($bugsys->datastore['priority'] AS $priority)
232 {
233 $value = $priority['priorityid'];
234 $label = $priority['priority'];
235 eval('$select[priority] .= "' . $template->fetch('selectoption') . '";');
236 }
237
238 $select['status'] = $blankselect;
239 foreach ($bugsys->datastore['status'] AS $status)
240 {
241 $value = $status['statusid'];
242 $label = $status['status'];
243 eval('$select[status] .= "' . $template->fetch('selectoption') . '";');
244 }
245
246 $select['resolution'] = $blankselect;
247 foreach ($bugsys->datastore['resolution'] AS $resolution)
248 {
249 $value = $resolution['resolutionid'];
250 $label = $resolution['resolution'];
251 eval('$select[resolution] .= "' . $template->fetch('selectoption') . '";');
252 }
253
254 $select['dev'] = $blankselect;
255 foreach ($bugsys->datastore['assignto'] AS $dev)
256 {
257 $value = $dev['userid'];
258 $label = construct_user_display($dev, false);
259 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
260 }
261
262 eval('$template->flush("' . $template->fetch('search') . '");');
263 }
264
265 /*=====================================================================*\
266 || ###################################################################
267 || # $HeadURL$
268 || # $Id$
269 || ###################################################################
270 \*=====================================================================*/
271 ?>