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) {
|
2003-09-20 19:41:35 +00:00
|
|
|
static $ts;
|
2003-08-21 15:47:50 +00:00
|
|
|
|
2003-09-20 19:41:35 +00:00
|
|
|
if (empty($ts)) {
|
2004-01-25 10:07:24 +00:00
|
|
|
$ts['order'] = tablesort_get_order($header);
|
|
|
|
$ts['order_sql'] = tablesort_get_order_sql($header, $ts['order']);
|
|
|
|
$ts['sort'] = tablesort_get_sort($header);
|
|
|
|
$ts['query_string'] = tablesort_get_querystring();
|
2003-09-20 19:41:35 +00:00
|
|
|
}
|
2003-09-20 19:24:27 +00:00
|
|
|
|
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-01-25 10:07:24 +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-01-25 10:07:24 +00:00
|
|
|
return " ORDER BY ". $ts['order_sql']. " ". strtoupper($ts['sort']);
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function tablesort($cell, $header) {
|
|
|
|
$ts = tablesort_init($header);
|
2004-01-25 10:07:24 +00:00
|
|
|
$title = t("sort by %s", array("%s" => $cell['data']));
|
2003-08-21 15:47:50 +00:00
|
|
|
|
|
|
|
// special formatting for the currently sorted column header
|
2004-01-25 10:07:24 +00:00
|
|
|
if ($cell['data'] == $ts['order']) {
|
|
|
|
$cell['class'] = 'cell-highlight';
|
2004-02-10 19:46:16 +00:00
|
|
|
$image = ' <img src="' . theme('image', 'arrow-' . $ts['sort'] . '.gif') . '" alt="'. t('sort icon') .'" />';
|
2004-02-05 22:58:59 +00:00
|
|
|
$title = ($ts['sort'] == 'asc' ? t("sort ascending") : t("sort descending"));
|
2003-10-13 19:18:39 +00:00
|
|
|
} else {
|
|
|
|
// If the user clicks a different header, we want to sort ascending initially.
|
2004-01-25 10:07:24 +00:00
|
|
|
$ts['sort'] = "asc";
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
2003-08-27 05:49:52 +00:00
|
|
|
|
2004-02-01 10:39:23 +00:00
|
|
|
$cell['data'] = l($cell['data'] . $image, $_GET['q'], array("title" => $title), "sort=". $ts['sort']. "&order=". urlencode($cell['data']). $ts['query_string']);
|
2004-01-29 06:42:03 +00:00
|
|
|
|
|
|
|
unset($cell['field']);
|
|
|
|
unset($cell['sort']);
|
2004-02-01 10:39:23 +00:00
|
|
|
|
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-02-01 10:39:23 +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']) {
|
|
|
|
return $header['data'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($header['sort'] == 'asc' || $header['sort'] == 'desc') {
|
|
|
|
$default = $header['data'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2003-10-27 21:50:16 +00:00
|
|
|
$first = reset($headers);
|
2003-10-27 21:45:55 +00:00
|
|
|
return $first['data'];
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tablesort_get_order_sql($header, $order) {
|
|
|
|
foreach ($header as $cell) {
|
2004-01-25 10:07:24 +00:00
|
|
|
if ($cell['data'] == $order) {
|
|
|
|
return $cell['field'];
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tablesort_get_sort($headers) {
|
|
|
|
if ($_GET['sort']) {
|
|
|
|
return ($_GET['sort'] == 'desc') ? 'asc' : 'desc';
|
|
|
|
}
|
|
|
|
// user has not specified a sort. check module for default and if none, use 'asc'
|
|
|
|
else {
|
|
|
|
foreach ($headers as $header) {
|
2004-01-25 10:07:24 +00:00
|
|
|
if (isset($header['sort'])) {
|
|
|
|
return $header['sort'];
|
2003-08-21 15:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'asc';
|
|
|
|
}
|
|
|
|
|
2003-09-14 18:28:17 +00:00
|
|
|
?>
|