2 // $Id: tablesort.inc,v 1.43.2.1 2007/06/16 22:29:25 drumm Exp $
6 * Functions to aid in the creation of sortable tables.
8 * All tables created with a call to theme('table') have the option of having
9 * column headers that the user can click on to sort the table by that column.
13 * Initialize the table sort context.
15 function tablesort_init($header) {
16 $ts = tablesort_get_order($header);
17 $ts['sort'] = tablesort_get_sort($header);
18 $ts['query_string'] = tablesort_get_querystring();
23 * Create an SQL sort clause.
25 * This function produces the ORDER BY clause to insert in your SQL queries,
26 * assuring that the returned database table rows match the sort order chosen
30 * An array of column headers in the format described in theme_table().
32 * An SQL string to insert after ORDER BY and before the table sorting code.
33 * Useful for sorting by important attributes like "sticky" first.
35 * An SQL string to append to the end of a query.
39 function tablesort_sql($header, $before = '') {
40 $ts = tablesort_init($header);
42 $sql = db_escape_string($ts['sql']);
43 $sort = drupal_strtoupper(db_escape_string($ts['sort']));
44 return " ORDER BY $before $sql $sort";
49 * Format a column header.
51 * If the cell in question is the column header for the current sort criterion,
52 * it gets special formatting. All possible sort criteria become links.
57 * An array of column headers in the format described in theme_table().
59 * The current table sort context as returned from tablesort_init().
61 * A properly formatted cell, ready for _theme_table_cell().
63 function tablesort_header($cell, $header, $ts) {
64 // Special formatting for the currently sorted column header.
65 if (is_array($cell) && isset($cell['field'])) {
66 $title = t('sort by @s', array('@s' => $cell['data']));
67 if ($cell['data'] == $ts['name']) {
68 $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
69 if (isset($cell['class'])) {
70 $cell['class'] .= ' active';
73 $cell['class'] = 'active';
75 $image = theme('tablesort_indicator', $ts['sort']);
78 // If the user clicks a different header, we want to sort ascending initially.
83 if (!empty($ts['query_string'])) {
84 $ts['query_string'] = '&'. $ts['query_string'];
86 $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('title' => $title), 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], NULL, FALSE, TRUE);
88 unset($cell['field'], $cell['sort']);
94 * Format a table cell.
96 * Adds a class attribute to all cells in the currently active column.
101 * An array of column headers in the format described in theme_table().
103 * The current table sort context as returned from tablesort_init().
105 * The index of the cell's table column.
107 * A properly formatted cell, ready for _theme_table_cell().
109 function tablesort_cell($cell, $header, $ts, $i) {
110 if (isset($header[$i]) && $header[$i]['data'] == $ts['name'] && $header[$i]['field']) {
111 if (is_array($cell)) {
112 if (isset($cell['class'])) {
113 $cell['class'] .= ' active';
116 $cell['class'] = 'active';
120 $cell = array('data' => $cell, 'class' => 'active');
127 * Compose a query string to append to table sorting requests.
130 * A query string that consists of all components of the current page request
131 * except for those pertaining to table sorting.
133 function tablesort_get_querystring() {
134 return drupal_query_string_encode($_REQUEST, array_merge(array('q', 'sort', 'order'), array_keys($_COOKIE)));
138 * Determine the current sort criterion.
141 * An array of column headers in the format described in theme_table().
143 * An associative array describing the criterion, containing the keys:
144 * - "name": The localized title of the table column.
145 * - "sql": The name of the database field to sort on.
147 function tablesort_get_order($headers) {
148 $order = isset($_GET['order']) ? $_GET['order'] : '';
149 foreach ($headers as $header) {
150 if (isset($header['data']) && $order == $header['data']) {
151 return array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
154 if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
155 $default = array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
159 if (isset($default)) {
163 // The first column specified is initial 'order by' field unless otherwise specified
164 if (is_array($headers[0])) {
165 return array('name' => $headers[0]['data'], 'sql' => $headers[0]['field']);
168 return array('name' => $headers[0]);
174 * Determine the current sort direction.
177 * An array of column headers in the format described in theme_table().
179 * The current sort direction ("asc" or "desc").
181 function tablesort_get_sort($headers) {
182 if (isset($_GET['sort'])) {
183 return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
185 // User has not specified a sort. Use default if specified; otherwise use "asc".
187 foreach ($headers as $header) {
188 if (is_array($header) && array_key_exists('sort', $header)) {
189 return $header['sort'];