r245: - construct_datastore_select() [includes/functions.php] now has the option...
[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 $message->error_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 $message->error('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) . ((!can_perform('canviewhidden')) ? "
187 AND !bug.hidden
188 AND !comment.hidden" : "") . "
189 $sortclause
190 GROUP BY bug.bugid"
191 );
192
193 $numrows = $db->num_rows($search);
194
195 if ($numrows < 1)
196 {
197 $message->error('no results found');
198 }
199
200 while ($bug = $db->fetch_array($search))
201 {
202 $bug['bgcolour'] = $bugsys->datastore['status']["$bug[status]"]['color'];
203 $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
204 $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
205 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
206 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
207 $bug['lastpostinfo'] = datelike('standard', $bug['lastposttime']) . ' by ' . $bug['lastpost'];
208 $bug['urladd'] = "&amp;hilight=$hilight";
209 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
210 }
211
212 eval('$template->flush("' . $template->fetch('search_results') . '");');
213
214 // print_r($querybuild);
215 }
216
217 // ###################################################################
218
219 if ($_REQUEST['do'] == 'search')
220 {
221 $pcv_select = construct_pcv_select('radio', '--');
222
223 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', $selectedvalue = 0, $includeblank = true);
224 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', $selectedvalue = 0, $includeblank = true);
225 $select['status'] = construct_datastore_select('status', 'status', 'statusid', $selectedvalue = 0, $includeblank = true);
226 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', $selectedvalue = 0, $includeblank = true);
227
228 $select['dev'] = '<option value="0" selected="selected"></option>';
229 foreach ($bugsys->datastore['assignto'] AS $dev)
230 {
231 $value = $dev['userid'];
232 $label = construct_user_display($dev, false);
233 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
234 }
235
236 eval('$template->flush("' . $template->fetch('search') . '");');
237 }
238
239 /*=====================================================================*\
240 || ###################################################################
241 || # $HeadURL$
242 || # $Id$
243 || ###################################################################
244 \*=====================================================================*/
245 ?>