[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/plugins/ -> views_plugin_style_table.inc (source)

   1  <?php
   2  /**
   3   * @file
   4   * Contains the table style plugin.
   5   */
   6  
   7  /**
   8   * Style plugin to render each item as a row in a table.
   9   *
  10   * @ingroup views_style_plugins
  11   */
  12  class views_plugin_style_table extends views_plugin_style {
  13    function option_definition() {
  14      $options = parent::option_definition();
  15  
  16      $options['columns'] = array('default' => array());
  17      $options['default'] = array('default' => '');
  18      $options['info'] = array('default' => array());
  19      $options['override'] = array('default' => TRUE);
  20      $options['sticky'] = array('default' => FALSE);
  21      $options['order'] = array('default' => 'asc');
  22      $options['summary'] = array('default' => '');
  23  
  24      return $options;
  25    }
  26  
  27    /**
  28     * Determine if we should provide sorting based upon $_GET inputs.
  29     */
  30    function build_sort() {
  31      if (!isset($_GET['order']) && ($this->options['default'] == -1 || empty($this->view->field[$this->options['default']]))) {
  32        return TRUE;
  33      }
  34  
  35      // If a sort we don't know anything about gets through, exit gracefully.
  36      if (isset($_GET['order']) && empty($this->view->field[$_GET['order']])) {
  37        return TRUE;
  38      }
  39  
  40      // Let the builder know whether or not we're overriding the default sorts.
  41      return empty($this->options['override']);
  42    }
  43  
  44    /**
  45     * Add our actual sort criteria
  46     */
  47    function build_sort_post() {
  48      if (!isset($_GET['order'])) {
  49        // check for a 'default' clicksort. If there isn't one, exit gracefully.
  50        if (empty($this->options['default'])) {
  51          return;
  52        }
  53        $sort = $this->options['default'];
  54        $this->order = !empty($this->options['order']) ? $this->options['order'] : 'asc';
  55      }
  56      else {
  57        $sort = $_GET['order'];
  58        // Store the $order for later use.
  59        $this->order = !empty($_GET['sort']) ? strtolower($_GET['sort']) : 'asc';
  60      }
  61  
  62      // If a sort we don't know anything about gets through, exit gracefully.
  63      if (empty($this->view->field[$sort])) {
  64        return;
  65      }
  66  
  67      // Ensure $this->order is valid.
  68      if ($this->order != 'asc' && $this->order != 'desc') {
  69        $this->order = 'asc';
  70      }
  71  
  72      // Store the $sort for later use.
  73      $this->active = $sort;
  74  
  75      // Tell the field to click sort.
  76      $this->view->field[$sort]->click_sort($this->order);
  77    }
  78  
  79    /**
  80     * Normalize a list of columns based upon the fields that are
  81     * available. This compares the fields stored in the style handler
  82     * to the list of fields actually in the view, removing fields that
  83     * have been removed and adding new fields in their own column.
  84     *
  85     * - Each field must be in a column.
  86     * - Each column must be based upon a field, and that field
  87     *   is somewhere in the column.
  88     * - Any fields not currently represented must be added.
  89     * - Columns must be re-ordered to match the fields.
  90     *
  91     * @param $columns
  92     *   An array of all fields; the key is the id of the field and the
  93     *   value is the id of the column the field should be in.
  94     * @param $fields
  95     *   The fields to use for the columns. If not provided, they will
  96     *   be requested from the current display. The running render should
  97     *   send the fields through, as they may be different than what the
  98     *   display has listed due to access control or other changes.
  99     */
 100    function sanitize_columns($columns, $fields = NULL) {
 101      $sanitized = array();
 102      if ($fields === NULL) {
 103        $fields = $this->display->handler->get_option('fields');
 104      }
 105  
 106      // Preconfigure the sanitized array so that the order is retained.
 107      foreach ($fields as $field => $info) {
 108        // Set to itself so that if it isn't touched, it gets column
 109        // status automatically.
 110        $sanitized[$field] = $field;
 111      }
 112  
 113      foreach ($columns as $field => $column) {
 114        // first, make sure the field still exists.
 115        if (!isset($sanitized[$field])) {
 116          continue;
 117        }
 118  
 119        // If the field is the column, mark it so, or the column
 120        // it's set to is a column, that's ok
 121        if ($field == $column || $columns[$column] == $column && !empty($sanitized[$column])) {
 122          $sanitized[$field] = $column;
 123        }
 124        // Since we set the field to itself initially, ignoring
 125        // the condition is ok; the field will get its column
 126        // status back.
 127      }
 128  
 129      return $sanitized;
 130    }
 131  
 132    /**
 133     * Render the given style.
 134     */
 135    function options_form(&$form, &$form_state) {
 136      parent::options_form($form, $form_state);
 137      $handlers = $this->display->handler->get_handlers('field');
 138      if (empty($handlers)) {
 139        $form['error_markup'] = array(
 140          '#value' => t('You need at least one field before you can configure your table settings'),
 141          '#prefix' => '<div class="error form-item description">',
 142          '#suffix' => '</div>',
 143        );
 144        return;
 145      }
 146  
 147      $form['override'] = array(
 148        '#type' => 'checkbox',
 149        '#title' => t('Override normal sorting if click sorting is used'),
 150        '#default_value' => !empty($this->options['override']),
 151      );
 152  
 153      $form['sticky'] = array(
 154        '#type' => 'checkbox',
 155        '#title' => t('Enable Drupal style "sticky" table headers (Javascript)'),
 156        '#default_value' => !empty($this->options['sticky']),
 157        '#description' => t('(Sticky header effects will not be active for preview below, only on live output.)'),
 158      );
 159  
 160      $form['order'] = array(
 161        '#type' => 'select',
 162        '#title' => t('Default sort order'),
 163        '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
 164        '#default_value' => $this->options['order'],
 165        '#description' => t('If a default sort order is selected, what order should it use by default.'),
 166      );
 167  
 168      $form['summary'] = array(
 169        '#type' => 'textfield',
 170        '#title' => t('Table summary'),
 171        '#description' => t('This value will be displayed as table-summary attribute in the html. Set this for better accessiblity of your site.'),
 172        '#default_value' => $this->options['summary'],
 173      );
 174  
 175      // Note: views UI registers this theme handler on our behalf. Your module
 176      // will have to register your theme handlers if you do stuff like this.
 177      $form['#theme'] = 'views_ui_style_plugin_table';
 178  
 179      $columns = $this->sanitize_columns($this->options['columns']);
 180  
 181      // Create an array of allowed columns from the data we know:
 182      $field_names = $this->display->handler->get_field_labels();
 183  
 184      if (isset($this->options['default'])) {
 185        $default = $this->options['default'];
 186        if (!isset($columns[$default])) {
 187          $default = -1;
 188        }
 189      }
 190      else {
 191        $default = -1;
 192      }
 193  
 194      foreach ($columns as $field => $column) {
 195        $safe = str_replace(array('][', '_', ' '), '-', $field);
 196        // the $id of the column for dependency checking.
 197        $id = 'edit-style-options-columns-' . $safe;
 198  
 199        $form['columns'][$field] = array(
 200          '#type' => 'select',
 201          '#options' => $field_names,
 202          '#default_value' => $column,
 203        );
 204        if ($handlers[$field]->click_sortable()) {
 205          $form['info'][$field]['sortable'] = array(
 206            '#type' => 'checkbox',
 207            '#default_value' => !empty($this->options['info'][$field]['sortable']),
 208            '#process' => array('views_process_dependency'),
 209            '#dependency' => array($id => array($field)),
 210          );
 211          // Provide an ID so we can have such things.
 212          $radio_id = form_clean_id('edit-default-' . $field);
 213          $form['default'][$field] = array(
 214            '#type' => 'radio',
 215            '#return_value' => $field,
 216            '#parents' => array('style_options', 'default'),
 217            '#id' => $radio_id,
 218            // because 'radio' doesn't fully support '#id' =(
 219            '#attributes' => array('id' => $radio_id),
 220            '#default_value' => $default,
 221            '#process' => array('views_process_dependency'),
 222            '#dependency' => array($id => array($field)),
 223          );
 224        }
 225        $form['info'][$field]['separator'] = array(
 226          '#type' => 'textfield',
 227          '#size' => 10,
 228          '#default_value' => isset($this->options['info'][$field]['separator']) ? $this->options['info'][$field]['separator'] : '',
 229          '#process' => array('views_process_dependency'),
 230          '#dependency' => array($id => array($field)),
 231        );
 232  
 233        // markup for the field name
 234        $form['info'][$field]['name'] = array(
 235          '#value' => $field_names[$field],
 236        );
 237      }
 238  
 239      // Provide a radio for no default sort
 240      $form['default'][-1] = array(
 241        '#type' => 'radio',
 242        '#return_value' => -1,
 243        '#parents' => array('style_options', 'default'),
 244        '#id' => 'edit-default-0',
 245        '#default_value' => $default,
 246      );
 247  
 248      $form['description_markup'] = array(
 249        '#prefix' => '<div class="description form-item">',
 250        '#suffix' => '</div>',
 251        '#value' => t('Place fields into columns; you may combine multiple fields into the same column. If you do, the separator in the column specified will be used to separate the fields. Check the sortable box to make that column click sortable, and check the default sort radio to determine which column will be sorted by default, if any. You may control column order and field labels in the fields section.'),
 252      );
 253    }
 254  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7