]>
src.bluestatic.org Git - bugdar.git/blob - includes/class_sort.php
2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright (c)2002-2007 Blue Static
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 2 of the License.
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
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 \*=====================================================================*/
25 * This class is used to sort bugs based on user-sent options and variables.
28 * @copyright Copyright (c)2002 - 2007, Blue Static
57 * Current sort direction
64 * Column array for table heads
69 // ###################################################################
71 * Constructor: set the page name
75 * @param string File name without the .php extension
77 function ListSorter ( $page )
80 $this- > registry
= $bugsys ;
82 $this- > process_incoming ();
85 // ###################################################################
87 * Processes the incoming variables and then sets all the sort order
88 * information appropriately
92 function process_incoming ()
94 $this- > sortkey
= $this- > registry
-> in
[ 'by' ];
95 if (! $this- > fetch_by_text ( $this- > registry
-> in
[ 'by' ]))
97 $this- > sortkey
= ( isset ( $this- > registry
-> userinfo
[ 'defaultsortkey' ]) ? $this- > registry
-> userinfo
[ 'defaultsortkey' ] : $this- > registry
-> options
[ 'defaultsortkey' ]);
100 $this- > direction
= $this- > registry
-> in
[ 'as' ];
101 if (! in_array ( $this- > direction
, array ( 'asc' , 'desc' )))
103 $this- > direction
= ( isset ( $this- > registry
-> userinfo
[ 'defaultsortas' ]) ? $this- > registry
-> userinfo
[ 'defaultsortas' ] : $this- > registry
-> options
[ 'defaultsortas' ]);
107 // ###################################################################
109 * Fetch a SQL query to gather bugs with the sort filters applied
113 * @param string Additional WHERE clauses in an array
114 * @param string A LIMIT clause
116 * @return string Compiled SQL query
118 function fetch_sql_query ( $where = null , $limit = null )
120 // this WHERE clause is used for all the queries
121 $basewhere = "bug.product IN (" . fetch_on_bits ( 'canviewbugs' ) . ")
122 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' ) . "))" : "" ) . ")" .
123 (( $this- > registry
-> options
[ 'hidestatuses' ] OR isset ( $this- > registry
-> userinfo
[ 'hidestatuses' ])) ? "
124 AND bug.status NOT IN (" . ( $this- > registry
-> userinfo
[ 'hidestatuses' ] != '' ? $this- > registry
-> userinfo
[ 'hidestatuses' ] : $this- > registry
-> options
[ 'hidestatuses' ]) . ")" : "" );
126 // remap the sort keys to be actual SQL fields
129 'summary' => 'summary' ,
130 'reporter' => 'userid' ,
131 'lastpost' => ( can_perform ( 'canviewhidden' ) ? "lastposttime" : "hiddenlastposttime" ),
132 'assignedto' => 'assignedto'
135 switch ( $this- > sortkey
)
143 SELECT bug.*, vote.votefor, vote.voteagainst FROM " . TABLE_PREFIX
. "bug AS bug
144 LEFT JOIN " . TABLE_PREFIX
. "vote AS vote
145 ON (bug.bugid = vote.bugid)
147 (is_array( $where ) ? "
148 AND " . implode(" \nAND
", $where ) : " ") . "
149 ORDER BY
" . $querykeys [ $this- >sortkey ] . " " . strtoupper( $this- >direction) . ( $this- >sortkey != 'lastpost' ? " , " . $querykeys ['lastpost'] . " " . strtoupper( $this- >direction) : " ") . ( $limit ? "
159 $key = ( $this- > sortkey
!= 'component' ? $this- > sortkey
: 'product' );
161 SELECT $key .*, bug.*, vote.votefor, vote.voteagainst FROM " . TABLE_PREFIX
. " $key AS $key
162 RIGHT JOIN " . TABLE_PREFIX
. "bug AS bug
163 ON (bug. $key = $key . {$key} id)
164 LEFT JOIN " . TABLE_PREFIX
. "vote AS vote
165 ON (bug.bugid = vote.bugid)
167 (is_array( $where ) ? "
168 AND " . implode(" \nAND
", $where ) : " ") . "
169 ORDER BY
$key . displayorder
" . strtoupper( $this- >direction) . " , bug
. $querykeys [ lastpost
] " . strtoupper( $this- >direction) . ( $limit ? "
174 SELECT bug.*, vote.votefor, vote.voteagainst FROM " . TABLE_PREFIX
. "bug AS bug
175 LEFT JOIN " . TABLE_PREFIX
. "vote AS vote
176 ON (bug.bugid = vote.bugid)
178 (is_array( $where ) ? "
179 AND " . implode(" \nAND
", $where ) : " ") . "
180 ORDER BY vote
. votefor
" . strtoupper( $this- >direction) . " , vote
. voteagainst
" . strtoupper( $this- >fetch_opposite_sort_direction()) . " , bug
. $querykeys [ lastpost
] " . strtoupper( $this- >direction) . ( $limit ? "
188 // ###################################################################
190 * Returns the display text for a given sort order key
192 * @access public static
194 * @param string Sort order key, or FALSE for the array
196 * @return mixed Display text if param is string, or array of all key=>text if param is NULL
198 function fetch_by_text ( $key )
203 'lastpost' => T ( 'Last Post Time' ),
204 'bugid' => T ( 'Bug ID' ),
205 'summary' => T ( 'Summary' ),
206 'reporter' => T ( 'Reporter' ),
207 'product' => T ( 'Product' ),
208 'component' => T ( 'Component' ),
209 'version' => T ( 'Version' ),
210 'status' => T ( 'Status' ),
211 'resolution' => T ( 'Resolution' ),
212 'priority' => T ( 'Priority' ),
213 'severity' => T ( 'Severity' ),
214 'votes' => T ( 'Votes' ),
215 'assignedto' => T ( 'Assigned To' )
224 return $keys [ " $key" ];
228 // ###################################################################
230 * Returns the display text for a given sort order direction
232 * @access public static
234 * @param string Sort direction, or FALSE for the array
236 * @return mixed Display text if param is string, or array of all key=>text if param is NULL
238 function fetch_as_text( $key )
243 'desc' => T('Descending'),
244 'asc' => T('Ascending')
253 return $keys [" $key" ];
257 // ###################################################################
259 * Returns a multi-dimensional array with sort by keys indexing arrays
260 * with 'image' and 'href' keys that store the values from
261 * fetch_sort_image() and fetch_sort_link(), respectively
265 * @param string Extra GET parameters to pass to fetch_sort_link()
267 * @return array Array as described above
269 function fetch_display_array ( $params = null )
271 $return = $this- > fetch_by_text ( false );
273 foreach ( $return AS $key => $nil )
275 $return [ " $key" ] = array('image' => ( $this- >sortkey == $key ? $this- >fetch_sort_image() : ''), 'href' => $this- >fetch_sort_link( $key , $params , true));
281 // ###################################################################
283 * Returns the entire <img> tag for the sort arrow
287 * @return string HTML <img> tag
289 function fetch_sort_image()
291 return '<img src=" templates
/ images
/ arrow_
' . $this- >fetch_sort_direction() . ' . gif
" alt=" " style=" vertical
- align
: top
; border
: none
" />';
294 // ###################################################################
296 * Returns the href value for an <a> tag by generating all the necessary
297 * bits and concat'ing it onto an extra string of GETs (optional)
301 * @param string Sorting key
302 * @param string Additional GET parameters
303 * @param bool Highlight the current sortkey if that's passed?
305 * @return string HREF
307 function fetch_sort_link( $key , $params = null, $highlight = false)
314 return $this- >page . '.php?' . $params . 'by=' . $key . '&as=' . (( $this- >sortkey == $key AND $highlight ) ? $this- >fetch_opposite_sort_direction() . '" class = "select' : $this- >fetch_sort_direction());
317 // ###################################################################
319 * Returns the OPPOSITE direction to sort when you click on a link
323 * @return string Either asc or desc
325 function fetch_opposite_sort_direction()
327 if ( $this- >direction == 'asc')
337 // ###################################################################
339 * Returns the current sorted direction for the image path
343 * @return string Either asc or desc
345 function fetch_sort_direction()
347 return $this- >direction;
350 // ###################################################################
352 * Returns the HTML code for bug listing table column headers
354 * @param bool Include the sort links/image?
355 * @param string Additional GET params to pass to fetch_sort_link()
357 * @return string HTML code
359 function constructColumnHeaders( $sortable , $params = null)
361 $this- >_processColumns();
364 foreach ( $this- >columns AS $columns )
367 foreach ( $columns AS $column )
369 $build [] = ( $sortable ? '<a href=" ' . $this- >fetch_sort_link( $column , $params , true) . ' ">' . $this- >registry->columnNames[" $column" ] . '</a>' : $this- > registry
-> columnNames
[ " $column" ]);
371 $image = ((in_array( $this- >sortkey, $columns ) AND $sortable ) ? $this- >fetch_sort_image() : '');
372 $name = implode(' / ', $build );
373 eval(' $output .= " ' . $this- >registry->template->fetch(' list_head
') . ' ";');
379 // ###################################################################
381 * Returns the HTML code for a row of data for the bug listing
383 * @param array Bug data array
384 * @param string Additional link params
386 * @return string Row HTML
388 function constructRow( $bug , $params = null)
392 $this- >_processColumns();
394 foreach ( $this- >columns AS $columns )
396 if (sizeof( $columns ) > 1)
399 foreach ( $columns AS $column )
401 $build [] = $this- >_processDataForColumn( $bug , $column , $params , true);
403 $data = " \n\t\t
" . implode(" \n\t\t
", $build ) . " \n\t
";
407 $data = $this- >_processDataForColumn( $bug , $columns [0], $params , false);
409 $fields .= " \n\t
< td
> $data </ td
> ";
412 eval(' $output = " ' . $this- >registry->template->fetch(' trackerhome_bits
') . ' ";');
416 // ###################################################################
418 * Handler for special-case column data
420 * @param array Bug data
421 * @param string Column name
422 * @param string Additional URL params
423 * @param bool Will this column have multiple data sets?
425 * @return string Processed column data
427 function _processDataForColumn( $bug , $column , $params = null, $multi = false)
429 $open = ( $multi ? '<div>' : '');
430 $close = ( $multi ? '</div>' : '');
434 return $open . '<a href=" showreport
. php
? bugid
= ' . $bug [' bugid
'] . $params . ' ">' . $bug ['summary'] . '</a>' . $close ;
436 return $open . ( $bug ['userid'] ? $bug ['username'] : T('Guest')) . $close ;
438 return " \n\t\t
< div
> " . $bug ['lastposttime'] . " </ div
>\n\t\t< div
> " . T('by') . ' ' . ( $bug ['lastpost'] ? $bug ['lastpost'] : T('Guest')) . " </ div
>\n\t ";
440 return " \n\t\t
< div
> " . T('For:') . ' ' . $bug ['votefor'] . " </ div
>\n\t\t< div
> " . T('Against:') . ' ' . $bug ['voteagainst'] . " </ div
>\n\t ";
442 return $open . $bug [" $column" ] . $close ;
446 // ###################################################################
448 * Sets up $this->columns so that the data can be processed more
451 function _processColumns ()
453 if ( is_array ( $this- > columns
))
458 $array = (( $this- > registry
-> userinfo
[ 'userid' ] AND is_array ( $this- > registry
-> userinfo
[ 'columnoptions' ])) ? $this- > registry
-> userinfo
[ 'columnoptions' ] : $this- > registry
-> options
[ 'columnoptions' ]);
460 foreach ( $array AS $column => $position )
464 $this- > columns
[ " $position" ][] = $column ;
468 ksort( $this- >columns);