[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/handlers/ -> views_handler_argument_string.inc (source)

   1  <?php
   2  
   3  /**
   4   * Basic argument handler to implement string arguments that may have length
   5   * limits.
   6   *
   7   * @ingroup views_argument_handlers
   8   */
   9  class views_handler_argument_string extends views_handler_argument {
  10    function init(&$view, &$options) {
  11      parent::init($view, $options);
  12      if (!empty($this->definition['many to one'])) {
  13        $this->helper = new views_many_to_one_helper($this);
  14  
  15        // Ensure defaults for these, during summaries and stuff:
  16        $this->operator = 'or';
  17        $this->value = array();
  18      }
  19    }
  20  
  21    function option_definition() {
  22      $options = parent::option_definition();
  23  
  24      $options['glossary'] = array('default' => FALSE);
  25      $options['ignorecase'] = array('default' => FALSE);
  26      $options['limit'] = array('default' => 0);
  27      $options['case'] = array('default' => 'none');
  28      $options['path_case'] = array('default' => 'none');
  29      $options['transform_dash'] = array('default' => FALSE);
  30  
  31      if (!empty($this->definition['many to one'])) {
  32        $options['add_table'] = array('default' => FALSE);
  33        $options['require_value'] = array('default' => FALSE);
  34      }
  35  
  36      return $options;
  37    }
  38  
  39    function options_form(&$form, &$form_state) {
  40      parent::options_form($form, $form_state);
  41  
  42      $form['glossary'] = array(
  43        '#type' => 'checkbox',
  44        '#title' => t('Glossary mode'),
  45        '#description' => t('Glossary mode applies a limit to the number of characters used in the argument, which allows the summary view to act as a glossary.'),
  46        '#default_value' => $this->options['glossary'],
  47      );
  48  
  49      $form['ignorecase'] = array(
  50        '#type' => 'checkbox',
  51        '#title' => t('Ignore case'),
  52        '#description' => t('Ignore case allows for doing database searches without case sensitivity. MySQL already works in lower-case mode, so MySQL users should leave this unchecked to improve performance.'),
  53        '#default_value' => $this->options['ignorecase'],
  54      );
  55  
  56      $form['limit'] = array(
  57        '#type' => 'textfield',
  58        '#title' => t('Character limit'),
  59        '#description' => t('How many characters of the argument to filter against. If set to 1, all fields starting with the letter in the argument would be matched.'),
  60        '#default_value' => $this->options['limit'],
  61        '#process' => array('views_process_dependency'),
  62        '#dependency' => array('edit-options-glossary' => array(TRUE)),
  63      );
  64  
  65      $form['case'] = array(
  66        '#type' => 'select',
  67        '#title' => t('Case'),
  68        '#description' => t('When printing the argument result, how to transform the case.'),
  69        '#options' => array(
  70          'none' => t('No transform'),
  71          'upper' => t('Upper case'),
  72          'lower' => t('Lower case'),
  73          'ucfirst' => t('Capitalize first letter'),
  74          'ucwords' => t('Capitalize each word'),
  75        ),
  76        '#default_value' => $this->options['case'],
  77      );
  78  
  79      $form['path_case'] = array(
  80        '#type' => 'select',
  81        '#title' => t('Case in path'),
  82        '#description' => t('When printing url paths, how to transform the case of the argument. Do not use this unless with Postgres as it uses case sensitive comparisons.'),
  83        '#options' => array(
  84          'none' => t('No transform'),
  85          'upper' => t('Upper case'),
  86          'lower' => t('Lower case'),
  87          'ucfirst' => t('Capitalize first letter'),
  88          'ucwords' => t('Capitalize each word'),
  89        ),
  90        '#default_value' => $this->options['path_case'],
  91      );
  92  
  93      $form['transform_dash'] = array(
  94        '#type' => 'checkbox',
  95        '#title' => t('Transform spaces to dashes in URL'),
  96        '#default_value' => $this->options['transform_dash'],
  97      );
  98  
  99      if (!empty($this->definition['many to one'])) {
 100        $form['add_table'] = array(
 101          '#type' => 'checkbox',
 102          '#title' => t('Allow multiple arguments to work together.'),
 103          '#description' => t('If selected, multiple instances of this argument can work together, as though multiple terms were supplied to the same argument. This setting is not compatible with the "Reduce duplicates" setting.'),
 104          '#default_value' => !empty($this->options['add_table']),
 105        );
 106  
 107        $form['require_value'] = array(
 108          '#type' => 'checkbox',
 109          '#title' => t('Do not display items with no value in summary'),
 110          '#default_value' => !empty($this->options['require_value']),
 111        );
 112      }
 113    }
 114  
 115    /**
 116     * Build the summary query based on a string
 117     */
 118    function summary_query() {
 119      if (empty($this->definition['many to one'])) {
 120        $this->ensure_my_table();
 121      }
 122      else {
 123        $this->table_alias = $this->helper->summary_join();
 124      }
 125  
 126      if (empty($this->options['glossary'])) {
 127        // Add the field.
 128        if (empty($this->options['ignorecase'])){
 129          $this->base_alias = $this->name_alias = $this->query->add_field($this->table_alias, $this->real_field);
 130          $this->query->set_count_field($this->table_alias, $this->real_field);
 131        }
 132        else {
 133          $this->base_alias = $this->name_alias = $this->query->add_field($this->table_alias, 'LOWER(' . $this->real_field . ')');
 134          $this->query->set_count_field($this->table_alias, 'LOWER(' . $this->real_field . ')');
 135        }
 136      }
 137      else {
 138        // Add the field.
 139        $formula = $this->get_formula();
 140        if (empty($this->options['ignorecase'])){
 141          $this->base_alias = $this->name_alias = $this->query->add_field(NULL, $formula, $this->field . '_truncated');
 142          $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
 143        }
 144        else {
 145          $this->base_alias = $this->name_alias = $this->query->add_field(NULL, 'LOWER(' . $formula . ')', $this->field . '_truncated');
 146          $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
 147        }
 148      }
 149  
 150      return $this->summary_basics(FALSE);
 151    }
 152  
 153    /**
 154     * Get the formula for this argument.
 155     *
 156     * $this->ensure_my_table() MUST have been called prior to this.
 157     */
 158    function get_formula() {
 159      return "SUBSTR($this->table_alias.$this->real_field, 1, " . intval($this->options['limit']) . ")";
 160    }
 161  
 162    /**
 163     * Build the query based upon the formula
 164     */
 165    function query() {
 166      $argument = $this->argument;
 167      if (!empty($this->options['transform_dash'])) {
 168        $argument = strtr($argument, '-', ' ');
 169      }
 170  
 171      if (!empty($this->definition['many to one'])) {
 172        if (!empty($this->options['glossary'])) {
 173          $this->helper->formula = TRUE;
 174        }
 175        $this->value = array($argument);
 176        $this->helper->ensure_my_table();
 177        $this->helper->add_filter();
 178        return;
 179      }
 180  
 181      $this->ensure_my_table();
 182      if (empty($this->options['glossary'])) {
 183        $field = "$this->table_alias.$this->real_field";
 184      }
 185      else {
 186        $field = $this->get_formula();
 187      }
 188  
 189      if (empty($this->options['ignorecase'])){
 190        $this->query->add_where(0, "$field = '%s'", $argument);
 191      }
 192      else {
 193        $this->query->add_where(0, "LOWER($field) = LOWER('%s')", $argument);
 194      }
 195    }
 196  
 197    function summary_argument($data) {
 198      $value = $this->case_transform($data->{$this->base_alias}, 'path_case');
 199      if (!empty($this->options['transform_dash'])) {
 200        $value = strtr($value, ' ', '-');
 201      }
 202      return $value;
 203    }
 204  
 205    function case_transform($string, $option) {
 206          global $multibyte;
 207          
 208      switch ($this->options[$option]) {
 209        default:
 210          return $string;
 211        case 'upper':
 212          return drupal_strtoupper($string);
 213        case 'lower':
 214          return drupal_strtolower($string);
 215        case 'ucfirst':
 216          return drupal_strtoupper(drupal_substr($string, 0, 1)) . drupal_substr($string, 1);
 217        case 'ucwords':
 218          if ($multibyte == UNICODE_MULTIBYTE) {
 219            return mb_convert_case($string, MB_CASE_TITLE);
 220          } else {
 221            return ucwords($string);
 222          }
 223      }
 224    }
 225  
 226    function title() {
 227      $title = $this->case_transform($this->argument, 'case');
 228      if (!empty($this->options['transform_dash'])) {
 229        $title = strtr($title, '-', ' ');
 230      }
 231  
 232      return check_plain($title);
 233    }
 234  
 235    function summary_name($data) {
 236      return $this->case_transform(parent::summary_name($data), 'case');
 237    }
 238  
 239  }
 240  


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