<?php
class TableSort extends SelectQueryExtender {
protected $header = array();
public function __construct(SelectQueryInterface $query, DatabaseConnection $connection) {
parent::__construct($query, $connection);
$this
->addTag('tablesort');
}
public function orderByHeader(array $header) {
$this->header = $header;
$ts = $this
->init();
if (!empty($ts['sql'])) {
$field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
$this
->orderBy($field, $ts['sort']);
}
return $this;
}
protected function init() {
$ts = $this
->order();
$ts['sort'] = $this
->getSort();
$ts['query'] = $this
->getQueryParameters();
return $ts;
}
protected function getSort() {
return tablesort_get_sort($this->header);
}
protected function getQueryParameters() {
return tablesort_get_query_parameters();
}
protected function order() {
return tablesort_get_order($this->header);
}
}
function tablesort_init($header) {
$ts = tablesort_get_order($header);
$ts['sort'] = tablesort_get_sort($header);
$ts['query'] = tablesort_get_query_parameters();
return $ts;
}
function tablesort_header($cell, $header, $ts) {
if (is_array($cell) && isset($cell['field'])) {
$title = t('sort by @s', array(
'@s' => $cell['data'],
));
if ($cell['data'] == $ts['name']) {
$ts['sort'] = $ts['sort'] == 'asc' ? 'desc' : 'asc';
$cell['class'][] = 'active';
$image = theme('tablesort_indicator', array(
'style' => $ts['sort'],
));
}
else {
$ts['sort'] = 'asc';
$image = '';
}
$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,
));
unset($cell['field'], $cell['sort']);
}
return $cell;
}
function tablesort_cell($cell, $header, $ts, $i) {
if (isset($header[$i]['data']) && $header[$i]['data'] == $ts['name'] && !empty($header[$i]['field'])) {
if (is_array($cell)) {
$cell['class'][] = 'active';
}
else {
$cell = array(
'data' => $cell,
'class' => array(
'active',
),
);
}
}
return $cell;
}
function tablesort_get_query_parameters() {
return drupal_get_query_parameters($_GET, array(
'q',
'sort',
'order',
));
}
function tablesort_get_order($headers) {
$order = isset($_GET['order']) ? $_GET['order'] : '';
foreach ($headers as $header) {
if (is_array($header)) {
if (isset($header['data']) && $order == $header['data']) {
$default = $header;
break;
}
if (empty($default) && isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
$default = $header;
}
}
}
if (!isset($default)) {
$default = reset($headers);
if (!is_array($default)) {
$default = array(
'data' => $default,
);
}
}
$default += array(
'data' => NULL,
'field' => NULL,
);
return array(
'name' => $default['data'],
'sql' => $default['field'],
);
}
function tablesort_get_sort($headers) {
if (isset($_GET['sort'])) {
return strtolower($_GET['sort']) == 'desc' ? 'desc' : 'asc';
}
else {
$ts = tablesort_get_order($headers);
foreach ($headers as $header) {
if (is_array($header) && isset($header['data']) && $header['data'] == $ts['name'] && isset($header['sort'])) {
return $header['sort'];
}
}
}
return 'asc';
}