00001 <?php
00002
00003
00015 function tablesort_init($header) {
00016 $ts = tablesort_get_order($header);
00017 $ts['sort'] = tablesort_get_sort($header);
00018 $ts['query_string'] = tablesort_get_querystring();
00019 return $ts;
00020 }
00021
00039 function tablesort_sql($header, $before = '') {
00040 $ts = tablesort_init($header);
00041 if ($ts['sql']) {
00042
00043 $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
00044
00045
00046 $sort = drupal_strtoupper($ts['sort']);
00047 $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';
00048
00049 return " ORDER BY $before $field $sort";
00050 }
00051 }
00052
00068 function tablesort_header($cell, $header, $ts) {
00069
00070 if (is_array($cell) && isset($cell['field'])) {
00071 $title = t('sort by @s', array('@s' => $cell['data']));
00072 if ($cell['data'] == $ts['name']) {
00073 $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
00074 if (isset($cell['class'])) {
00075 $cell['class'] .= ' active';
00076 }
00077 else {
00078 $cell['class'] = 'active';
00079 }
00080 $image = theme('tablesort_indicator', $ts['sort']);
00081 }
00082 else {
00083
00084 $ts['sort'] = 'asc';
00085 $image = '';
00086 }
00087
00088 if (!empty($ts['query_string'])) {
00089 $ts['query_string'] = '&' . $ts['query_string'];
00090 }
00091 $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));
00092
00093 unset($cell['field'], $cell['sort']);
00094 }
00095 return $cell;
00096 }
00097
00114 function tablesort_cell($cell, $header, $ts, $i) {
00115 if (isset($header[$i]['data']) && $header[$i]['data'] == $ts['name'] && !empty($header[$i]['field'])) {
00116 if (is_array($cell)) {
00117 if (isset($cell['class'])) {
00118 $cell['class'] .= ' active';
00119 }
00120 else {
00121 $cell['class'] = 'active';
00122 }
00123 }
00124 else {
00125 $cell = array('data' => $cell, 'class' => 'active');
00126 }
00127 }
00128 return $cell;
00129 }
00130
00138 function tablesort_get_querystring() {
00139 return drupal_query_string_encode($_REQUEST, array_merge(array('q', 'sort', 'order'), array_keys($_COOKIE)));
00140 }
00141
00152 function tablesort_get_order($headers) {
00153 $order = isset($_GET['order']) ? $_GET['order'] : '';
00154 foreach ($headers as $header) {
00155 if (isset($header['data']) && $order == $header['data']) {
00156 return array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
00157 }
00158
00159 if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
00160 $default = array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
00161 }
00162 }
00163
00164 if (isset($default)) {
00165 return $default;
00166 }
00167 else {
00168
00169 if (is_array($headers[0])) {
00170 $headers[0] += array('data' => NULL, 'field' => NULL);
00171 return array('name' => $headers[0]['data'], 'sql' => $headers[0]['field']);
00172 }
00173 else {
00174 return array('name' => $headers[0]);
00175 }
00176 }
00177 }
00178
00187 function tablesort_get_sort($headers) {
00188 if (isset($_GET['sort'])) {
00189 return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
00190 }
00191
00192 else {
00193 foreach ($headers as $header) {
00194 if (is_array($header) && array_key_exists('sort', $header)) {
00195 return $header['sort'];
00196 }
00197 }
00198 }
00199 return 'asc';
00200 }