r1100: We need fetch_on_bits() for canviewownhidden in order to have per-product...
[bugdar.git] / includes / class_sort.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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 /**
23 * Bug Listing Sorter
24 *
25 * This class is used to sort bugs based on user-sent options and variables.
26 *
27 * @author Blue Static
28 * @copyright Copyright ©2002 - [#]year[#], Blue Static
29 * @version $Revision$
30 * @package Bugdar
31 *
32 */
33 class ListSorter
34 {
35 /**
36 * Bugsys registry
37 * @var object
38 * @access private
39 */
40 var $registry;
41
42 /**
43 * Page name
44 * @var string
45 * @access public
46 */
47 var $page = '';
48
49 /**
50 * Current sort key
51 * @var string
52 * @access private
53 */
54 var $sortkey = '';
55
56 /**
57 * Current sort direction
58 * @var string
59 * @access private
60 */
61 var $direction = '';
62
63 // ###################################################################
64 /**
65 * Constructor: set the page name
66 *
67 * @access public
68 *
69 * @param string File name without the .php extension
70 */
71 function ListSorter($page)
72 {
73 global $bugsys;
74 $this->registry = $bugsys;
75 $this->page = $page;
76 $this->process_incoming();
77 }
78
79 // ###################################################################
80 /**
81 * Processes the incoming variables and then sets all the sort order
82 * information appropriately
83 *
84 * @access private
85 */
86 function process_incoming()
87 {
88 $this->sortkey = $this->registry->in['by'];
89 if (!$this->fetch_by_text($this->registry->in['by']))
90 {
91 $this->sortkey = (isset($this->registry->userinfo['defaultsortkey']) ? $this->registry->userinfo['defaultsortkey'] : $this->registry->options['defaultsortkey']);
92 }
93
94 $this->direction = $this->registry->in['as'];
95 if (!in_array($this->direction, array('asc', 'desc')))
96 {
97 $this->direction = (isset($this->registry->userinfo['defaultsortas']) ? $this->registry->userinfo['defaultsortas'] : $this->registry->options['defaultsortas']);
98 }
99 }
100
101 // ###################################################################
102 /**
103 * Fetch a SQL query to gather bugs with the sort filters applied
104 *
105 * @access public
106 *
107 * @param string Additional WHERE clauses in an array
108 * @param string A LIMIT clause
109 *
110 * @return string Compiled SQL query
111 */
112 function fetch_sql_query($where = null, $limit = null)
113 {
114 // this WHERE clause is used for all the queries
115 $basewhere = "bug.product IN (" . fetch_on_bits('canviewbugs') . ")
116 AND (!bug.hidden OR (bug.hidden AND bug.product IN (" . fetch_on_bits('canviewhidden') . "))" . (can_perform('canviewownhidden') ? " OR (bug.hidden AND bug.userid = " . $this->registry->userinfo['userid'] . " AND bug.product IN (" . fetch_on_bits('canviewownhidden') . "))" : "") . ")" .
117 (($this->registry->options['hidestatuses'] OR isset($this->registry->userinfo['hidestatuses'])) ? "
118 AND bug.status NOT IN (" . ($this->registry->userinfo['hidestatuses'] != '' ? $this->registry->userinfo['hidestatuses'] : $this->registry->options['hidestatuses']) . ")" : "");
119
120 // remap the sort keys to be actual SQL fields
121 $querykeys = array(
122 'id' => 'bugid',
123 'summary' => 'summary',
124 'reporter' => 'userid',
125 'lastpost' => (can_perform('canviewhidden') ? "lastposttime" : "hiddenlastposttime")
126 );
127
128 switch ($this->sortkey)
129 {
130 case 'id':
131 case 'summary':
132 case 'reporter':
133 case 'lastpost':
134 $query = "
135 SELECT * FROM " . TABLE_PREFIX . "bug AS bug
136 WHERE $basewhere" .
137 (is_array($where) ? "
138 AND " . implode("\nAND ", $where) : "") . "
139 ORDER BY " . $querykeys[ $this->sortkey ] . " " . strtoupper($this->direction) . ($this->sortkey != 'lastpost' ? ", " . $querykeys['lastpost'] . " " . strtoupper($this->direction) : "") . ($limit ? "
140 LIMIT $limit" : "");
141 break;
142 case 'product':
143 case 'status':
144 case 'resolution':
145 case 'priority':
146 case 'severity':
147 $query = "
148 SELECT * FROM " . TABLE_PREFIX . "{$this->sortkey} AS {$this->sortkey}
149 RIGHT JOIN " . TABLE_PREFIX . "bug AS bug
150 ON (bug.{$this->sortkey} = {$this->sortkey}.{$this->sortkey}id)
151 WHERE $basewhere" .
152 (is_array($where) ? "
153 AND " . implode("\nAND ", $where) : "") . "
154 ORDER BY {$this->sortkey}.displayorder " . strtoupper($this->direction) . ", bug.$querykeys[lastpost] " . strtoupper($this->direction) . ($limit ? "
155 LIMIT $limit" : "");
156 break;
157 }
158
159 return $query;
160 }
161
162 // ###################################################################
163 /**
164 * Returns the display text for a given sort order key
165 *
166 * @access public static
167 *
168 * @param string Sort order key, or FALSE for the array
169 *
170 * @return mixed Display text if param is string, or array of all key=>text if param is NULL
171 */
172 function fetch_by_text($key)
173 {
174 global $lang;
175
176 $keys = array(
177 'lastpost' => _('Last Post Time'),
178 'id' => _('Bug ID'),
179 'summary' => _('Summary'),
180 'reporter' => _('Reporter'),
181 'product' => _('Product'),
182 'status' => _('Status'),
183 'resolution' => _('Resolution'),
184 'priority' => _('Priority'),
185 'severity' => _('Severity')
186 );
187
188 if ($key === false)
189 {
190 return $keys;
191 }
192 else
193 {
194 return $keys["$key"];
195 }
196 }
197
198 // ###################################################################
199 /**
200 * Returns the display text for a given sort order direction
201 *
202 * @access public static
203 *
204 * @param string Sort direction, or FALSE for the array
205 *
206 * @return mixed Display text if param is string, or array of all key=>text if param is NULL
207 */
208 function fetch_as_text($key)
209 {
210 global $lang;
211
212 $keys = array(
213 'desc' => _('Descending'),
214 'asc' => _('Ascending')
215 );
216
217 if ($key === false)
218 {
219 return $keys;
220 }
221 else
222 {
223 return $keys["$key"];
224 }
225 }
226
227 // ###################################################################
228 /**
229 * Returns a multi-dimensional array with sort by keys indexing arrays
230 * with 'image' and 'href' keys that store the values from
231 * fetch_sort_image() and fetch_sort_link(), respectively
232 *
233 * @access public
234 *
235 * @param string Extra GET parameters to pass to fetch_sort_link()
236 *
237 * @return array Array as described above
238 */
239 function fetch_display_array($params = null)
240 {
241 $return = $this->fetch_by_text(false);
242
243 foreach ($return AS $key => $nil)
244 {
245 $return["$key"] = array('image' => ($this->sortkey == $key ? $this->fetch_sort_image() : ''), 'href' => $this->fetch_sort_link($key, $params, true));
246 }
247
248 return $return;
249 }
250
251 // ###################################################################
252 /**
253 * Returns the entire <img> tag for the sort arrow
254 *
255 * @access public
256 *
257 * @return string HTML <img> tag
258 */
259 function fetch_sort_image()
260 {
261 return '<img src="templates/images/arrow_' . $this->fetch_sort_direction() . '.gif" alt="" style="vertical-align: top; border: none" />';
262 }
263
264 // ###################################################################
265 /**
266 * Returns the href value for an <a> tag by generating all the necessary
267 * bits and concat'ing it onto an extra string of GETs (optional)
268 *
269 * @access public
270 *
271 * @param string Sorting key
272 * @param string Additional GET parameters
273 * @param bool Highlight the current sortkey if that's passed?
274 *
275 * @return string HREF
276 */
277 function fetch_sort_link($key, $params = null, $highlight = false)
278 {
279 if ($params)
280 {
281 $params .= '&amp;';
282 }
283
284 return $this->page . '.php?' . $params . 'by=' . $key . '&amp;as=' . (($this->sortkey == $key AND $highlight) ? $this->fetch_opposite_sort_direction() . '" class="select' : $this->fetch_sort_direction());
285 }
286
287 // ###################################################################
288 /**
289 * Returns the OPPOSITE direction to sort when you click on a link
290 *
291 * @access public
292 *
293 * @return string Either asc or desc
294 */
295 function fetch_opposite_sort_direction()
296 {
297 if ($this->direction == 'asc')
298 {
299 return 'desc';
300 }
301 else
302 {
303 return 'asc';
304 }
305 }
306
307 // ###################################################################
308 /**
309 * Returns the current sorted direction for the image path
310 *
311 * @access public
312 *
313 * @return string Either asc or desc
314 */
315 function fetch_sort_direction()
316 {
317 return $this->direction;
318 }
319 }
320
321 /*=====================================================================*\
322 || ###################################################################
323 || # $HeadURL$
324 || # $Id$
325 || ###################################################################
326 \*=====================================================================*/
327 ?>