annotate includes/tablesort.inc @ 5:2427550111ae 6.2

Drupal 6.2
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:30:08 +0100
parents c1f4ac30525a
children
rev   line source
webmaster@1 1 <?php
webmaster@1 2 // $Id: tablesort.inc,v 1.47 2008/01/04 09:31:48 goba Exp $
webmaster@1 3
webmaster@1 4 /**
webmaster@1 5 * @file
webmaster@1 6 * Functions to aid in the creation of sortable tables.
webmaster@1 7 *
webmaster@1 8 * All tables created with a call to theme('table') have the option of having
webmaster@1 9 * column headers that the user can click on to sort the table by that column.
webmaster@1 10 */
webmaster@1 11
webmaster@1 12 /**
webmaster@1 13 * Initialize the table sort context.
webmaster@1 14 */
webmaster@1 15 function tablesort_init($header) {
webmaster@1 16 $ts = tablesort_get_order($header);
webmaster@1 17 $ts['sort'] = tablesort_get_sort($header);
webmaster@1 18 $ts['query_string'] = tablesort_get_querystring();
webmaster@1 19 return $ts;
webmaster@1 20 }
webmaster@1 21
webmaster@1 22 /**
webmaster@1 23 * Create an SQL sort clause.
webmaster@1 24 *
webmaster@1 25 * This function produces the ORDER BY clause to insert in your SQL queries,
webmaster@1 26 * assuring that the returned database table rows match the sort order chosen
webmaster@1 27 * by the user.
webmaster@1 28 *
webmaster@1 29 * @param $header
webmaster@1 30 * An array of column headers in the format described in theme_table().
webmaster@1 31 * @param $before
webmaster@1 32 * An SQL string to insert after ORDER BY and before the table sorting code.
webmaster@1 33 * Useful for sorting by important attributes like "sticky" first.
webmaster@1 34 * @return
webmaster@1 35 * An SQL string to append to the end of a query.
webmaster@1 36 *
webmaster@1 37 * @ingroup database
webmaster@1 38 */
webmaster@1 39 function tablesort_sql($header, $before = '') {
webmaster@1 40 $ts = tablesort_init($header);
webmaster@1 41 if ($ts['sql']) {
webmaster@1 42 // Based on code from db_escape_table(), but this can also contain a dot.
webmaster@1 43 $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
webmaster@1 44
webmaster@1 45 // Sort order can only be ASC or DESC.
webmaster@1 46 $sort = drupal_strtoupper($ts['sort']);
webmaster@1 47 $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';
webmaster@1 48
webmaster@1 49 return " ORDER BY $before $field $sort";
webmaster@1 50 }
webmaster@1 51 }
webmaster@1 52
webmaster@1 53 /**
webmaster@1 54 * Format a column header.
webmaster@1 55 *
webmaster@1 56 * If the cell in question is the column header for the current sort criterion,
webmaster@1 57 * it gets special formatting. All possible sort criteria become links.
webmaster@1 58 *
webmaster@1 59 * @param $cell
webmaster@1 60 * The cell to format.
webmaster@1 61 * @param $header
webmaster@1 62 * An array of column headers in the format described in theme_table().
webmaster@1 63 * @param $ts
webmaster@1 64 * The current table sort context as returned from tablesort_init().
webmaster@1 65 * @return
webmaster@1 66 * A properly formatted cell, ready for _theme_table_cell().
webmaster@1 67 */
webmaster@1 68 function tablesort_header($cell, $header, $ts) {
webmaster@1 69 // Special formatting for the currently sorted column header.
webmaster@1 70 if (is_array($cell) && isset($cell['field'])) {
webmaster@1 71 $title = t('sort by @s', array('@s' => $cell['data']));
webmaster@1 72 if ($cell['data'] == $ts['name']) {
webmaster@1 73 $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
webmaster@1 74 if (isset($cell['class'])) {
webmaster@1 75 $cell['class'] .= ' active';
webmaster@1 76 }
webmaster@1 77 else {
webmaster@1 78 $cell['class'] = 'active';
webmaster@1 79 }
webmaster@1 80 $image = theme('tablesort_indicator', $ts['sort']);
webmaster@1 81 }
webmaster@1 82 else {
webmaster@1 83 // If the user clicks a different header, we want to sort ascending initially.
webmaster@1 84 $ts['sort'] = 'asc';
webmaster@1 85 $image = '';
webmaster@1 86 }
webmaster@1 87
webmaster@1 88 if (!empty($ts['query_string'])) {
webmaster@1 89 $ts['query_string'] = '&'. $ts['query_string'];
webmaster@1 90 }
webmaster@1 91 $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], 'html' => TRUE));
webmaster@1 92
webmaster@1 93 unset($cell['field'], $cell['sort']);
webmaster@1 94 }
webmaster@1 95 return $cell;
webmaster@1 96 }
webmaster@1 97
webmaster@1 98 /**
webmaster@1 99 * Format a table cell.
webmaster@1 100 *
webmaster@1 101 * Adds a class attribute to all cells in the currently active column.
webmaster@1 102 *
webmaster@1 103 * @param $cell
webmaster@1 104 * The cell to format.
webmaster@1 105 * @param $header
webmaster@1 106 * An array of column headers in the format described in theme_table().
webmaster@1 107 * @param $ts
webmaster@1 108 * The current table sort context as returned from tablesort_init().
webmaster@1 109 * @param $i
webmaster@1 110 * The index of the cell's table column.
webmaster@1 111 * @return
webmaster@1 112 * A properly formatted cell, ready for _theme_table_cell().
webmaster@1 113 */
webmaster@1 114 function tablesort_cell($cell, $header, $ts, $i) {
webmaster@1 115 if (isset($header[$i]['data']) && $header[$i]['data'] == $ts['name'] && !empty($header[$i]['field'])) {
webmaster@1 116 if (is_array($cell)) {
webmaster@1 117 if (isset($cell['class'])) {
webmaster@1 118 $cell['class'] .= ' active';
webmaster@1 119 }
webmaster@1 120 else {
webmaster@1 121 $cell['class'] = 'active';
webmaster@1 122 }
webmaster@1 123 }
webmaster@1 124 else {
webmaster@1 125 $cell = array('data' => $cell, 'class' => 'active');
webmaster@1 126 }
webmaster@1 127 }
webmaster@1 128 return $cell;
webmaster@1 129 }
webmaster@1 130
webmaster@1 131 /**
webmaster@1 132 * Compose a query string to append to table sorting requests.
webmaster@1 133 *
webmaster@1 134 * @return
webmaster@1 135 * A query string that consists of all components of the current page request
webmaster@1 136 * except for those pertaining to table sorting.
webmaster@1 137 */
webmaster@1 138 function tablesort_get_querystring() {
webmaster@1 139 return drupal_query_string_encode($_REQUEST, array_merge(array('q', 'sort', 'order'), array_keys($_COOKIE)));
webmaster@1 140 }
webmaster@1 141
webmaster@1 142 /**
webmaster@1 143 * Determine the current sort criterion.
webmaster@1 144 *
webmaster@1 145 * @param $headers
webmaster@1 146 * An array of column headers in the format described in theme_table().
webmaster@1 147 * @return
webmaster@1 148 * An associative array describing the criterion, containing the keys:
webmaster@1 149 * - "name": The localized title of the table column.
webmaster@1 150 * - "sql": The name of the database field to sort on.
webmaster@1 151 */
webmaster@1 152 function tablesort_get_order($headers) {
webmaster@1 153 $order = isset($_GET['order']) ? $_GET['order'] : '';
webmaster@1 154 foreach ($headers as $header) {
webmaster@1 155 if (isset($header['data']) && $order == $header['data']) {
webmaster@1 156 return array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
webmaster@1 157 }
webmaster@1 158
webmaster@1 159 if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
webmaster@1 160 $default = array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
webmaster@1 161 }
webmaster@1 162 }
webmaster@1 163
webmaster@1 164 if (isset($default)) {
webmaster@1 165 return $default;
webmaster@1 166 }
webmaster@1 167 else {
webmaster@1 168 // The first column specified is initial 'order by' field unless otherwise specified
webmaster@1 169 if (is_array($headers[0])) {
webmaster@1 170 $headers[0] += array('data' => NULL, 'field' => NULL);
webmaster@1 171 return array('name' => $headers[0]['data'], 'sql' => $headers[0]['field']);
webmaster@1 172 }
webmaster@1 173 else {
webmaster@1 174 return array('name' => $headers[0]);
webmaster@1 175 }
webmaster@1 176 }
webmaster@1 177 }
webmaster@1 178
webmaster@1 179 /**
webmaster@1 180 * Determine the current sort direction.
webmaster@1 181 *
webmaster@1 182 * @param $headers
webmaster@1 183 * An array of column headers in the format described in theme_table().
webmaster@1 184 * @return
webmaster@1 185 * The current sort direction ("asc" or "desc").
webmaster@1 186 */
webmaster@1 187 function tablesort_get_sort($headers) {
webmaster@1 188 if (isset($_GET['sort'])) {
webmaster@1 189 return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
webmaster@1 190 }
webmaster@1 191 // User has not specified a sort. Use default if specified; otherwise use "asc".
webmaster@1 192 else {
webmaster@1 193 foreach ($headers as $header) {
webmaster@1 194 if (is_array($header) && array_key_exists('sort', $header)) {
webmaster@1 195 return $header['sort'];
webmaster@1 196 }
webmaster@1 197 }
webmaster@1 198 }
webmaster@1 199 return 'asc';
webmaster@1 200 }