r1048: Converting all $lang->string() stuff to use the gettext call
[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') . ")))" . (($this->registry->options['hidestatuses'] OR isset($this->registry->userinfo['hidestatuses'])) ? "
117 AND bug.status NOT IN (" . ($this->registry->userinfo['hidestatuses'] != '' ? $this->registry->userinfo['hidestatuses'] : $this->registry->options['hidestatuses']) . ")" : "");
118
119 // remap the sort keys to be actual SQL fields
120 $querykeys = array(
121 'id' => 'bugid',
122 'summary' => 'summary',
123 'reporter' => 'userid',
124 'lastpost' => (can_perform('canviewhidden') ? "lastposttime" : "hiddenlastposttime")
125 );
126
127 switch ($this->sortkey)
128 {
129 case 'id':
130 case 'summary':
131 case 'reporter':
132 case 'lastpost':
133 $query = "
134 SELECT * FROM " . TABLE_PREFIX . "bug AS bug
135 WHERE $basewhere" .
136 (is_array($where) ? "
137 AND " . implode("\nAND ", $where) : "") . "
138 ORDER BY " . $querykeys[ $this->sortkey ] . " " . strtoupper($this->direction) . ($this->sortkey != 'lastpost' ? ", " . $querykeys['lastpost'] . " " . strtoupper($this->direction) : "") . ($limit ? "
139 LIMIT $limit" : "");
140 break;
141 case 'product':
142 case 'status':
143 case 'resolution':
144 case 'priority':
145 case 'severity':
146 $query = "
147 SELECT * FROM " . TABLE_PREFIX . "{$this->sortkey} AS {$this->sortkey}
148 RIGHT JOIN " . TABLE_PREFIX . "bug AS bug
149 ON (bug.{$this->sortkey} = {$this->sortkey}.{$this->sortkey}id)
150 WHERE $basewhere" .
151 (is_array($where) ? "
152 AND " . implode("\nAND ", $where) : "") . "
153 ORDER BY {$this->sortkey}.displayorder " . strtoupper($this->direction) . ", bug.$querykeys[lastpost] " . strtoupper($this->direction) . ($limit ? "
154 LIMIT $limit" : "");
155 break;
156 }
157
158 return $query;
159 }
160
161 // ###################################################################
162 /**
163 * Returns the display text for a given sort order key
164 *
165 * @access public static
166 *
167 * @param string Sort order key, or FALSE for the array
168 *
169 * @return mixed Display text if param is string, or array of all key=>text if param is NULL
170 */
171 function fetch_by_text($key)
172 {
173 global $lang;
174
175 $keys = array(
176 'lastpost' => _('Last Post Time'),
177 'id' => _('Bug ID'),
178 'summary' => _('Summary'),
179 'reporter' => _('Reporter'),
180 'product' => _('Product'),
181 'status' => _('Status'),
182 'resolution' => _('Resolution'),
183 'priority' => _('Priority'),
184 'severity' => _('Severity')
185 );
186
187 if ($key === false)
188 {
189 return $keys;
190 }
191 else
192 {
193 return $keys["$key"];
194 }
195 }
196
197 // ###################################################################
198 /**
199 * Returns the display text for a given sort order direction
200 *
201 * @access public static
202 *
203 * @param string Sort direction, or FALSE for the array
204 *
205 * @return mixed Display text if param is string, or array of all key=>text if param is NULL
206 */
207 function fetch_as_text($key)
208 {
209 global $lang;
210
211 $keys = array(
212 'desc' => _('Descending'),
213 'asc' => _('Ascending')
214 );
215
216 if ($key === false)
217 {
218 return $keys;
219 }
220 else
221 {
222 return $keys["$key"];
223 }
224 }
225
226 // ###################################################################
227 /**
228 * Returns a multi-dimensional array with sort by keys indexing arrays
229 * with 'image' and 'href' keys that store the values from
230 * fetch_sort_image() and fetch_sort_link(), respectively
231 *
232 * @access public
233 *
234 * @param string Extra GET parameters to pass to fetch_sort_link()
235 *
236 * @return array Array as described above
237 */
238 function fetch_display_array($params = null)
239 {
240 $return = $this->fetch_by_text(false);
241
242 foreach ($return AS $key => $nil)
243 {
244 $return["$key"] = array('image' => ($this->sortkey == $key ? $this->fetch_sort_image() : ''), 'href' => $this->fetch_sort_link($key, $params, true));
245 }
246
247 return $return;
248 }
249
250 // ###################################################################
251 /**
252 * Returns the entire <img> tag for the sort arrow
253 *
254 * @access public
255 *
256 * @return string HTML <img> tag
257 */
258 function fetch_sort_image()
259 {
260 return '<img src="templates/images/arrow_' . $this->fetch_sort_direction() . '.gif" alt="" style="vertical-align: top; border: none" />';
261 }
262
263 // ###################################################################
264 /**
265 * Returns the href value for an <a> tag by generating all the necessary
266 * bits and concat'ing it onto an extra string of GETs (optional)
267 *
268 * @access public
269 *
270 * @param string Sorting key
271 * @param string Additional GET parameters
272 * @param bool Highlight the current sortkey if that's passed?
273 *
274 * @return string HREF
275 */
276 function fetch_sort_link($key, $params = null, $highlight = false)
277 {
278 if ($params)
279 {
280 $params .= '&amp;';
281 }
282
283 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());
284 }
285
286 // ###################################################################
287 /**
288 * Returns the OPPOSITE direction to sort when you click on a link
289 *
290 * @access public
291 *
292 * @return string Either asc or desc
293 */
294 function fetch_opposite_sort_direction()
295 {
296 if ($this->direction == 'asc')
297 {
298 return 'desc';
299 }
300 else
301 {
302 return 'asc';
303 }
304 }
305
306 // ###################################################################
307 /**
308 * Returns the current sorted direction for the image path
309 *
310 * @access public
311 *
312 * @return string Either asc or desc
313 */
314 function fetch_sort_direction()
315 {
316 return $this->direction;
317 }
318 }
319
320 /*=====================================================================*\
321 || ###################################################################
322 || # $HeadURL$
323 || # $Id$
324 || ###################################################################
325 \*=====================================================================*/
326 ?>