2003-08-21 15:47:50 +00:00
|
|
|
<?php
|
2003-08-25 08:44:17 +00:00
|
|
|
// $Id$
|
2003-08-21 15:47:50 +00:00
|
|
|
|
|
|
|
function tablesort_init($header) {
|
2004-06-02 05:35:51 +00:00
|
|
|
$ts = tablesort_get_order($header);
|
|
|
|
$ts['sort'] = tablesort_get_sort($header);
|
|
|
|
$ts['query_string'] = tablesort_get_querystring();
|
2003-08-21 15:47:50 +00:00
|
|
|
return $ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tablesort_pager() {
|
2003-08-27 05:49:52 +00:00
|
|
|
$cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET : $_POST;
|
2004-06-02 05:35:51 +00:00
|
|
|
unset($cgi['q'], $cgi['from']);
|
2003-08-27 05:49:52 +00:00
|
|
|
return $cgi;
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function tablesort_sql($header) {
|
|
|
|
$ts = tablesort_init($header);
|
2004-06-02 05:35:51 +00:00
|
|
|
if ($ts['field']) {
|
|
|
|
$sql = check_query($ts['sql']);
|
|
|
|
$sort = strtoupper(check_query($ts['sort']));
|
|
|
|
return " ORDER BY $sql $sort";
|
|
|
|
}
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
2004-06-02 05:35:51 +00:00
|
|
|
function tablesort_header($cell, $header, $ts) {
|
2003-08-21 15:47:50 +00:00
|
|
|
// special formatting for the currently sorted column header
|
2004-06-02 05:35:51 +00:00
|
|
|
if (is_array($cell) && $cell['field']) {
|
|
|
|
if ($cell['data'] == $ts['name']) {
|
|
|
|
$ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
|
|
|
|
$cell['class'] = 'active';
|
|
|
|
$image = ' <img src="' . theme('image', 'arrow-' . $ts['sort'] . '.gif') . '" alt="'. t('sort icon') .'" />';
|
|
|
|
$title = ($ts['sort'] == 'asc' ? t('sort ascending') : t('sort descending'));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If the user clicks a different header, we want to sort ascending initially.
|
|
|
|
$ts['sort'] = 'asc';
|
|
|
|
}
|
|
|
|
$title = t('sort by %s', array('%s' => $cell['data']));
|
|
|
|
$cell['data'] = l($cell['data'] . $image, $_GET['q'], array('title' => $title), "sort=". $ts['sort']. "&order=". urlencode($cell['data']). $ts['query_string']);
|
2003-08-27 05:49:52 +00:00
|
|
|
|
2004-06-02 05:35:51 +00:00
|
|
|
unset($cell['field'], $cell['sort']);
|
|
|
|
}
|
|
|
|
return $cell;
|
|
|
|
}
|
2004-01-29 06:42:03 +00:00
|
|
|
|
2004-06-02 05:35:51 +00:00
|
|
|
function tablesort_cell($cell, $header, $ts, $i) {
|
|
|
|
if ($header[$i]['data'] == $ts['name'] && $header[$i]['field']) {
|
|
|
|
if (is_array($cell)) {
|
|
|
|
$cell['class'] .= ' active';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$cell = array('data' => $cell, 'class' => 'active');
|
|
|
|
}
|
|
|
|
}
|
2003-08-21 15:47:50 +00:00
|
|
|
return $cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tablesort_get_querystring() {
|
|
|
|
$cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET : $_POST;
|
|
|
|
foreach ($cgi as $key => $val) {
|
2004-01-25 10:07:24 +00:00
|
|
|
if ($key != 'order' && $key != 'sort' && $key != 'q') {
|
2004-06-02 05:35:51 +00:00
|
|
|
$query_string .= '&'. $key .'='. $val;
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $query_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tablesort_get_order($headers) {
|
2003-10-27 21:45:55 +00:00
|
|
|
$order = $_GET['order'];
|
|
|
|
foreach ($headers as $header) {
|
|
|
|
if ($order == $header['data']) {
|
2004-05-29 17:38:54 +00:00
|
|
|
return array('name' => $header['data'], 'sql' => $header['field']);
|
2003-10-27 21:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($header['sort'] == 'asc' || $header['sort'] == 'desc') {
|
2004-05-29 17:38:54 +00:00
|
|
|
$default = array('name' => $header['data'], 'sql' => $header['field']);
|
2003-10-27 21:45:55 +00:00
|
|
|
}
|
2004-06-02 05:35:51 +00:00
|
|
|
$i++;
|
2003-10-27 21:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($default) {
|
|
|
|
return $default;
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2003-10-27 21:45:55 +00:00
|
|
|
// The first column specified is initial 'order by' field unless otherwise specified
|
2004-05-29 17:38:54 +00:00
|
|
|
if (is_array($headers[0])) {
|
2004-06-02 05:35:51 +00:00
|
|
|
return array('name' => $headers[0]['data'], 'sql' => $headers[0]['field']);
|
2004-05-29 17:38:54 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return array('name' => $headers[0]);
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tablesort_get_sort($headers) {
|
|
|
|
if ($_GET['sort']) {
|
2004-03-30 21:01:19 +00:00
|
|
|
return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
// user has not specified a sort. check module for default and if none, use 'asc'
|
|
|
|
else {
|
|
|
|
foreach ($headers as $header) {
|
2004-05-25 05:03:03 +00:00
|
|
|
if (is_array($header) && isset($header['sort'])) {
|
2004-01-25 10:07:24 +00:00
|
|
|
return $header['sort'];
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'asc';
|
|
|
|
}
|
|
|
|
|
2003-09-14 18:28:17 +00:00
|
|
|
?>
|