tablesort.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. // orderBy() will ensure that only ASC/DESC values are accepted, so we
  42. // don't need to sanitize that here.
  43. $this->orderBy($field, $ts['sort']);
  44. }
  45. return $this;
  46. }
  47. /**
  48. * Initializes the table sort context.
  49. */
  50. protected function init() {
  51. $ts = $this->order();
  52. $ts['sort'] = $this->getSort();
  53. $ts['query'] = $this->getQueryParameters();
  54. return $ts;
  55. }
  56. /**
  57. * Determine the current sort direction.
  58. *
  59. * @param $headers
  60. * An array of column headers in the format described in theme_table().
  61. * @return
  62. * The current sort direction ("asc" or "desc").
  63. */
  64. protected function getSort() {
  65. return tablesort_get_sort($this->header);
  66. }
  67. /**
  68. * Compose a URL query parameter array to append to table sorting requests.
  69. *
  70. * @return
  71. * A URL query parameter array that consists of all components of the current
  72. * page request except for those pertaining to table sorting.
  73. *
  74. * @see tablesort_get_query_parameters()
  75. */
  76. protected function getQueryParameters() {
  77. return tablesort_get_query_parameters();
  78. }
  79. /**
  80. * Determine the current sort criterion.
  81. *
  82. * @param $headers
  83. * An array of column headers in the format described in theme_table().
  84. * @return
  85. * An associative array describing the criterion, containing the keys:
  86. * - "name": The localized title of the table column.
  87. * - "sql": The name of the database field to sort on.
  88. */
  89. protected function order() {
  90. return tablesort_get_order($this->header);
  91. }
  92. }
  93. /**
  94. * Initialize the table sort context.
  95. */
  96. function tablesort_init($header) {
  97. $ts = tablesort_get_order($header);
  98. $ts['sort'] = tablesort_get_sort($header);
  99. $ts['query'] = tablesort_get_query_parameters();
  100. return $ts;
  101. }
  102. /**
  103. * Formats a column header.
  104. *
  105. * If the cell in question is the column header for the current sort criterion,
  106. * it gets special formatting. All possible sort criteria become links.
  107. *
  108. * @param $cell
  109. * The cell to format.
  110. * @param $header
  111. * An array of column headers in the format described in theme_table().
  112. * @param $ts
  113. * The current table sort context as returned from tablesort_init().
  114. *
  115. * @return
  116. * A properly formatted cell, ready for _theme_table_cell().
  117. */
  118. function tablesort_header($cell, $header, $ts) {
  119. // Special formatting for the currently sorted column header.
  120. if (is_array($cell) && isset($cell['field'])) {
  121. $title = t('sort by @s', array('@s' => $cell['data']));
  122. if ($cell['data'] == $ts['name']) {
  123. $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
  124. $cell['class'][] = 'active';
  125. $image = theme('tablesort_indicator', array('style' => $ts['sort']));
  126. }
  127. else {
  128. // If the user clicks a different header, we want to sort ascending initially.
  129. $ts['sort'] = 'asc';
  130. $image = '';
  131. }
  132. $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));
  133. unset($cell['field'], $cell['sort']);
  134. }
  135. return $cell;
  136. }
  137. /**
  138. * Formats a table cell.
  139. *
  140. * Adds a class attribute to all cells in the currently active column.
  141. *
  142. * @param $cell
  143. * The cell to format.
  144. * @param $header
  145. * An array of column headers in the format described in theme_table().
  146. * @param $ts
  147. * The current table sort context as returned from tablesort_init().
  148. * @param $i
  149. * The index of the cell's table column.
  150. *
  151. * @return
  152. * A properly formatted cell, ready for _theme_table_cell().
  153. */
  154. function tablesort_cell($cell, $header, $ts, $i) {
  155. if (isset($header[$i]['data']) && $header[$i]['data'] == $ts['name'] && !empty($header[$i]['field'])) {
  156. if (is_array($cell)) {
  157. $cell['class'][] = 'active';
  158. }
  159. else {
  160. $cell = array('data' => $cell, 'class' => array('active'));
  161. }
  162. }
  163. return $cell;
  164. }
  165. /**
  166. * Composes a URL query parameter array for table sorting links.
  167. *
  168. * @return
  169. * A URL query parameter array that consists of all components of the current
  170. * page request except for those pertaining to table sorting.
  171. */
  172. function tablesort_get_query_parameters() {
  173. return drupal_get_query_parameters($_GET, array('q', 'sort', 'order'));
  174. }
  175. /**
  176. * Determines the current sort criterion.
  177. *
  178. * @param $headers
  179. * An array of column headers in the format described in theme_table().
  180. *
  181. * @return
  182. * An associative array describing the criterion, containing the keys:
  183. * - "name": The localized title of the table column.
  184. * - "sql": The name of the database field to sort on.
  185. */
  186. function tablesort_get_order($headers) {
  187. $order = isset($_GET['order']) ? $_GET['order'] : '';
  188. foreach ($headers as $header) {
  189. if (is_array($header)) {
  190. if (isset($header['data']) && $order == $header['data']) {
  191. $default = $header;
  192. break;
  193. }
  194. if (empty($default) && isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
  195. $default = $header;
  196. }
  197. }
  198. }
  199. if (!isset($default)) {
  200. $default = reset($headers);
  201. if (!is_array($default)) {
  202. $default = array('data' => $default);
  203. }
  204. }
  205. $default += array('data' => NULL, 'field' => NULL);
  206. return array('name' => $default['data'], 'sql' => $default['field']);
  207. }
  208. /**
  209. * Determines the current sort direction.
  210. *
  211. * @param $headers
  212. * An array of column headers in the format described in theme_table().
  213. *
  214. * @return
  215. * The current sort direction ("asc" or "desc").
  216. */
  217. function tablesort_get_sort($headers) {
  218. if (isset($_GET['sort'])) {
  219. return (strtolower($_GET['sort']) == 'desc') ? 'desc' : 'asc';
  220. }
  221. // The user has not specified a sort. Use the default for the currently sorted
  222. // header if specified; otherwise use "asc".
  223. else {
  224. // Find out which header is currently being sorted.
  225. $ts = tablesort_get_order($headers);
  226. foreach ($headers as $header) {
  227. if (is_array($header) && isset($header['data']) && $header['data'] == $ts['name'] && isset($header['sort'])) {
  228. return $header['sort'];
  229. }
  230. }
  231. }
  232. return 'asc';
  233. }