2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright (c)2004-2008 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)2004 - 2008, Blue Static
53 * Current sort direction
56 private $direction = '';
59 * Column array for table heads
65 * Constructor: set the page name
67 * @param string File name without the .php extension
69 public function __construct($page)
71 $this->db
= BSApp
::$db;
73 $this->_processIncoming();
77 * Processes the incoming variables and then sets all the sort order
78 * information appropriately
80 private function _processIncoming()
82 $this->sortkey
= BSApp
::$input->in
['by'];
83 if (!self
::fetch_by_text(BSApp
::$input->in
['by']))
85 $this->sortkey
= (isset(bugdar
::$userinfo['defaultsortkey']) ? bugdar
::$userinfo['defaultsortkey'] : bugdar
::$options['defaultsortkey']);
88 $this->direction
= BSApp
::$input->in
['as'];
89 if (!in_array($this->direction
, array('asc', 'desc')))
91 $this->direction
= (isset(bugdar
::$userinfo['defaultsortas']) ? bugdar
::$userinfo['defaultsortas'] : bugdar
::$options['defaultsortas']);
96 * Fetch a SQL query to gather bugs with the sort filters applied
98 * @param string Additional WHERE clauses in an array
99 * @param string A LIMIT clause
101 * @return string Compiled SQL query
103 public function fetchSqlQuery($where = null
, $limit = null
)
105 // this WHERE clause is used for all the queries
106 $basewhere = "bug.product IN (" . fetch_on_bits('canviewbugs') . ")
107 AND (!bug.hidden OR (bug.hidden AND bug.product IN (" . fetch_on_bits('canviewhidden') . "))" . (can_perform('canviewownhidden') ?
" OR (bug.hidden AND bug.userid = " . bugdar
::$userinfo['userid'] . " AND bug.product IN (" . fetch_on_bits('canviewownhidden') . "))" : "") . ")" .
108 ((bugdar
::$options['hidestatuses'] ||
isset(bugdar
::$userinfo['hidestatuses'])) ?
"
109 AND bug.status NOT IN (" . (bugdar
::$userinfo['hidestatuses'] != '' ? bugdar
::$userinfo['hidestatuses'] : bugdar
::$options['hidestatuses']) . ")" : "");
111 // remap the sort keys to be actual SQL fields
114 'summary' => 'summary',
115 'reporter' => 'userid',
116 'lastpost' => (can_perform('canviewhidden') ?
"lastposttime" : "hiddenlastposttime"),
117 'assignedto'=> 'assignedto'
120 switch ($this->sortkey
)
128 SELECT bug.*, vote.votefor, vote.voteagainst FROM " . TABLE_PREFIX
. "bug AS bug
129 LEFT JOIN " . TABLE_PREFIX
. "vote AS vote
130 ON (bug.bugid = vote.bugid)
132 (is_array($where) ?
"
133 AND " . implode("\nAND ", $where) : "") . "
134 ORDER BY " . $querykeys[ $this->sortkey
] . " " . strtoupper($this->direction
) . ($this->sortkey
!= 'lastpost' ?
", " . $querykeys['lastpost'] . " " . strtoupper($this->direction
) : "") . ($limit ?
"
144 $key = ($this->sortkey
!= 'component' ?
$this->sortkey
: 'product');
146 SELECT $key.*, bug.*, vote.votefor, vote.voteagainst FROM " . TABLE_PREFIX
. "$key AS $key
147 RIGHT JOIN " . TABLE_PREFIX
. "bug AS bug
148 ON (bug.$key = $key.{$key}id)
149 LEFT JOIN " . TABLE_PREFIX
. "vote AS vote
150 ON (bug.bugid = vote.bugid)
152 (is_array($where) ?
"
153 AND " . implode("\nAND ", $where) : "") . "
154 ORDER BY $key.displayorder " . strtoupper($this->direction
) . ", bug.$querykeys[lastpost] " . strtoupper($this->direction
) . ($limit ?
"
159 SELECT bug.*, vote.votefor, vote.voteagainst FROM " . TABLE_PREFIX
. "bug AS bug
160 LEFT JOIN " . TABLE_PREFIX
. "vote AS vote
161 ON (bug.bugid = vote.bugid)
163 (is_array($where) ?
"
164 AND " . implode("\nAND ", $where) : "") . "
165 ORDER BY vote.votefor " . strtoupper($this->direction
) . ", vote.voteagainst " . strtoupper($this->fetchOppositeSortDirection()) . ", bug.$querykeys[lastpost] " . strtoupper($this->direction
) . ($limit ?
"
169 if (substr($this->sortkey
, 0, 6) != 'custom')
175 SELECT bug.*, vote.votefor, vote.voteagainst FROM " . TABLE_PREFIX
. "bug AS bug
176 LEFT JOIN " . TABLE_PREFIX
. "vote AS vote
177 ON (bug.bugid = vote.bugid)
179 (is_array($where) ?
"
180 AND " . implode("\nAND ", $where) : "") . "
181 ORDER BY {$this->sortkey} " . strtoupper($this->direction
) . ", " . $querykeys['lastpost'] . " " . strtoupper($this->direction
) . ($limit ?
"
189 * Returns the display text for a given sort order key
191 * @param string Sort order key, or FALSE for the array
192 * @param bool Permission check the custom fields?
194 * @return mixed Display text if param is string, or array of all key=>text if param is NULL
196 public static function fetch_by_text($key, $doPerm = true
)
199 'lastpost' => T('Last Post Time'),
200 'bugid' => T('Bug ID'),
201 'summary' => T('Summary'),
202 'reporter' => T('Reporter'),
203 'product' => T('Product'),
204 'component' => T('Component'),
205 'version' => T('Version'),
206 'status' => T('Status'),
207 'resolution' => T('Resolution'),
208 'priority' => T('Priority'),
209 'severity' => T('Severity'),
210 'votes' => T('Votes'),
211 'assignedto' => T('Assigned To')
214 $fields = self
::_fetch_custom_fields($doPerm);
215 foreach ($fields AS $field)
217 $keys['custom' . $field['fieldid']] = $field['name'];
226 return $keys["$key"];
231 * Returns the display text for a given sort order direction
233 * @param string Sort direction, or FALSE for the array
235 * @return mixed Display text if param is string, or array of all key=>text if param is NULL
237 public static function fetch_as_text($key)
240 'desc' => T('Descending'),
241 'asc' => T('Ascending')
250 return $keys["$key"];
255 * Returns a multi-dimensional array with sort by keys indexing arrays
256 * with 'image' and 'href' keys that store the values from
257 * fetchSortImage() and fetchSortLink(), respectively
259 * @param string Extra GET parameters to pass to fetchSortLink()
261 * @return array Array as described above
263 public function fetchDisplayArray($params = null
)
265 $return = self
::fetch_by_text(false
);
267 foreach ($return as $key => $nil)
269 $return["$key"] = array('image' => ($this->sortkey
== $key ?
$this->fetchSortImage() : ''), 'href' => $this->fetchSortLink($key, $params, true
));
276 * Returns the entire <img> tag for the sort arrow
278 * @return string HTML <img> tag
280 public function fetchSortImage()
282 return '<img src="templates/images/arrow_' . $this->fetchSortDirection() . '.gif" alt="" style="vertical-align: top; border: none" />';
286 * Returns the href value for an <a> tag by generating all the necessary
287 * bits and concat'ing it onto an extra string of GETs (optional)
289 * @param string Sorting key
290 * @param string Additional GET parameters
291 * @param bool Highlight the current sortkey if that's passed?
293 * @return string HREF
295 public function fetchSortLink($key, $params = null
, $highlight = false
)
302 return $this->page
. '.php?' . $params . 'by=' . $key . '&as=' . (($this->sortkey
== $key && $highlight) ?
$this->fetchOppositeSortDirection() . '" class="select' : $this->fetchSortDirection());
306 * Returns the OPPOSITE direction to sort when you click on a link
308 * @return string Either asc or desc
310 public function fetchOppositeSortDirection()
312 if ($this->direction
== 'asc')
323 * Returns the current sorted direction for the image path
325 * @return string Either asc or desc
327 public function fetchSortDirection()
329 return $this->direction
;
333 * Returns the HTML code for bug listing table column headers
335 * @param bool Include the sort links/image?
336 * @param string Additional GET params to pass to fetchSortLink()
338 * @return string HTML code
340 public function constructColumnHeaders($sortable, $params = null
)
342 $this->_processColumns();
344 $names = self
::fetch_by_text(false
);
347 foreach ($this->columns
as $columns)
350 foreach ($columns as $column)
352 $build[] = ($sortable ?
'<a href="' . $this->fetchSortLink($column, $params, true
) . '">' . $names[$column] . '</a>' : $names[$column]);
354 $image = ((in_array($this->sortkey
, $columns) && $sortable) ?
$this->fetchSortImage() : '');
355 $name = implode(' / ', $build);
357 $tpl = new BSTemplate('list_head');
362 $output .= $tpl->evaluate()->getTemplate();
369 * Returns the HTML code for a row of data for the bug listing
371 * @param array Bug data array
372 * @param string Additional link params
374 * @return string Row HTML
376 function constructRow($bug, $params = null
)
378 $this->_processColumns();
380 foreach ($this->columns
as $columns)
382 if (sizeof($columns) > 1)
385 foreach ($columns as $column)
387 $build[] = $this->_processDataForColumn($bug, $column, $params, true
);
389 $data = "\n\t\t" . implode("\n\t\t", $build) . "\n\t";
393 $data = $this->_processDataForColumn($bug, $columns[0], $params, false
);
395 $fields .= "\n\t<td>$data</td>";
398 $tpl = new BSTemplate('trackerhome_bits');
403 return $tpl->evaluate()->getTemplate();
407 * Handler for special-case column data
409 * @param array Bug data
410 * @param string Column name
411 * @param string Additional URL params
412 * @param bool Will this column have multiple data sets?
414 * @return string Processed column data
416 private function _processDataForColumn($bug, $column, $params = null
, $multi = false
)
418 $open = ($multi ?
'<div>' : '');
419 $close = ($multi ?
'</div>' : '');
423 return $open . '<a href="showreport.php?bugid=' . $bug['bugid'] . $params . '">' . $bug['summary'] . '</a>' . $close;
425 return $open . ($bug['userid'] ?
$bug['username'] : T('Guest')) . $close;
427 return "\n\t\t<div>" . $bug['lastposttime'] . "</div>\n\t\t<div>" . T('by') . ' ' . ($bug['lastpost'] ?
$bug['lastpost'] : T('Guest')) . "</div>\n\t";
429 return "\n\t\t<div>" . T('For:') . ' ' . $bug['votefor'] . "</div>\n\t\t<div>" . T('Against:') . ' ' . $bug['voteagainst'] . "</div>\n\t";
431 return $open . $bug["$column"] . $close;
436 * Sets up $this->columns so that the data can be processed more
439 private function _processColumns()
441 if (is_array($this->columns
))
446 $columns = self
::fetch_by_text(false
);
448 $array = ((bugdar
::$userinfo['userid'] && is_array(bugdar
::$userinfo['columnoptions'])) ? bugdar
::$userinfo['columnoptions'] : bugdar
::$options['columnoptions']);
450 foreach ($array as $column => $position)
452 // the column doesn't exist, or more likely, we don't have permission to view it
453 if (!isset($columns[$column]))
459 $this->columns
["$position"][] = $column;
463 ksort($this->columns
);
467 * Returns an array of all the custom fields that the current user
468 * has permission to use
470 * @param boolean Ignore permissions?
474 private static function _fetch_custom_fields($doPerm = true
)
476 static $fields = array(), $fieldsPerm = array();
478 if ($doPerm && !empty($fieldsPerm))
482 else if (!$doPerm && !empty($fields))
489 $fields_fetch = BSApp
::$db->query("
490 SELECT bugfield.*, MAX(permission.mask) AS mask
491 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
492 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
493 ON (bugfield.fieldid = permission.fieldid)
494 WHERE (permission.mask = 2 OR permission.mask = 1)
495 AND permission.usergroupid IN (" . bugdar
::$userinfo['usergroupid'] . (sizeof(bugdar
::$userinfo['groupids']) != 0 ?
',' . implode(',', bugdar
::$userinfo['groupids']) : '') . ")
496 GROUP BY (bugfield.fieldid)
501 $fields_fetch = BSApp
::$db->query("SELECT * FROM " . TABLE_PREFIX
. "bugfield");
504 foreach ($fields_fetch as $field)
508 $fieldsPerm[$field['fieldid']] = $field;
512 $fields[$field['fieldid']] = $field;