- Rollback of #94058: see issue.

5.x
Dries Buytaert 2006-11-17 06:53:31 +00:00
parent 3cc177dd97
commit 4f0f376430
2 changed files with 16 additions and 32 deletions

View File

@ -12,14 +12,10 @@
/** /**
* Initialize the table sort context. * Initialize the table sort context.
*/ */
function tablesort_init($header, $id) { function tablesort_init($header) {
if (strlen($id) > 0) { $ts = tablesort_get_order($header);
$id = '-'. $id; $ts['sort'] = tablesort_get_sort($header);
} $ts['query_string'] = tablesort_get_querystring();
$ts = tablesort_get_order($header, $id);
$ts['sort'] = tablesort_get_sort($header, $id);
$ts['query_string'] = tablesort_get_querystring($id);
$ts['id'] = $id;
return $ts; return $ts;
} }
@ -35,16 +31,13 @@ function tablesort_init($header, $id) {
* @param $before * @param $before
* An SQL string to insert after ORDER BY and before the table sorting code. * An SQL string to insert after ORDER BY and before the table sorting code.
* Useful for sorting by important attributes like "sticky" first. * Useful for sorting by important attributes like "sticky" first.
* @param $id
* A unique id used to identify the table. Use this in the case where
* multiple tables appear on one page.
* @return * @return
* An SQL string to append to the end of a query. * An SQL string to append to the end of a query.
* *
* @ingroup database * @ingroup database
*/ */
function tablesort_sql($header, $before = '', $id = '') { function tablesort_sql($header, $before = '') {
$ts = tablesort_init($header, $id); $ts = tablesort_init($header);
if ($ts['sql']) { if ($ts['sql']) {
$sql = db_escape_string($ts['sql']); $sql = db_escape_string($ts['sql']);
$sort = drupal_strtoupper(db_escape_string($ts['sort'])); $sort = drupal_strtoupper(db_escape_string($ts['sort']));
@ -85,7 +78,7 @@ function tablesort_header($cell, $header, $ts) {
if (!empty($ts['query_string'])) { if (!empty($ts['query_string'])) {
$ts['query_string'] = '&'. $ts['query_string']; $ts['query_string'] = '&'. $ts['query_string'];
} }
$cell['data'] = l($cell['data'] . $image, $_GET['q'], array('title' => $title), 'sort'. $ts['id'] .'='. $ts['sort'] .'&order'. $ts['id'] .'='. urlencode($cell['data']) . $ts['query_string'], NULL, FALSE, TRUE); $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('title' => $title), 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], NULL, FALSE, TRUE);
unset($cell['field'], $cell['sort']); unset($cell['field'], $cell['sort']);
} }
@ -128,14 +121,12 @@ function tablesort_cell($cell, $header, $ts, $i) {
/** /**
* Compose a query string to append to table sorting requests. * Compose a query string to append to table sorting requests.
* *
* @param $id
* A unique id used to identify the table.
* @return * @return
* A query string that consists of all components of the current page request * A query string that consists of all components of the current page request
* except for those pertaining to table sorting. * except for those pertaining to table sorting.
*/ */
function tablesort_get_querystring($id) { function tablesort_get_querystring() {
return drupal_query_string_encode($_REQUEST, array_merge(array('q', 'sort'. $id, 'order'. $id), array_keys($_COOKIE))); return drupal_query_string_encode($_REQUEST, array_merge(array('q', 'sort', 'order'), array_keys($_COOKIE)));
} }
/** /**
@ -143,15 +134,13 @@ function tablesort_get_querystring($id) {
* *
* @param $headers * @param $headers
* An array of column headers in the format described in theme_table(). * An array of column headers in the format described in theme_table().
* @param $id
* A unique id used to identify the table.
* @return * @return
* An associative array describing the criterion, containing the keys: * An associative array describing the criterion, containing the keys:
* - "name": The localized title of the table column. * - "name": The localized title of the table column.
* - "sql": The name of the database field to sort on. * - "sql": The name of the database field to sort on.
*/ */
function tablesort_get_order($headers, $id) { function tablesort_get_order($headers) {
$order = isset($_GET['order'. $id]) ? $_GET['order'. $id] : ''; $order = isset($_GET['order']) ? $_GET['order'] : '';
foreach ($headers as $header) { foreach ($headers as $header) {
if (isset($header['data']) && $order == $header['data']) { if (isset($header['data']) && $order == $header['data']) {
return array('name' => $header['data'], 'sql' => $header['field']); return array('name' => $header['data'], 'sql' => $header['field']);
@ -181,14 +170,12 @@ function tablesort_get_order($headers, $id) {
* *
* @param $headers * @param $headers
* An array of column headers in the format described in theme_table(). * An array of column headers in the format described in theme_table().
* @param $id
* A unique id used to identify the table.
* @return * @return
* The current sort direction ("asc" or "desc"). * The current sort direction ("asc" or "desc").
*/ */
function tablesort_get_sort($headers, $id) { function tablesort_get_sort($headers) {
if (isset($_GET['sort'. $id])) { if (isset($_GET['sort'])) {
return ($_GET['sort'. $id] == 'desc') ? 'desc' : 'asc'; return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
} }
// User has not specified a sort. Use default if specified; otherwise use "asc". // User has not specified a sort. Use default if specified; otherwise use "asc".
else { else {

View File

@ -712,13 +712,10 @@ function theme_submenu($links) {
* An array of HTML attributes to apply to the table tag. * An array of HTML attributes to apply to the table tag.
* @param $caption * @param $caption
* A localized string to use for the <caption> tag. * A localized string to use for the <caption> tag.
* @param $id
* A unique id used to identify the table. Use this in the case where
* multiple tables appear on one page.
* @return * @return
* An HTML string representing the table. * An HTML string representing the table.
*/ */
function theme_table($header, $rows, $attributes = array(), $caption = NULL, $id = '') { function theme_table($header, $rows, $attributes = array(), $caption = NULL) {
$output = '<table'. drupal_attributes($attributes) .">\n"; $output = '<table'. drupal_attributes($attributes) .">\n";
if (isset($caption)) { if (isset($caption)) {
@ -727,7 +724,7 @@ function theme_table($header, $rows, $attributes = array(), $caption = NULL, $id
// Format the table header: // Format the table header:
if (count($header)) { if (count($header)) {
$ts = tablesort_init($header, $id); $ts = tablesort_init($header);
$output .= ' <thead><tr>'; $output .= ' <thead><tr>';
foreach ($header as $cell) { foreach ($header as $cell) {
$cell = tablesort_header($cell, $header, $ts); $cell = tablesort_header($cell, $header, $ts);