Issue #1541892 by Rob Loach: Convert TableSort to PSR-0.
parent
ede4aee9c4
commit
c1f05ae409
|
@ -12,101 +12,6 @@ use Drupal\Core\Database\Query\SelectInterface;
|
|||
* column headers that the user can click on to sort the table by that column.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Query extender class for tablesort queries.
|
||||
*/
|
||||
class TableSort extends SelectExtender {
|
||||
|
||||
/**
|
||||
* The array of fields that can be sorted by.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $header = array();
|
||||
|
||||
public function __construct(SelectInterface $query, Connection $connection) {
|
||||
parent::__construct($query, $connection);
|
||||
|
||||
// Add convenience tag to mark that this is an extended query. We have to
|
||||
// do this in the constructor to ensure that it is set before preExecute()
|
||||
// gets called.
|
||||
$this->addTag('tablesort');
|
||||
}
|
||||
|
||||
/**
|
||||
* Order the query based on a header array.
|
||||
*
|
||||
* @see theme_table()
|
||||
* @param $header
|
||||
* Table header array.
|
||||
* @return SelectQueryInterface
|
||||
* The called object.
|
||||
*/
|
||||
public function orderByHeader(Array $header) {
|
||||
$this->header = $header;
|
||||
$ts = $this->init();
|
||||
if (!empty($ts['sql'])) {
|
||||
// Based on code from db_escape_table(), but this can also contain a dot.
|
||||
$field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
|
||||
|
||||
// Sort order can only be ASC or DESC.
|
||||
$sort = drupal_strtoupper($ts['sort']);
|
||||
$sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';
|
||||
$this->orderBy($field, $sort);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the table sort context.
|
||||
*/
|
||||
protected function init() {
|
||||
$ts = $this->order();
|
||||
$ts['sort'] = $this->getSort();
|
||||
$ts['query'] = $this->getQueryParameters();
|
||||
return $ts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the current sort direction.
|
||||
*
|
||||
* @param $headers
|
||||
* An array of column headers in the format described in theme_table().
|
||||
* @return
|
||||
* The current sort direction ("asc" or "desc").
|
||||
*/
|
||||
protected function getSort() {
|
||||
return tablesort_get_sort($this->header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compose a URL query parameter array to append to table sorting requests.
|
||||
*
|
||||
* @return
|
||||
* A URL query parameter array that consists of all components of the current
|
||||
* page request except for those pertaining to table sorting.
|
||||
*
|
||||
* @see tablesort_get_query_parameters()
|
||||
*/
|
||||
protected function getQueryParameters() {
|
||||
return tablesort_get_query_parameters();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the current sort criterion.
|
||||
*
|
||||
* @param $headers
|
||||
* An array of column headers in the format described in theme_table().
|
||||
* @return
|
||||
* An associative array describing the criterion, containing the keys:
|
||||
* - "name": The localized title of the table column.
|
||||
* - "sql": The name of the database field to sort on.
|
||||
*/
|
||||
protected function order() {
|
||||
return tablesort_get_order($this->header);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the table sort context.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Definition of Drupal\Core\Database\Query\TableSortExtender.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\Database\Query;
|
||||
|
||||
use Drupal\Core\Database\Connection;
|
||||
|
||||
/**
|
||||
* Query extender class for tablesort queries.
|
||||
*/
|
||||
class TableSortExtender extends SelectExtender {
|
||||
|
||||
/**
|
||||
* The array of fields that can be sorted by.
|
||||
*/
|
||||
protected $header = array();
|
||||
|
||||
public function __construct(SelectInterface $query, Connection $connection) {
|
||||
parent::__construct($query, $connection);
|
||||
|
||||
// Add convenience tag to mark that this is an extended query. We have to
|
||||
// do this in the constructor to ensure that it is set before preExecute()
|
||||
// gets called.
|
||||
$this->addTag('tablesort');
|
||||
}
|
||||
|
||||
/**
|
||||
* Order the query based on a header array.
|
||||
*
|
||||
* @param array $header
|
||||
* Table header array.
|
||||
*
|
||||
* @return SelectQueryInterface
|
||||
* The called object.
|
||||
*
|
||||
* @see theme_table()
|
||||
*/
|
||||
public function orderByHeader(array $header) {
|
||||
$this->header = $header;
|
||||
$ts = $this->init();
|
||||
if (!empty($ts['sql'])) {
|
||||
// Based on code from db_escape_table(), but this can also contain a dot.
|
||||
$field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
|
||||
|
||||
// Sort order can only be ASC or DESC.
|
||||
$sort = drupal_strtoupper($ts['sort']);
|
||||
$sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';
|
||||
$this->orderBy($field, $sort);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the table sort context.
|
||||
*/
|
||||
protected function init() {
|
||||
$ts = $this->order();
|
||||
$ts['sort'] = $this->getSort();
|
||||
$ts['query'] = $this->getQueryParameters();
|
||||
return $ts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the current sort direction.
|
||||
*
|
||||
* @return
|
||||
* The current sort direction ("asc" or "desc").
|
||||
*
|
||||
* @see tablesort_get_sort()
|
||||
*/
|
||||
protected function getSort() {
|
||||
return tablesort_get_sort($this->header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compose a URL query parameter array to append to table sorting requests.
|
||||
*
|
||||
* @return
|
||||
* A URL query parameter array that consists of all components of the current
|
||||
* page request except for those pertaining to table sorting.
|
||||
*
|
||||
* @see tablesort_get_query_parameters()
|
||||
*/
|
||||
protected function getQueryParameters() {
|
||||
return tablesort_get_query_parameters();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the current sort criterion.
|
||||
*
|
||||
* @return
|
||||
* An associative array describing the criterion, containing the keys:
|
||||
* - "name": The localized title of the table column.
|
||||
* - "sql": The name of the database field to sort on.
|
||||
*
|
||||
* @see tablesort_get_order()
|
||||
*/
|
||||
protected function order() {
|
||||
return tablesort_get_order($this->header);
|
||||
}
|
||||
}
|
|
@ -81,7 +81,7 @@ function comment_admin_overview($form, &$form_state, $arg) {
|
|||
|
||||
$query = db_select('comment', 'c')
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query->join('node', 'n', 'n.nid = c.nid');
|
||||
$query->addField('n', 'title', 'node_title');
|
||||
$query->addTag('node_access');
|
||||
|
|
|
@ -39,7 +39,7 @@ function dblog_overview() {
|
|||
|
||||
$query = db_select('watchdog', 'w')
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query->leftJoin('users', 'u', 'w.uid = u.uid');
|
||||
$query
|
||||
->fields('w', array('wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', 'link'))
|
||||
|
@ -100,7 +100,7 @@ function dblog_top($type) {
|
|||
|
||||
$query = db_select('watchdog', 'w')
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query->addExpression('COUNT(wid)', 'count');
|
||||
$query = $query
|
||||
->fields('w', array('message', 'variables'))
|
||||
|
|
|
@ -895,7 +895,7 @@ function forum_get_topics($tid, $sortby, $forum_per_page) {
|
|||
|
||||
$query = db_select('forum_index', 'f')
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query->fields('f');
|
||||
$query
|
||||
->condition('f.tid', $tid)
|
||||
|
@ -918,7 +918,8 @@ function forum_get_topics($tid, $sortby, $forum_per_page) {
|
|||
if ($nids) {
|
||||
$nodes = node_load_multiple($nids);
|
||||
|
||||
$query = db_select('node', 'n')->extend('TableSort');
|
||||
$query = db_select('node', 'n')
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query->fields('n', array('nid'));
|
||||
|
||||
$query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
|
||||
|
|
|
@ -449,7 +449,7 @@ function node_admin_nodes() {
|
|||
|
||||
$query = db_select('node', 'n')
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
node_build_filter_query($query);
|
||||
|
||||
if (!user_access('bypass node access')) {
|
||||
|
|
|
@ -29,7 +29,7 @@ function path_admin_overview($keys = NULL) {
|
|||
|
||||
$query = db_select('url_alias')
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
if ($keys) {
|
||||
// Replace wildcards with PDO wildcards.
|
||||
$query->condition('alias', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE');
|
||||
|
|
|
@ -58,7 +58,7 @@ function poll_votes($node) {
|
|||
|
||||
$select = db_select('poll_vote', 'pv')
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$select->join('poll_choice', 'pc', 'pv.chid = pc.chid');
|
||||
$select->join('users', 'u', 'pv.uid = u.uid');
|
||||
$queried_votes = $select
|
||||
|
|
|
@ -27,7 +27,7 @@ function statistics_recent_hits() {
|
|||
|
||||
$query = db_select('accesslog', 'a', array('target' => 'slave'))
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query->join('users', 'u', 'a.uid = u.uid');
|
||||
$query
|
||||
->fields('a', array('aid', 'timestamp', 'path', 'title', 'uid'))
|
||||
|
@ -75,7 +75,7 @@ function statistics_top_pages() {
|
|||
|
||||
$query = db_select('accesslog', 'a', array('target' => 'slave'))
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query->addExpression('COUNT(path)', 'hits');
|
||||
// MAX(title) avoids having empty node titles which otherwise causes
|
||||
// duplicates in the top pages list.
|
||||
|
@ -130,7 +130,7 @@ function statistics_top_visitors() {
|
|||
);
|
||||
$query = db_select('accesslog', 'a', array('target' => 'slave'))
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query->leftJoin('blocked_ips', 'bl', 'a.hostname = bl.ip');
|
||||
$query->leftJoin('users', 'u', 'a.uid = u.uid');
|
||||
|
||||
|
@ -192,7 +192,7 @@ function statistics_top_referrers() {
|
|||
);
|
||||
$query = db_select('accesslog', 'a')
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
|
||||
$query->addExpression('COUNT(url)', 'hits');
|
||||
$query->addExpression('MAX(timestamp)', 'last');
|
||||
|
|
|
@ -27,7 +27,7 @@ function statistics_node_tracker() {
|
|||
|
||||
$query = db_select('accesslog', 'a', array('target' => 'slave'))
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query->join('users', 'u', 'a.uid = u.uid');
|
||||
|
||||
$query
|
||||
|
@ -83,7 +83,7 @@ function statistics_user_tracker() {
|
|||
array('data' => t('Operations')));
|
||||
$query = db_select('accesslog', 'a', array('target' => 'slave'))
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query
|
||||
->fields('a', array('aid', 'timestamp', 'path', 'title'))
|
||||
->condition('uid', $account->uid)
|
||||
|
|
|
@ -2,15 +2,17 @@
|
|||
|
||||
/**
|
||||
* @file
|
||||
* Various tablesort tests.
|
||||
* Definition of Drupal\system\Tests\Common\TableSortExtenderTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\system\Tests\Common;
|
||||
|
||||
use Drupal\simpletest\UnitTestBase;
|
||||
|
||||
/**
|
||||
* Test unicode handling features implemented in unicode.inc.
|
||||
*/
|
||||
class TableSortTest extends UnitTestBase {
|
||||
class TableSortExtenderUnitTest extends UnitTestBase {
|
||||
|
||||
/**
|
||||
* Storage for initial value of $_GET.
|
|
@ -2924,7 +2924,7 @@ function system_actions_manage() {
|
|||
);
|
||||
$query = db_select('actions')
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$result = $query
|
||||
->fields('actions')
|
||||
->limit(50)
|
||||
|
|
|
@ -161,7 +161,9 @@ function database_test_tablesort() {
|
|||
$query
|
||||
->fields('t', array('tid', 'pid', 'task', 'priority'));
|
||||
|
||||
$query = $query->extend('TableSort')->orderByHeader($header);
|
||||
$query = $query
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender')
|
||||
->orderByHeader($header);
|
||||
|
||||
// We need all the results at once to check the sort.
|
||||
$tasks = $query->execute()->fetchAll();
|
||||
|
@ -190,7 +192,10 @@ function database_test_tablesort_first() {
|
|||
$query
|
||||
->fields('t', array('tid', 'pid', 'task', 'priority'));
|
||||
|
||||
$query = $query->extend('TableSort')->orderByHeader($header)->orderBy('priority');
|
||||
$query = $query
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender')
|
||||
->orderByHeader($header)
|
||||
->orderBy('priority');
|
||||
|
||||
// We need all the results at once to check the sort.
|
||||
$tasks = $query->execute()->fetchAll();
|
||||
|
|
|
@ -160,7 +160,7 @@ function user_admin_account() {
|
|||
|
||||
$query = $query
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->extend('TableSort');
|
||||
->extend('Drupal\Core\Database\Query\TableSortExtender');
|
||||
$query
|
||||
->fields('u', array('uid', 'name', 'status', 'created', 'access'))
|
||||
->limit(50)
|
||||
|
|
Loading…
Reference in New Issue