tablesort.inc 6.04 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3

4 5 6 7 8 9 10 11 12 13 14
/**
 * @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.
 */

/**
 * Initialize the table sort context.
 */
Dries's avatar
 
Dries committed
15
function tablesort_init($header) {
16 17 18
  $ts = tablesort_get_order($header);
  $ts['sort'] = tablesort_get_sort($header);
  $ts['query_string'] = tablesort_get_querystring();
Dries's avatar
 
Dries committed
19 20 21
  return $ts;
}

22 23 24 25 26 27
/**
 * Fetch pager link arguments.
 *
 * When producing a sortable table that presents paged data, pass the output
 * of this function into theme('pager') to preserve the current sort order.
 */
Dries's avatar
 
Dries committed
28
function tablesort_pager() {
Dries's avatar
 
Dries committed
29
  $cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET : $_POST;
30
  unset($cgi['q'], $cgi['page']);
Dries's avatar
 
Dries committed
31
  return $cgi;
Dries's avatar
 
Dries committed
32 33
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 * Create an SQL sort clause.
 *
 * This function produces the ORDER BY clause to insert in your SQL queries,
 * assuring that the returned database table rows match the sort order chosen
 * by the user.
 *
 * @param $header
 *   An array of column headers in the format described in theme_table().
 * @param $before
 *   An SQL string to insert after ORDER BY and before the table sorting code.
 *   Useful for sorting by important attributes like "sticky" first.
 * @return
 *   An SQL string to append to the end of a query.
Dries's avatar
 
Dries committed
48 49
 *
 * @ingroup database
50 51
 */
function tablesort_sql($header, $before = '') {
Dries's avatar
 
Dries committed
52
  $ts = tablesort_init($header);
53
  if ($ts['sql']) {
Dries's avatar
 
Dries committed
54 55
    $sql = db_escape_string($ts['sql']);
    $sort = strtoupper(db_escape_string($ts['sort']));
Dries's avatar
 
Dries committed
56
    return " ORDER BY $before $sql $sort";
57
  }
Dries's avatar
 
Dries committed
58 59
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/**
 * Format a column header.
 *
 * 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().
 * @return
 *   A properly formatted cell, ready for _theme_table_cell().
 */
75
function tablesort_header($cell, $header, $ts) {
76
  // Special formatting for the currently sorted column header.
77 78
  if (is_array($cell) && isset($cell['field'])) {
    $title = t('sort by %s', array('%s' => $cell['data']));
79 80 81 82
    if ($cell['data'] == $ts['name']) {
      $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
      $cell['class'] = 'active';
      $title = ($ts['sort'] == 'asc' ? t('sort ascending') : t('sort descending'));
Dries's avatar
 
Dries committed
83
      $image = '&nbsp;'. theme('image', 'misc/arrow-'. $ts['sort'] .'.png', t('sort icon'), $title);
84 85 86 87
    }
    else {
      // If the user clicks a different header, we want to sort ascending initially.
      $ts['sort'] = 'asc';
88
      $image = '';
89
    }
90
    $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('title' => $title), 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']). $ts['query_string'], NULL, FALSE, TRUE);
Dries's avatar
 
Dries committed
91

92 93 94 95
    unset($cell['field'], $cell['sort']);
  }
  return $cell;
}
Dries's avatar
 
Dries committed
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/**
 * Format a table cell.
 *
 * 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.
 * @return
 *   A properly formatted cell, ready for _theme_table_cell().
 */
113
function tablesort_cell($cell, $header, $ts, $i) {
114
  if (isset($header[$i]) && $header[$i]['data'] == $ts['name'] && $header[$i]['field']) {
115
    if (is_array($cell)) {
116 117 118 119 120 121
      if (isset($cell['class'])) {
        $cell['class'] .= ' active';
      }
      else {
        $cell['class'] = 'active';
      }
122 123 124 125 126
    }
    else {
      $cell = array('data' => $cell, 'class' => 'active');
    }
  }
Dries's avatar
 
Dries committed
127 128 129
  return $cell;
}

130 131 132 133 134 135 136
/**
 * Compose a query string to append to table sorting requests.
 *
 * @return
 *   A query string that consists of all components of the current page request
 *   except for those pertaining to table sorting.
 */
Dries's avatar
 
Dries committed
137 138
function tablesort_get_querystring() {
  $cgi = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET : $_POST;
139
  $query_string = '';
Dries's avatar
 
Dries committed
140
  foreach ($cgi as $key => $val) {
Dries's avatar
 
Dries committed
141
    if ($key != 'order' && $key != 'sort' && $key != 'q') {
142
      $query_string .= '&'. $key .'='. $val;
Dries's avatar
 
Dries committed
143 144 145 146 147
    }
  }
  return $query_string;
}

148 149 150 151 152 153 154 155 156 157
/**
 * Determine the current sort criterion.
 *
 * @param $headers
 *   An array of column headers in the format described in theme_table().
 * @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
158
function tablesort_get_order($headers) {
159
  $order = isset($_GET['order']) ? $_GET['order'] : '';
Dries's avatar
 
Dries committed
160
  foreach ($headers as $header) {
161
    if (isset($header['data']) && $order == $header['data']) {
Dries's avatar
 
Dries committed
162
      return array('name' => $header['data'], 'sql' => $header['field']);
Dries's avatar
 
Dries committed
163 164
    }

165
    if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
Dries's avatar
 
Dries committed
166
      $default = array('name' => $header['data'], 'sql' => $header['field']);
Dries's avatar
 
Dries committed
167 168 169
    }
  }

170
  if (isset($default)) {
Dries's avatar
 
Dries committed
171
    return $default;
Dries's avatar
 
Dries committed
172 173
  }
  else {
Dries's avatar
 
Dries committed
174
    // The first column specified is initial 'order by' field unless otherwise specified
Dries's avatar
 
Dries committed
175
    if (is_array($headers[0])) {
176
      return array('name' => $headers[0]['data'], 'sql' => $headers[0]['field']);
Dries's avatar
 
Dries committed
177 178 179
    }
    else {
      return array('name' => $headers[0]);
Dries's avatar
 
Dries committed
180 181 182 183
    }
  }
}

184 185 186 187 188 189 190 191
/**
 * Determine the current sort direction.
 *
 * @param $headers
 *   An array of column headers in the format described in theme_table().
 * @return
 *   The current sort direction ("asc" or "desc").
 */
Dries's avatar
 
Dries committed
192
function tablesort_get_sort($headers) {
193
  if (isset($_GET['sort'])) {
194
    return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
Dries's avatar
 
Dries committed
195
  }
196
  // User has not specified a sort. Use default if specified; otherwise use "asc".
Dries's avatar
 
Dries committed
197 198
  else {
    foreach ($headers as $header) {
199
      if (is_array($header) && array_key_exists('sort', $header)) {
Dries's avatar
 
Dries committed
200
        return $header['sort'];
Dries's avatar
 
Dries committed
201 202 203 204 205 206
      }
    }
  }
  return 'asc';
}

Dries's avatar
 
Dries committed
207
?>