source: sipes/cord/includes/tablesort.inc @ b9d4e2e

stableversion-3.0
Last change on this file since b9d4e2e was b354002, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio del cord

  • Propiedad mode establecida a 100755
File size: 6.0 KB
Línea 
1<?php
2
3/**
4 * @file
5 * Functions to aid in the creation of sortable tables.
6 *
7 * All tables created with a call to theme('table') have the option of having
8 * column headers that the user can click on to sort the table by that column.
9 */
10
11/**
12 * Initialize the table sort context.
13 */
14function tablesort_init($header) {
15  $ts = tablesort_get_order($header);
16  $ts['sort'] = tablesort_get_sort($header);
17  $ts['query_string'] = tablesort_get_querystring();
18  return $ts;
19}
20
21/**
22 * Create an SQL sort clause.
23 *
24 * This function produces the ORDER BY clause to insert in your SQL queries,
25 * assuring that the returned database table rows match the sort order chosen
26 * by the user.
27 *
28 * @param $header
29 *   An array of column headers in the format described in theme_table().
30 * @param $before
31 *   An SQL string to insert after ORDER BY and before the table sorting code.
32 *   Useful for sorting by important attributes like "sticky" first.
33 * @return
34 *   An SQL string to append to the end of a query.
35 *
36 * @ingroup database
37 */
38function tablesort_sql($header, $before = '') {
39  $ts = tablesort_init($header);
40  if ($ts['sql']) {
41    // Based on code from db_escape_table(), but this can also contain a dot.
42    $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
43
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
48    return " ORDER BY $before $field $sort";
49  }
50}
51
52/**
53 * Format a column header.
54 *
55 * If the cell in question is the column header for the current sort criterion,
56 * it gets special formatting. All possible sort criteria become links.
57 *
58 * @param $cell
59 *   The cell to format.
60 * @param $header
61 *   An array of column headers in the format described in theme_table().
62 * @param $ts
63 *   The current table sort context as returned from tablesort_init().
64 * @return
65 *   A properly formatted cell, ready for _theme_table_cell().
66 */
67function tablesort_header($cell, $header, $ts) {
68  // Special formatting for the currently sorted column header.
69  if (is_array($cell) && isset($cell['field'])) {
70    $title = t('sort by @s', array('@s' => $cell['data']));
71    if ($cell['data'] == $ts['name']) {
72      $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
73      if (isset($cell['class'])) {
74        $cell['class'] .= ' active';
75      }
76      else {
77        $cell['class'] = 'active';
78      }
79      $image = theme('tablesort_indicator', $ts['sort']);
80    }
81    else {
82      // If the user clicks a different header, we want to sort ascending initially.
83      $ts['sort'] = 'asc';
84      $image = '';
85    }
86
87    if (!empty($ts['query_string'])) {
88      $ts['query_string'] = '&'. $ts['query_string'];
89    }
90    $cell['data'] = l($cell['data'] . $image, $_GET['q'], array('attributes' => array('title' => $title), 'query' => 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) . $ts['query_string'], 'html' => TRUE));
91
92    unset($cell['field'], $cell['sort']);
93  }
94  return $cell;
95}
96
97/**
98 * Format a table cell.
99 *
100 * Adds a class attribute to all cells in the currently active column.
101 *
102 * @param $cell
103 *   The cell to format.
104 * @param $header
105 *   An array of column headers in the format described in theme_table().
106 * @param $ts
107 *   The current table sort context as returned from tablesort_init().
108 * @param $i
109 *   The index of the cell's table column.
110 * @return
111 *   A properly formatted cell, ready for _theme_table_cell().
112 */
113function tablesort_cell($cell, $header, $ts, $i) {
114  if (isset($header[$i]['data']) && $header[$i]['data'] == $ts['name'] && !empty($header[$i]['field'])) {
115    if (is_array($cell)) {
116      if (isset($cell['class'])) {
117        $cell['class'] .= ' active';
118      }
119      else {
120        $cell['class'] = 'active';
121      }
122    }
123    else {
124      $cell = array('data' => $cell, 'class' => 'active');
125    }
126  }
127  return $cell;
128}
129
130/**
131 * Compose a query string to append to table sorting requests.
132 *
133 * @return
134 *   A query string that consists of all components of the current page request
135 *   except for those pertaining to table sorting.
136 */
137function tablesort_get_querystring() {
138  return drupal_query_string_encode($_REQUEST, array_merge(array('q', 'sort', 'order', 'pass'), array_keys($_COOKIE)));
139}
140
141/**
142 * Determine the current sort criterion.
143 *
144 * @param $headers
145 *   An array of column headers in the format described in theme_table().
146 * @return
147 *   An associative array describing the criterion, containing the keys:
148 *   - "name": The localized title of the table column.
149 *   - "sql": The name of the database field to sort on.
150 */
151function tablesort_get_order($headers) {
152  $order = isset($_GET['order']) ? $_GET['order'] : '';
153  foreach ($headers as $header) {
154    if (isset($header['data']) && $order == $header['data']) {
155      return array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
156    }
157
158    if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
159      $default = array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
160    }
161  }
162
163  if (isset($default)) {
164    return $default;
165  }
166  else {
167    // The first column specified is initial 'order by' field unless otherwise specified
168    if (is_array($headers[0])) {
169      $headers[0] += array('data' => NULL, 'field' => NULL);
170      return array('name' => $headers[0]['data'], 'sql' => $headers[0]['field']);
171    }
172    else {
173      return array('name' => $headers[0]);
174    }
175  }
176}
177
178/**
179 * Determine the current sort direction.
180 *
181 * @param $headers
182 *   An array of column headers in the format described in theme_table().
183 * @return
184 *   The current sort direction ("asc" or "desc").
185 */
186function tablesort_get_sort($headers) {
187  if (isset($_GET['sort'])) {
188    return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
189  }
190  // User has not specified a sort. Use default if specified; otherwise use "asc".
191  else {
192    foreach ($headers as $header) {
193      if (is_array($header) && array_key_exists('sort', $header)) {
194        return $header['sort'];
195      }
196    }
197  }
198  return 'asc';
199}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.