r938: - Basics of the sorting system are now working
[bugdar.git] / includes / class_sort.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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 Iris Studios, Inc.
28 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
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 = 'lastpost';
92 }
93
94 $this->direction = $this->registry->in['as'];
95 if (!in_array($this->direction, array('asc', 'desc')))
96 {
97 $this->direction = 'desc';
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 (" . (isset($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 ] . ($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 'id' => $lang->string('Bug ID'),
177 'summary' => $lang->string('Summary'),
178 'reporter' => $lang->string('Reporter'),
179 'product' => $lang->string('Product'),
180 'status' => $lang->string('Status'),
181 'resolution' => $lang->string('Resolution'),
182 'priority' => $lang->string('Priority'),
183 'severity' => $lang->string('Severity'),
184 'lastpost' => $lang->string('Last Post Time')
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 a multi-dimensional array with sort by keys indexing arrays
200 * with 'image' and 'href' keys that store the values from
201 * fetch_sort_image() and fetch_sort_link(), respectively
202 *
203 * @access public
204 *
205 * @param string Extra GET parameters to pass to fetch_sort_link()
206 *
207 * @return array Array as described above
208 */
209 function fetch_display_array($params = null)
210 {
211 $return = $this->fetch_by_text(false);
212
213 foreach ($return AS $key => $nil)
214 {
215 $return["$key"] = array('image' => ($this->sortkey == $key ? $this->fetch_sort_image() : ''), 'href' => $this->fetch_sort_link($key, $params));
216 }
217
218 return $return;
219 }
220
221 // ###################################################################
222 /**
223 * Returns the entire <img> tag for the sort arrow
224 *
225 * @access public
226 *
227 * @return string HTML <img> tag
228 */
229 function fetch_sort_image()
230 {
231 return '<img src="templates/images/arrow_' . $this->fetch_sort_direction() . '.gif" alt="" style="vertical-align: top; border: none" />';
232 }
233
234 // ###################################################################
235 /**
236 * Returns the href value for an <a> tag by generating all the necessary
237 * bits and concat'ing it onto an extra string of GETs (optional)
238 *
239 * @access public
240 *
241 * @param string Sorting key
242 * @param string Additional GET parameters
243 *
244 * @return string HREF
245 */
246 function fetch_sort_link($key, $params = null)
247 {
248 if ($params)
249 {
250 $params .= '&amp;';
251 }
252
253 return $this->page . '.php?' . $params . 'by=' . $key . '&amp;as=' . ($this->sortkey == $key ? $this->fetch_opposite_sort_direction() . '" class="select' : $this->fetch_sort_direction());
254 }
255
256 // ###################################################################
257 /**
258 * Returns the OPPOSITE direction to sort when you click on a link
259 *
260 * @access public
261 *
262 * @return string Either asc or desc
263 */
264 function fetch_opposite_sort_direction()
265 {
266 if ($this->direction == 'asc')
267 {
268 return 'desc';
269 }
270 else
271 {
272 return 'asc';
273 }
274 }
275
276 // ###################################################################
277 /**
278 * Returns the current sorted direction for the image path
279 *
280 * @access public
281 *
282 * @return string Either asc or desc
283 */
284 function fetch_sort_direction()
285 {
286 return $this->direction;
287 }
288 }
289
290 /*=====================================================================*\
291 || ###################################################################
292 || # $HeadURL$
293 || # $Id$
294 || ###################################################################
295 \*=====================================================================*/
296 ?>