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