tablesort.inc 7.3 KB

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