$cell['data'])); if ($cell['data'] == $ts['name']) { $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc'); $cell['class'] = 'active'; $title = ($ts['sort'] == 'asc' ? t('sort ascending') : t('sort descending')); $image = ' '. theme('image', 'misc/arrow-'. $ts['sort'] .'.png', t('sort icon'), $title); } else { // If the user clicks a different header, we want to sort ascending initially. $ts['sort'] = 'asc'; $image = ''; } $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']); } return $cell; } /** * Format a table cell. * * Adds a class attribute to all cells in the currently active column. * * @param $cell * The cell to format. * @param $header * An array of column headers in the format described in theme_table(). * @param $ts * The current table sort context as returned from tablesort_init(). * @param $i * The index of the cell's table column. * @return * A properly formatted cell, ready for _theme_table_cell(). */ function tablesort_cell($cell, $header, $ts, $i) { if (isset($header[$i]) && $header[$i]['data'] == $ts['name'] && $header[$i]['field']) { if (is_array($cell)) { if (isset($cell['class'])) { $cell['class'] .= ' active'; } else { $cell['class'] = 'active'; } } else { $cell = array('data' => $cell, 'class' => 'active'); } } return $cell; } /** * Compose a query string to append to table sorting requests. * * @return * A query string that consists of all components of the current page request * except for those pertaining to table sorting. */ function tablesort_get_querystring() { $cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET : $_POST; $query_string = ''; foreach ($cgi as $key => $val) { if ($key != 'order' && $key != 'sort' && $key != 'q') { $query_string .= '&'. $key .'='. $val; } } return $query_string; } /** * Determine the current sort criterion. * * @param $headers * An array of column headers in the format described in theme_table(). * @return * An associative array describing the criterion, containing the keys: * - "name": The localized title of the table column. * - "sql": The name of the database field to sort on. */ function tablesort_get_order($headers) { $order = isset($_GET['order']) ? $_GET['order'] : ''; foreach ($headers as $header) { if (isset($header['data']) && $order == $header['data']) { return array('name' => $header['data'], 'sql' => $header['field']); } if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) { $default = array('name' => $header['data'], 'sql' => $header['field']); } } if (isset($default)) { return $default; } else { // The first column specified is initial 'order by' field unless otherwise specified if (is_array($headers[0])) { return array('name' => $headers[0]['data'], 'sql' => $headers[0]['field']); } else { return array('name' => $headers[0]); } } } /** * Determine the current sort direction. * * @param $headers * An array of column headers in the format described in theme_table(). * @return * The current sort direction ("asc" or "desc"). */ function tablesort_get_sort($headers) { if (isset($_GET['sort'])) { return ($_GET['sort'] == 'desc') ? 'desc' : 'asc'; } // User has not specified a sort. Use default if specified; otherwise use "asc". else { foreach ($headers as $header) { if (is_array($header) && array_key_exists('sort', $header)) { return $header['sort']; } } } return 'asc'; } ?>