TableSort

  1. drupal
    1. 7 includes/tablesort.inc
    2. 8 core/includes/tablesort.inc

Query extender class for tablesort queries.

Hierarchy

Properties

NameDescription
SelectQueryExtender::$connectionThe connection object on which to run this query.
SelectQueryExtender::$placeholderThe placeholder counter.
SelectQueryExtender::$queryThe SelectQuery object we are extending/decorating.
SelectQueryExtender::$uniqueIdentifierA unique identifier for this query object.
TableSort::$headerThe array of fields that can be sorted by.

Functions & methods

NameDescription
SelectQueryExtender::addExpressionAdds an expression to the list of "fields" to be SELECTed. Overrides SelectQueryInterface::addExpression
SelectQueryExtender::addFieldAdds a field to the list to be SELECTed. Overrides SelectQueryInterface::addField
SelectQueryExtender::addJoinJoin against another table in the database. Overrides SelectQueryInterface::addJoin
SelectQueryExtender::addMetaData
SelectQueryExtender::addTag
SelectQueryExtender::arguments
SelectQueryExtender::compile
SelectQueryExtender::compiled
SelectQueryExtender::condition
SelectQueryExtender::conditions
SelectQueryExtender::countQueryGet the equivalent COUNT query of this query as a new query object. Overrides SelectQueryInterface::countQuery
SelectQueryExtender::distinctSets this query to be DISTINCT. Overrides SelectQueryInterface::distinct
SelectQueryExtender::execute
SelectQueryExtender::exists
SelectQueryExtender::extend
SelectQueryExtender::fieldsAdd multiple fields from the same table to be SELECTed. Overrides SelectQueryInterface::fields
SelectQueryExtender::forUpdateAdd FOR UPDATE to the query. Overrides SelectQueryInterface::forUpdate
SelectQueryExtender::getArgumentsCompiles and returns an associative array of the arguments for this prepared statement. Overrides SelectQueryInterface::getArguments
SelectQueryExtender::getExpressionsReturns a reference to the expressions array for this query. Overrides SelectQueryInterface::getExpressions
SelectQueryExtender::getFieldsReturns a reference to the fields array for this query. Overrides SelectQueryInterface::getFields
SelectQueryExtender::getGroupByReturns a reference to the group-by array for this query. Overrides SelectQueryInterface::getGroupBy
SelectQueryExtender::getMetaData
SelectQueryExtender::getOrderByReturns a reference to the order by array for this query. Overrides SelectQueryInterface::getOrderBy
SelectQueryExtender::getTablesReturns a reference to the tables array for this query. Overrides SelectQueryInterface::getTables
SelectQueryExtender::getUnionReturns a reference to the union queries for this query. This include queries for UNION, UNION ALL, and UNION DISTINCT. Overrides SelectQueryInterface::getUnion
SelectQueryExtender::groupByGroups the result set by the specified field. Overrides SelectQueryInterface::groupBy
SelectQueryExtender::hasAllTags
SelectQueryExtender::hasAnyTag
SelectQueryExtender::hasTag
SelectQueryExtender::having
SelectQueryExtender::havingArguments
SelectQueryExtender::havingCompile
SelectQueryExtender::havingConditionHelper function to build most common HAVING conditional clauses. Overrides SelectQueryInterface::havingCondition
SelectQueryExtender::havingConditions
SelectQueryExtender::innerJoinInner Join against another table in the database. Overrides SelectQueryInterface::innerJoin
SelectQueryExtender::isNotNull
SelectQueryExtender::isNull
SelectQueryExtender::isPreparedIndicates if preExecute() has already been called on that object. Overrides SelectQueryInterface::isPrepared
SelectQueryExtender::joinDefault Join against another table in the database. Overrides SelectQueryInterface::join
SelectQueryExtender::leftJoinLeft Outer Join against another table in the database. Overrides SelectQueryInterface::leftJoin
SelectQueryExtender::nextPlaceholderImplements QueryPlaceholderInterface::nextPlaceholder().
SelectQueryExtender::notExists
SelectQueryExtender::orderByOrders the result set by a given field. Overrides SelectQueryInterface::orderBy
SelectQueryExtender::orderRandomOrders the result set by a random value. Overrides SelectQueryInterface::orderRandom
SelectQueryExtender::preExecuteGeneric preparation and validation for a SELECT query. Overrides SelectQueryInterface::preExecute
SelectQueryExtender::rangeRestricts a query to a given range in the result set. Overrides SelectQueryInterface::range
SelectQueryExtender::rightJoinRight Outer Join against another table in the database. Overrides SelectQueryInterface::rightJoin
SelectQueryExtender::unionAdd another Select query to UNION to this one. Overrides SelectQueryInterface::union
SelectQueryExtender::uniqueIdentifierImplements QueryPlaceholderInterface::uniqueIdentifier().
SelectQueryExtender::where
SelectQueryExtender::__callMagic override for undefined methods.
SelectQueryExtender::__cloneClone magic method. Overrides SelectQueryInterface::__clone
SelectQueryExtender::__toString
TableSort::getQueryParametersCompose a URL query parameter array to append to table sorting requests.
TableSort::getSortDetermine the current sort direction.
TableSort::initInitialize the table sort context.
TableSort::orderDetermine the current sort criterion.
TableSort::orderByHeaderOrder the query based on a header array.
TableSort::__construct Overrides SelectQueryExtender::__construct

File

includes/tablesort.inc, line 15
Functions to aid in the creation of sortable tables.

View source
class TableSort extends SelectQueryExtender {

  /**
   * The array of fields that can be sorted by.
   *
   * @var array
   */
  protected $header = array();

  public function __construct(SelectQueryInterface $query, DatabaseConnection $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);
  }
}
Login or register to post comments