Drupal PHP Cross Reference Content Management Systems

Source: /includes/tablesort.inc - 252 lines - 7460 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Functions to aid in the creation of sortable tables.
   6   *
   7   * All tables created with a call to theme('table') have the option of having
   8   * column headers that the user can click on to sort the table by that column.
   9   */
  10  
  11  
  12  /**
  13   * Query extender class for tablesort queries.
  14   */
  15  class TableSort extends SelectQueryExtender {
  16  
  17    /**
  18     * The array of fields that can be sorted by.
  19     *
  20     * @var array
  21     */
  22    protected $header = array();
  23  
  24    public function __construct(SelectQueryInterface $query, DatabaseConnection $connection) {
  25      parent::__construct($query, $connection);
  26  
  27      // Add convenience tag to mark that this is an extended query. We have to
  28      // do this in the constructor to ensure that it is set before preExecute()
  29      // gets called.
  30      $this->addTag('tablesort');
  31    }
  32  
  33    /**
  34     * Order the query based on a header array.
  35     *
  36     * @see theme_table()
  37     * @param $header
  38     *   Table header array.
  39     * @return SelectQueryInterface
  40     *   The called object.
  41     */
  42    public function orderByHeader(Array $header) {
  43      $this->header = $header;
  44      $ts = $this->init();
  45      if (!empty($ts['sql'])) {
  46        // Based on code from db_escape_table(), but this can also contain a dot.
  47        $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
  48  
  49        // Sort order can only be ASC or DESC.
  50        $sort = drupal_strtoupper($ts['sort']);
  51        $sort = in_array($sort, array('ASC', 'DESC')) ? $sort : '';
  52        $this->orderBy($field, $sort);
  53      }
  54      return $this;
  55    }
  56  
  57    /**
  58     * Initialize the table sort context.
  59     */
  60    protected function init() {
  61      $ts = $this->order();
  62      $ts['sort'] = $this->getSort();
  63      $ts['query'] = $this->getQueryParameters();
  64      return $ts;
  65    }
  66  
  67    /**
  68     * Determine the current sort direction.
  69     *
  70     * @param $headers
  71     *   An array of column headers in the format described in theme_table().
  72     * @return
  73     *   The current sort direction ("asc" or "desc").
  74     */
  75    protected function getSort() {
  76      return tablesort_get_sort($this->header);
  77    }
  78  
  79    /**
  80     * Compose a URL query parameter array to append to table sorting requests.
  81     *
  82     * @return
  83     *   A URL query parameter array that consists of all components of the current
  84     *   page request except for those pertaining to table sorting.
  85     *
  86     * @see tablesort_get_query_parameters()
  87     */
  88    protected function getQueryParameters() {
  89      return tablesort_get_query_parameters();
  90    }
  91  
  92    /**
  93     * Determine the current sort criterion.
  94     *
  95     * @param $headers
  96     *   An array of column headers in the format described in theme_table().
  97     * @return
  98     *   An associative array describing the criterion, containing the keys:
  99     *   - "name": The localized title of the table column.
 100     *   - "sql": The name of the database field to sort on.
 101     */
 102    protected function order() {
 103      return tablesort_get_order($this->header);
 104    }
 105  }
 106  
 107  /**
 108   * Initialize the table sort context.
 109   */
 110  function tablesort_init($header) {
 111    $ts = tablesort_get_order($header);
 112    $ts['sort'] = tablesort_get_sort($header);
 113    $ts['query'] = tablesort_get_query_parameters();
 114    return $ts;
 115  }
 116  
 117  /**
 118   * Format a column header.
 119   *
 120   * If the cell in question is the column header for the current sort criterion,
 121   * it gets special formatting. All possible sort criteria become links.
 122   *
 123   * @param $cell
 124   *   The cell to format.
 125   * @param $header
 126   *   An array of column headers in the format described in theme_table().
 127   * @param $ts
 128   *   The current table sort context as returned from tablesort_init().
 129   * @return
 130   *   A properly formatted cell, ready for _theme_table_cell().
 131   */
 132  function tablesort_header($cell, $header, $ts) {
 133    // Special formatting for the currently sorted column header.
 134    if (is_array($cell) && isset($cell['field'])) {
 135      $title = t('sort by @s', array('@s' => $cell['data']));
 136      if ($cell['data'] == $ts['name']) {
 137        $ts['sort'] = (($ts['sort'] == 'asc') ? 'desc' : 'asc');
 138        $cell['class'][] = 'active';
 139        $image = theme('tablesort_indicator', array('style' => $ts['sort']));
 140      }
 141      else {
 142        // If the user clicks a different header, we want to sort ascending initially.
 143        $ts['sort'] = 'asc';
 144        $image = '';
 145      }
 146      $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));
 147  
 148      unset($cell['field'], $cell['sort']);
 149    }
 150    return $cell;
 151  }
 152  
 153  /**
 154   * Format a table cell.
 155   *
 156   * Adds a class attribute to all cells in the currently active column.
 157   *
 158   * @param $cell
 159   *   The cell to format.
 160   * @param $header
 161   *   An array of column headers in the format described in theme_table().
 162   * @param $ts
 163   *   The current table sort context as returned from tablesort_init().
 164   * @param $i
 165   *   The index of the cell's table column.
 166   * @return
 167   *   A properly formatted cell, ready for _theme_table_cell().
 168   */
 169  function tablesort_cell($cell, $header, $ts, $i) {
 170    if (isset($header[$i]['data']) && $header[$i]['data'] == $ts['name'] && !empty($header[$i]['field'])) {
 171      if (is_array($cell)) {
 172        $cell['class'][] = 'active';
 173      }
 174      else {
 175        $cell = array('data' => $cell, 'class' => array('active'));
 176      }
 177    }
 178    return $cell;
 179  }
 180  
 181  /**
 182   * Compose a URL query parameter array for table sorting links.
 183   *
 184   * @return
 185   *   A URL query parameter array that consists of all components of the current
 186   *   page request except for those pertaining to table sorting.
 187   */
 188  function tablesort_get_query_parameters() {
 189    return drupal_get_query_parameters($_GET, array('q', 'sort', 'order'));
 190  }
 191  
 192  /**
 193   * Determine the current sort criterion.
 194   *
 195   * @param $headers
 196   *   An array of column headers in the format described in theme_table().
 197   * @return
 198   *   An associative array describing the criterion, containing the keys:
 199   *   - "name": The localized title of the table column.
 200   *   - "sql": The name of the database field to sort on.
 201   */
 202  function tablesort_get_order($headers) {
 203    $order = isset($_GET['order']) ? $_GET['order'] : '';
 204    foreach ($headers as $header) {
 205      if (is_array($header)) {
 206        if (isset($header['data']) && $order == $header['data']) {
 207          $default = $header;
 208          break;
 209        }
 210  
 211        if (empty($default) && isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
 212          $default = $header;
 213        }
 214      }
 215    }
 216  
 217    if (!isset($default)) {
 218      $default = reset($headers);
 219      if (!is_array($default)) {
 220        $default = array('data' => $default);
 221      }
 222    }
 223  
 224    $default += array('data' => NULL, 'field' => NULL);
 225    return array('name' => $default['data'], 'sql' => $default['field']);
 226  }
 227  
 228  /**
 229   * Determine the current sort direction.
 230   *
 231   * @param $headers
 232   *   An array of column headers in the format described in theme_table().
 233   * @return
 234   *   The current sort direction ("asc" or "desc").
 235   */
 236  function tablesort_get_sort($headers) {
 237    if (isset($_GET['sort'])) {
 238      return (strtolower($_GET['sort']) == 'desc') ? 'desc' : 'asc';
 239    }
 240    // The user has not specified a sort. Use the default for the currently sorted
 241    // header if specified; otherwise use "asc".
 242    else {
 243      // Find out which header is currently being sorted.
 244      $ts = tablesort_get_order($headers);
 245      foreach ($headers as $header) {
 246        if (is_array($header) && isset($header['data']) && $header['data'] == $ts['name'] && isset($header['sort'])) {
 247          return $header['sort'];
 248        }
 249      }
 250    }
 251    return 'asc';
 252  }

title

Description

title

Description

title

Description

title

title

Body