2 // $Id: tablesort.inc 144 2007-03-28 07:52:20Z thierry $
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 $cell['class'] = 'active';
70 $image = theme('tablesort_indicator', $ts['sort']);
73 // If the user clicks a different header, we want to sort ascending initially.
78 if (!empty($ts['query_string'])) {
79 $ts['query_string'] = '&'. $ts['query_string'];
81 $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('title' => $title), 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], NULL, FALSE, TRUE);
83 unset($cell['field'], $cell['sort']);
89 * Format a table cell.
91 * Adds a class attribute to all cells in the currently active column.
96 * An array of column headers in the format described in theme_table().
98 * The current table sort context as returned from tablesort_init().
100 * The index of the cell's table column.
102 * A properly formatted cell, ready for _theme_table_cell().
104 function tablesort_cell($cell, $header, $ts, $i) {
105 if (isset($header[$i]) && $header[$i]['data'] == $ts['name'] && $header[$i]['field']) {
106 if (is_array($cell)) {
107 if (isset($cell['class'])) {
108 $cell['class'] .= ' active';
111 $cell['class'] = 'active';
115 $cell = array('data' => $cell, 'class' => 'active');
122 * Compose a query string to append to table sorting requests.
125 * A query string that consists of all components of the current page request
126 * except for those pertaining to table sorting.
128 function tablesort_get_querystring() {
129 return drupal_query_string_encode($_REQUEST, array_merge(array('q', 'sort', 'order'), array_keys($_COOKIE)));
133 * Determine the current sort criterion.
136 * An array of column headers in the format described in theme_table().
138 * An associative array describing the criterion, containing the keys:
139 * - "name": The localized title of the table column.
140 * - "sql": The name of the database field to sort on.
142 function tablesort_get_order($headers) {
143 $order = isset($_GET['order']) ? $_GET['order'] : '';
144 foreach ($headers as $header) {
145 if (isset($header['data']) && $order == $header['data']) {
146 return array('name' => $header['data'], 'sql' => $header['field']);
149 if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
150 $default = array('name' => $header['data'], 'sql' => $header['field']);
154 if (isset($default)) {
158 // The first column specified is initial 'order by' field unless otherwise specified
159 if (is_array($headers[0])) {
160 return array('name' => $headers[0]['data'], 'sql' => $headers[0]['field']);
163 return array('name' => $headers[0]);
169 * Determine the current sort direction.
172 * An array of column headers in the format described in theme_table().
174 * The current sort direction ("asc" or "desc").
176 function tablesort_get_sort($headers) {
177 if (isset($_GET['sort'])) {
178 return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
180 // User has not specified a sort. Use default if specified; otherwise use "asc".
182 foreach ($headers as $header) {
183 if (is_array($header) && array_key_exists('sort', $header)) {
184 return $header['sort'];