[ Index ] |
|
Code source de Drupal 5.3 |
1 <?php 2 // $Id: tablesort.inc,v 1.43.2.1 2007/06/16 22:29:25 drumm Exp $ 3 4 /** 5 * @file 6 * Functions to aid in the creation of sortable tables. 7 * 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. 10 */ 11 12 /** 13 * Initialize the table sort context. 14 */ 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(); 19 return $ts; 20 } 21 22 /** 23 * Create an SQL sort clause. 24 * 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 27 * by the user. 28 * 29 * @param $header 30 * An array of column headers in the format described in theme_table(). 31 * @param $before 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. 34 * @return 35 * An SQL string to append to the end of a query. 36 * 37 * @ingroup database 38 */ 39 function tablesort_sql($header, $before = '') { 40 $ts = tablesort_init($header); 41 if ($ts['sql']) { 42 $sql = db_escape_string($ts['sql']); 43 $sort = drupal_strtoupper(db_escape_string($ts['sort'])); 44 return " ORDER BY $before $sql $sort"; 45 } 46 } 47 48 /** 49 * Format a column header. 50 * 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. 53 * 54 * @param $cell 55 * The cell to format. 56 * @param $header 57 * An array of column headers in the format described in theme_table(). 58 * @param $ts 59 * The current table sort context as returned from tablesort_init(). 60 * @return 61 * A properly formatted cell, ready for _theme_table_cell(). 62 */ 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'; 71 } 72 else { 73 $cell['class'] = 'active'; 74 } 75 $image = theme('tablesort_indicator', $ts['sort']); 76 } 77 else { 78 // If the user clicks a different header, we want to sort ascending initially. 79 $ts['sort'] = 'asc'; 80 $image = ''; 81 } 82 83 if (!empty($ts['query_string'])) { 84 $ts['query_string'] = '&'. $ts['query_string']; 85 } 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); 87 88 unset($cell['field'], $cell['sort']); 89 } 90 return $cell; 91 } 92 93 /** 94 * Format a table cell. 95 * 96 * Adds a class attribute to all cells in the currently active column. 97 * 98 * @param $cell 99 * The cell to format. 100 * @param $header 101 * An array of column headers in the format described in theme_table(). 102 * @param $ts 103 * The current table sort context as returned from tablesort_init(). 104 * @param $i 105 * The index of the cell's table column. 106 * @return 107 * A properly formatted cell, ready for _theme_table_cell(). 108 */ 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'; 114 } 115 else { 116 $cell['class'] = 'active'; 117 } 118 } 119 else { 120 $cell = array('data' => $cell, 'class' => 'active'); 121 } 122 } 123 return $cell; 124 } 125 126 /** 127 * Compose a query string to append to table sorting requests. 128 * 129 * @return 130 * A query string that consists of all components of the current page request 131 * except for those pertaining to table sorting. 132 */ 133 function tablesort_get_querystring() { 134 return drupal_query_string_encode($_REQUEST, array_merge(array('q', 'sort', 'order'), array_keys($_COOKIE))); 135 } 136 137 /** 138 * Determine the current sort criterion. 139 * 140 * @param $headers 141 * An array of column headers in the format described in theme_table(). 142 * @return 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. 146 */ 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'] : ''); 152 } 153 154 if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) { 155 $default = array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : ''); 156 } 157 } 158 159 if (isset($default)) { 160 return $default; 161 } 162 else { 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']); 166 } 167 else { 168 return array('name' => $headers[0]); 169 } 170 } 171 } 172 173 /** 174 * Determine the current sort direction. 175 * 176 * @param $headers 177 * An array of column headers in the format described in theme_table(). 178 * @return 179 * The current sort direction ("asc" or "desc"). 180 */ 181 function tablesort_get_sort($headers) { 182 if (isset($_GET['sort'])) { 183 return ($_GET['sort'] == 'desc') ? 'desc' : 'asc'; 184 } 185 // User has not specified a sort. Use default if specified; otherwise use "asc". 186 else { 187 foreach ($headers as $header) { 188 if (is_array($header) && array_key_exists('sort', $header)) { 189 return $header['sort']; 190 } 191 } 192 } 193 return 'asc'; 194 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Nov 30 16:20:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |