123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?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';
- }
|