registry = $bugsys;
$this->page = $page;
$this->process_incoming();
}
// ###################################################################
/**
* Processes the incoming variables and then sets all the sort order
* information appropriately
*
* @access private
*/
function process_incoming()
{
$this->sortkey = $this->registry->in['by'];
if (!$this->fetch_by_text($this->registry->in['by']))
{
$this->sortkey = (isset($this->registry->userinfo['defaultsortkey']) ? $this->registry->userinfo['defaultsortkey'] : $this->registry->options['defaultsortkey']);
}
$this->direction = $this->registry->in['as'];
if (!in_array($this->direction, array('asc', 'desc')))
{
$this->direction = (isset($this->registry->userinfo['defaultsortas']) ? $this->registry->userinfo['defaultsortas'] : $this->registry->options['defaultsortas']);
}
}
// ###################################################################
/**
* Fetch a SQL query to gather bugs with the sort filters applied
*
* @access public
*
* @param string Additional WHERE clauses in an array
* @param string A LIMIT clause
*
* @return string Compiled SQL query
*/
function fetch_sql_query($where = null, $limit = null)
{
// this WHERE clause is used for all the queries
$basewhere = "bug.product IN (" . fetch_on_bits('canviewbugs') . ")
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') . "))" : "") . ")" .
(($this->registry->options['hidestatuses'] OR isset($this->registry->userinfo['hidestatuses'])) ? "
AND bug.status NOT IN (" . ($this->registry->userinfo['hidestatuses'] != '' ? $this->registry->userinfo['hidestatuses'] : $this->registry->options['hidestatuses']) . ")" : "");
// remap the sort keys to be actual SQL fields
$querykeys = array(
'bugid' => 'bugid',
'summary' => 'summary',
'reporter' => 'userid',
'lastpost' => (can_perform('canviewhidden') ? "lastposttime" : "hiddenlastposttime")
);
switch ($this->sortkey)
{
case 'bugid':
case 'summary':
case 'reporter':
case 'lastpost':
$query = "
SELECT bug.*, vote.votefor, vote.voteagainst FROM " . TABLE_PREFIX . "bug AS bug
LEFT JOIN " . TABLE_PREFIX . "vote AS vote
ON (bug.bugid = vote.bugid)
WHERE $basewhere" .
(is_array($where) ? "
AND " . implode("\nAND ", $where) : "") . "
ORDER BY " . $querykeys[ $this->sortkey ] . " " . strtoupper($this->direction) . ($this->sortkey != 'lastpost' ? ", " . $querykeys['lastpost'] . " " . strtoupper($this->direction) : "") . ($limit ? "
LIMIT $limit" : "");
break;
case 'product':
case 'component':
case 'version':
case 'status':
case 'resolution':
case 'priority':
case 'severity':
$key = ($this->sortkey != 'component' ? $this->sortkey : 'product');
$query = "
SELECT $key.*, bug.*, vote.votefor, vote.voteagainst FROM " . TABLE_PREFIX . "$key AS $key
RIGHT JOIN " . TABLE_PREFIX . "bug AS bug
ON (bug.$key = $key.{$key}id)
LEFT JOIN " . TABLE_PREFIX . "vote AS vote
ON (bug.bugid = vote.bugid)
WHERE $basewhere" .
(is_array($where) ? "
AND " . implode("\nAND ", $where) : "") . "
ORDER BY $key.displayorder " . strtoupper($this->direction) . ", bug.$querykeys[lastpost] " . strtoupper($this->direction) . ($limit ? "
LIMIT $limit" : "");
break;
case 'votes':
$query = "
SELECT bug.*, vote.votefor, vote.voteagainst FROM " . TABLE_PREFIX . "bug AS bug
LEFT JOIN " . TABLE_PREFIX . "vote AS vote
ON (bug.bugid = vote.bugid)
WHERE $basewhere" .
(is_array($where) ? "
AND " . implode("\nAND ", $where) : "") . "
ORDER BY vote.votefor " . strtoupper($this->direction) . ", vote.voteagainst " . strtoupper($this->fetch_opposite_sort_direction()) . ", bug.$querykeys[lastpost] " . strtoupper($this->direction) . ($limit ? "
LIMIT $limit" : "");
break;
}
return $query;
}
// ###################################################################
/**
* Returns the display text for a given sort order key
*
* @access public static
*
* @param string Sort order key, or FALSE for the array
*
* @return mixed Display text if param is string, or array of all key=>text if param is NULL
*/
function fetch_by_text($key)
{
global $lang;
$keys = array(
'lastpost' => _('Last Post Time'),
'bugid' => _('Bug ID'),
'summary' => _('Summary'),
'reporter' => _('Reporter'),
'product' => _('Product'),
'component' => _('Component'),
'version' => _('Version'),
'status' => _('Status'),
'resolution' => _('Resolution'),
'priority' => _('Priority'),
'severity' => _('Severity'),
'votes' => _('Votes')
);
if ($key === false)
{
return $keys;
}
else
{
return $keys["$key"];
}
}
// ###################################################################
/**
* Returns the display text for a given sort order direction
*
* @access public static
*
* @param string Sort direction, or FALSE for the array
*
* @return mixed Display text if param is string, or array of all key=>text if param is NULL
*/
function fetch_as_text($key)
{
global $lang;
$keys = array(
'desc' => _('Descending'),
'asc' => _('Ascending')
);
if ($key === false)
{
return $keys;
}
else
{
return $keys["$key"];
}
}
// ###################################################################
/**
* Returns a multi-dimensional array with sort by keys indexing arrays
* with 'image' and 'href' keys that store the values from
* fetch_sort_image() and fetch_sort_link(), respectively
*
* @access public
*
* @param string Extra GET parameters to pass to fetch_sort_link()
*
* @return array Array as described above
*/
function fetch_display_array($params = null)
{
$return = $this->fetch_by_text(false);
foreach ($return AS $key => $nil)
{
$return["$key"] = array('image' => ($this->sortkey == $key ? $this->fetch_sort_image() : ''), 'href' => $this->fetch_sort_link($key, $params, true));
}
return $return;
}
// ###################################################################
/**
* Returns the entire tag for the sort arrow
*
* @access public
*
* @return string HTML
tag
*/
function fetch_sort_image()
{
return '
';
}
// ###################################################################
/**
* Returns the href value for an tag by generating all the necessary
* bits and concat'ing it onto an extra string of GETs (optional)
*
* @access public
*
* @param string Sorting key
* @param string Additional GET parameters
* @param bool Highlight the current sortkey if that's passed?
*
* @return string HREF
*/
function fetch_sort_link($key, $params = null, $highlight = false)
{
if ($params)
{
$params .= '&';
}
return $this->page . '.php?' . $params . 'by=' . $key . '&as=' . (($this->sortkey == $key AND $highlight) ? $this->fetch_opposite_sort_direction() . '" class="select' : $this->fetch_sort_direction());
}
// ###################################################################
/**
* Returns the OPPOSITE direction to sort when you click on a link
*
* @access public
*
* @return string Either asc or desc
*/
function fetch_opposite_sort_direction()
{
if ($this->direction == 'asc')
{
return 'desc';
}
else
{
return 'asc';
}
}
// ###################################################################
/**
* Returns the current sorted direction for the image path
*
* @access public
*
* @return string Either asc or desc
*/
function fetch_sort_direction()
{
return $this->direction;
}
// ###################################################################
/**
* Returns the HTML code for bug listing table column headers
*
* @param bool Include the sort links/image?
* @param string Additional GET params to pass to fetch_sort_link()
*
* @return string HTML code
*/
function constructColumnHeaders($sortable, $params = null)
{
$this->_processColumns();
$output = '';
foreach ($this->columns AS $columns)
{
$build = array();
foreach ($columns AS $column)
{
$build[] = ($sortable ? '' . $this->registry->columnNames["$column"] . '' : $this->registry->columnNames["$column"]);
}
$image = ((in_array($this->sortkey, $columns) AND $sortable) ? $this->fetch_sort_image() : '');
$name = implode(' / ', $build);
eval('$output .= "' . $this->registry->template->fetch('list_head') . '";');
}
return $output;
}
// ###################################################################
/**
* Returns the HTML code for a row of data for the bug listing
*
* @param array Bug data array
* @param string Additional link params
*
* @return string Row HTML
*/
function constructRow($bug, $params = null)
{
$this->_processColumns();
foreach ($this->columns AS $columns)
{
if (sizeof($columns) > 1)
{
$build = array();
foreach ($columns AS $column)
{
$build[] = $this->_processDataForColumn($bug, $column, $params, true);
}
$data = "\n\t\t" . implode("\n\t\t", $build) . "\n\t";
}
else
{
$data = $this->_processDataForColumn($bug, $columns[0], $params, false);
}
$fields .= "\n\t