tablesort.inc 5.15 KB
Newer Older
1 2
<?php

3 4 5
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\SelectExtender;
use Drupal\Core\Database\Query\SelectInterface;
6

7 8 9 10 11 12 13 14 15
/**
 * @file
 * 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.
 */

/**
16
 * Initializes the table sort context.
17
 */
Dries's avatar
Dries committed
18 19 20
function tablesort_init($header) {
  $ts = tablesort_get_order($header);
  $ts['sort'] = tablesort_get_sort($header);
21
  $ts['query'] = tablesort_get_query_parameters();
22 23 24
  return $ts;
}

25
/**
26
 * Formats a column header.
27 28 29 30 31 32 33 34 35 36
 *
 * If the cell in question is the column header for the current sort criterion,
 * it gets special formatting. All possible sort criteria become links.
 *
 * @param $cell
 *   The cell to format.
 * @param $header
 *   An array of column headers in the format described in theme_table().
 * @param $ts
 *   The current table sort context as returned from tablesort_init().
37
 *
38 39 40
 * @return
 *   A properly formatted cell, ready for _theme_table_cell().
 */
41
function tablesort_header($cell, $header, $ts) {
42
  // Special formatting for the currently sorted column header.
43
  if (is_array($cell) && isset($cell['field'])) {
44
    $title = t('sort by @s', array('@s' => $cell['data']));
45
    if ($cell['data'] == $ts['name']) {
46 47 48 49
      // aria-sort is a WAI-ARIA property that indicates if items in a table
      // or grid are sorted in ascending or descending order. See
      // http://www.w3.org/TR/wai-aria/states_and_properties#aria-sort
      $cell['aria-sort'] = ($ts['sort'] == 'asc') ? 'ascending' : 'descending';
50
      $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
51
      $cell['class'][] = 'active';
52
      $image = theme('tablesort_indicator', array('style' => $ts['sort']));
53 54 55 56
    }
    else {
      // If the user clicks a different header, we want to sort ascending initially.
      $ts['sort'] = 'asc';
57
      $image = '';
58
    }
59
    $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));
60

61 62 63 64
    unset($cell['field'], $cell['sort']);
  }
  return $cell;
}
65

66
/**
67
 * Formats a table cell.
68 69 70 71 72 73 74 75 76 77 78
 *
 * Adds a class attribute to all cells in the currently active column.
 *
 * @param $cell
 *   The cell to format.
 * @param $header
 *   An array of column headers in the format described in theme_table().
 * @param $ts
 *   The current table sort context as returned from tablesort_init().
 * @param $i
 *   The index of the cell's table column.
79
 *
80 81 82
 * @return
 *   A properly formatted cell, ready for _theme_table_cell().
 */
83
function tablesort_cell($cell, $header, $ts, $i) {
84
  if (isset($header[$i]['data']) && $header[$i]['data'] == $ts['name'] && !empty($header[$i]['field'])) {
85
    if (is_array($cell)) {
86
      $cell['class'][] = 'active';
87 88
    }
    else {
89
      $cell = array('data' => $cell, 'class' => array('active'));
90 91
    }
  }
92 93 94
  return $cell;
}

95
/**
96
 * Composes a URL query parameter array for table sorting links.
97 98
 *
 * @return
99 100
 *   A URL query parameter array that consists of all components of the current
 *   page request except for those pertaining to table sorting.
101
 */
102
function tablesort_get_query_parameters() {
103
  return drupal_get_query_parameters($_GET, array('sort', 'order'));
104 105
}

106
/**
107
 * Determines the current sort criterion.
108 109 110
 *
 * @param $headers
 *   An array of column headers in the format described in theme_table().
111
 *
112 113 114 115 116
 * @return
 *   An associative array describing the criterion, containing the keys:
 *   - "name": The localized title of the table column.
 *   - "sql": The name of the database field to sort on.
 */
Dries's avatar
Dries committed
117
function tablesort_get_order($headers) {
118
  $order = isset($_GET['order']) ? $_GET['order'] : '';
119
  foreach ($headers as $header) {
120 121 122 123 124
    if (is_array($header)) {
      if (isset($header['data']) && $order == $header['data']) {
        $default = $header;
        break;
      }
125

126 127 128
      if (empty($default) && isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
        $default = $header;
      }
129 130 131
    }
  }

132 133 134 135
  if (!isset($default)) {
    $default = reset($headers);
    if (!is_array($default)) {
      $default = array('data' => $default);
136 137
    }
  }
138 139 140

  $default += array('data' => NULL, 'field' => NULL);
  return array('name' => $default['data'], 'sql' => $default['field']);
141 142
}

143
/**
144
 * Determines the current sort direction.
145 146 147
 *
 * @param $headers
 *   An array of column headers in the format described in theme_table().
148
 *
149 150 151
 * @return
 *   The current sort direction ("asc" or "desc").
 */
Dries's avatar
Dries committed
152
function tablesort_get_sort($headers) {
153 154
  if (isset($_GET['sort'])) {
    return (strtolower($_GET['sort']) == 'desc') ? 'desc' : 'asc';
155
  }
156 157
  // The user has not specified a sort. Use the default for the currently sorted
  // header if specified; otherwise use "asc".
158
  else {
159 160
    // Find out which header is currently being sorted.
    $ts = tablesort_get_order($headers);
161
    foreach ($headers as $header) {
162
      if (is_array($header) && isset($header['data']) && $header['data'] == $ts['name'] && isset($header['sort'])) {
163
        return $header['sort'];
164 165 166 167 168
      }
    }
  }
  return 'asc';
}