tablesort.inc

  1. 7.x drupal-7.x/includes/tablesort.inc
  2. 6.x drupal-6.x/includes/tablesort.inc

Functions to aid in the creation of sortable tables.

All tables created with a call to theme('table') have the option of having column headers that the user can click on to sort the table by that column.

File

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