tablesort.inc

  1. drupal
    1. 4.6 includes/tablesort.inc
    2. 4.7 includes/tablesort.inc
    3. 5 includes/tablesort.inc
    4. 6 includes/tablesort.inc
    5. 7 includes/tablesort.inc
    6. 8 core/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.

Classes

NameDescription
TableSortQuery extender class for tablesort queries.

Functions & methods

NameDescription
tablesort_cellFormat a table cell.
tablesort_get_orderDetermine the current sort criterion.
tablesort_get_query_parametersCompose a URL query parameter array for table sorting links.
tablesort_get_sortDetermine the current sort direction.
tablesort_headerFormat a column header.
tablesort_initInitialize the table sort context.

File

core/includes/tablesort.inc
View source
  1. <?php
  2. use DrupalCoreDatabaseConnection;
  3. use DrupalCoreDatabaseQuerySelectExtender;
  4. use DrupalCoreDatabaseQuerySelectInterface;
  5. /**
  6. * @file
  7. * Functions to aid in the creation of sortable tables.
  8. *
  9. * All tables created with a call to theme('table') have the option of having
  10. * column headers that the user can click on to sort the table by that column.
  11. */
  12. /**
  13. * Query extender class for tablesort queries.
  14. */
  15. class TableSort extends SelectExtender {
  16. /**
  17. * The array of fields that can be sorted by.
  18. *
  19. * @var array
  20. */
  21. protected $header = array();
  22. public function __construct(SelectInterface $query, Connection $connection) {
  23. parent::__construct($query, $connection);
  24. // Add convenience tag to mark that this is an extended query. We have to
  25. // do this in the constructor to ensure that it is set before preExecute()
  26. // gets called.
  27. $this->addTag('tablesort');
  28. }
  29. /**
  30. * Order the query based on a header array.
  31. *
  32. * @see theme_table()
  33. * @param $header
  34. * Table header array.
  35. * @return SelectQueryInterface
  36. * The called object.
  37. */
  38. public function orderByHeader(Array $header) {
  39. $this->header = $header;
  40. $ts = $this->init();
  41. if (!empty($ts['sql'])) {
  42. // Based on code from db_escape_table(), but this can also contain a dot.
  43. $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
  44. // Sort order can only be ASC or DESC.
  45. $sort = drupal_strtoupper($ts['sort']);
  46. $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';
  47. $this->orderBy($field, $sort);
  48. }
  49. return $this;
  50. }
  51. /**
  52. * Initialize the table sort context.
  53. */
  54. protected function init() {
  55. $ts = $this->order();
  56. $ts['sort'] = $this->getSort();
  57. $ts['query'] = $this->getQueryParameters();
  58. return $ts;
  59. }
  60. /**
  61. * Determine the current sort direction.
  62. *
  63. * @param $headers
  64. * An array of column headers in the format described in theme_table().
  65. * @return
  66. * The current sort direction ("asc" or "desc").
  67. */
  68. protected function getSort() {
  69. return tablesort_get_sort($this->header);
  70. }
  71. /**
  72. * Compose a URL query parameter array to append to table sorting requests.
  73. *
  74. * @return
  75. * A URL query parameter array that consists of all components of the current
  76. * page request except for those pertaining to table sorting.
  77. *
  78. * @see tablesort_get_query_parameters()
  79. */
  80. protected function getQueryParameters() {
  81. return tablesort_get_query_parameters();
  82. }
  83. /**
  84. * Determine the current sort criterion.
  85. *
  86. * @param $headers
  87. * An array of column headers in the format described in theme_table().
  88. * @return
  89. * An associative array describing the criterion, containing the keys:
  90. * - "name": The localized title of the table column.
  91. * - "sql": The name of the database field to sort on.
  92. */
  93. protected function order() {
  94. return tablesort_get_order($this->header);
  95. }
  96. }
  97. /**
  98. * Initialize the table sort context.
  99. */
  100. function tablesort_init($header) {
  101. $ts = tablesort_get_order($header);
  102. $ts['sort'] = tablesort_get_sort($header);
  103. $ts['query'] = tablesort_get_query_parameters();
  104. return $ts;
  105. }
  106. /**
  107. * Format a column header.
  108. *
  109. * If the cell in question is the column header for the current sort criterion,
  110. * it gets special formatting. All possible sort criteria become links.
  111. *
  112. * @param $cell
  113. * The cell to format.
  114. * @param $header
  115. * An array of column headers in the format described in theme_table().
  116. * @param $ts
  117. * The current table sort context as returned from tablesort_init().
  118. * @return
  119. * A properly formatted cell, ready for _theme_table_cell().
  120. */
  121. function tablesort_header($cell, $header, $ts) {
  122. // Special formatting for the currently sorted column header.
  123. if (is_array($cell) && isset($cell['field'])) {
  124. $title = t('sort by @s', array('@s' => $cell['data']));
  125. if ($cell['data'] == $ts['name']) {
  126. // aria-sort is a WAI-ARIA property that indicates if items in a table
  127. // or grid are sorted in ascending or descending order. See
  128. // http://www.w3.org/TR/wai-aria/states_and_properties#aria-sort
  129. $cell['aria-sort'] = ($ts['sort'] == 'asc') ? 'ascending' : 'descending';
  130. $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
  131. $cell['class'][] = 'active';
  132. $image = theme('tablesort_indicator', array('style' => $ts['sort']));
  133. }
  134. else {
  135. // If the user clicks a different header, we want to sort ascending initially.
  136. $ts['sort'] = 'asc';
  137. $image = '';
  138. }
  139. $cell['data'] = l($cell['data'] . $image, current_path(), array('attributes' => array('title' => $title), 'query' => array_merge($ts['query'], array('sort' => $ts['sort'], 'order' => $cell['data'])), 'html' => TRUE));
  140. unset($cell['field'], $cell['sort']);
  141. }
  142. return $cell;
  143. }
  144. /**
  145. * Format a table cell.
  146. *
  147. * Adds a class attribute to all cells in the currently active column.
  148. *
  149. * @param $cell
  150. * The cell to format.
  151. * @param $header
  152. * An array of column headers in the format described in theme_table().
  153. * @param $ts
  154. * The current table sort context as returned from tablesort_init().
  155. * @param $i
  156. * The index of the cell's table column.
  157. * @return
  158. * A properly formatted cell, ready for _theme_table_cell().
  159. */
  160. function tablesort_cell($cell, $header, $ts, $i) {
  161. if (isset($header[$i]['data']) && $header[$i]['data'] == $ts['name'] && !empty($header[$i]['field'])) {
  162. if (is_array($cell)) {
  163. $cell['class'][] = 'active';
  164. }
  165. else {
  166. $cell = array('data' => $cell, 'class' => array('active'));
  167. }
  168. }
  169. return $cell;
  170. }
  171. /**
  172. * Compose a URL query parameter array for table sorting links.
  173. *
  174. * @return
  175. * A URL query parameter array that consists of all components of the current
  176. * page request except for those pertaining to table sorting.
  177. */
  178. function tablesort_get_query_parameters() {
  179. return drupal_get_query_parameters($_GET, array('sort', 'order'));
  180. }
  181. /**
  182. * Determine the current sort criterion.
  183. *
  184. * @param $headers
  185. * An array of column headers in the format described in theme_table().
  186. * @return
  187. * An associative array describing the criterion, containing the keys:
  188. * - "name": The localized title of the table column.
  189. * - "sql": The name of the database field to sort on.
  190. */
  191. function tablesort_get_order($headers) {
  192. $order = isset($_GET['order']) ? $_GET['order'] : '';
  193. foreach ($headers as $header) {
  194. if (is_array($header)) {
  195. if (isset($header['data']) && $order == $header['data']) {
  196. $default = $header;
  197. break;
  198. }
  199. if (empty($default) && isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
  200. $default = $header;
  201. }
  202. }
  203. }
  204. if (!isset($default)) {
  205. $default = reset($headers);
  206. if (!is_array($default)) {
  207. $default = array('data' => $default);
  208. }
  209. }
  210. $default += array('data' => NULL, 'field' => NULL);
  211. return array('name' => $default['data'], 'sql' => $default['field']);
  212. }
  213. /**
  214. * Determine the current sort direction.
  215. *
  216. * @param $headers
  217. * An array of column headers in the format described in theme_table().
  218. * @return
  219. * The current sort direction ("asc" or "desc").
  220. */
  221. function tablesort_get_sort($headers) {
  222. if (isset($_GET['sort'])) {
  223. return (strtolower($_GET['sort']) == 'desc') ? 'desc' : 'asc';
  224. }
  225. // The user has not specified a sort. Use the default for the currently sorted
  226. // header if specified; otherwise use "asc".
  227. else {
  228. // Find out which header is currently being sorted.
  229. $ts = tablesort_get_order($headers);
  230. foreach ($headers as $header) {
  231. if (is_array($header) && isset($header['data']) && $header['data'] == $ts['name'] && isset($header['sort'])) {
  232. return $header['sort'];
  233. }
  234. }
  235. }
  236. return 'asc';
  237. }
Login or register to post comments