You are here

class views_plugin_argument_validate_taxonomy_term in Views (for Drupal 7) 6.2

Same name and namespace in other branches
  1. 6.3 modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc \views_plugin_argument_validate_taxonomy_term
  2. 7.3 modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc \views_plugin_argument_validate_taxonomy_term

Validate whether an argument is an acceptable node.

Hierarchy

Expanded class hierarchy of views_plugin_argument_validate_taxonomy_term

1 string reference to 'views_plugin_argument_validate_taxonomy_term'
taxonomy_views_plugins in modules/taxonomy.views.inc
Implementation of hook_views_plugins

File

modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc, line 10
Contains the 'taxonomy term' argument validator plugin.

View source
class views_plugin_argument_validate_taxonomy_term extends views_plugin_argument_validate {
  function validate_form(&$form, &$form_state) {
    $vocabularies = taxonomy_get_vocabularies();
    $options = array();
    foreach ($vocabularies as $voc) {
      $options[$voc->vid] = check_plain($voc->name);
    }
    $form['validate_argument_vocabulary'] = array(
      '#type' => 'checkboxes',
      '#prefix' => '<div id="edit-options-validate-argument-vocabulary-wrapper">',
      '#suffix' => '</div>',
      '#title' => t('Vocabularies'),
      '#options' => $options,
      '#default_value' => isset($this->argument->options['validate_argument_vocabulary']) ? $this->argument->options['validate_argument_vocabulary'] : array(),
      '#description' => t('If you wish to validate for specific vocabularies, check them; if none are checked, all terms will pass.'),
      '#process' => array(
        'expand_checkboxes',
        'views_process_dependency',
      ),
      '#dependency' => array(
        'edit-options-validate-type' => array(
          $this->id,
        ),
      ),
    );
    $form['validate_argument_type'] = array(
      '#type' => 'select',
      '#title' => t('Argument type'),
      '#options' => array(
        'tid' => t('Term ID'),
        'tids' => t('Term IDs separated by , or +'),
        'name' => t('Term name or synonym'),
        'convert' => t('Term name/synonym converted to Term ID'),
      ),
      '#default_value' => isset($this->argument->options['validate_argument_type']) ? $this->argument->options['validate_argument_type'] : 'tid',
      '#description' => t('Select the form of this argument; if using term name, it is generally more efficient to convert it to a term ID and use Taxonomy: Term ID rather than Taxonomy: Term Name" as an argument.'),
      '#process' => array(
        'views_process_dependency',
      ),
      '#dependency' => array(
        'edit-options-validate-type' => array(
          $this->id,
        ),
      ),
    );
    $form['validate_argument_transform'] = array(
      '#type' => 'checkbox',
      '#title' => t('Transform dashes in URL to spaces in term name arguments'),
      '#default_value' => isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE,
      '#process' => array(
        'views_process_dependency',
      ),
      '#dependency' => array(
        'edit-options-validate-argument-type' => array(
          'convert',
          'name',
        ),
      ),
    );
  }
  function validate_argument($argument) {
    $vids = isset($this->argument->options['validate_argument_vocabulary']) ? array_filter($this->argument->options['validate_argument_vocabulary']) : array();
    $type = isset($this->argument->options['validate_argument_type']) ? $this->argument->options['validate_argument_type'] : 'tid';
    $transform = isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE;
    switch ($type) {
      case 'tid':
        if (!is_numeric($argument)) {
          return FALSE;
        }
        $result = db_fetch_object(db_query(db_rewrite_sql("SELECT t.* FROM {term_data} t WHERE t.tid = %d", 't', 'tid', array(
          $argument,
        )), $argument));
        if (!$result) {
          return FALSE;
        }
        return empty($vids) || !empty($vids[$result->vid]);
      case 'tids':

        // An empty argument is not a term so doesn't pass.
        if (empty($argument)) {
          return FALSE;
        }
        $tids = new stdClass();
        $tids->value = $argument;
        $tids = views_break_phrase($argument, $tids);
        if ($tids->value == array(
          -1,
        )) {
          return FALSE;
        }
        $test = drupal_map_assoc($tids->value);
        $titles = array();

        // check, if some tids already verified
        static $validated_cache = array();
        foreach ($test as $tid) {
          if (isset($validated_cache[$tid])) {
            if ($validated_cache[$tid] === FALSE) {
              return FALSE;
            }
            else {
              $titles[] = $validated_cache[$tid];
              unset($test[$tid]);
            }
          }
        }

        // if unverified tids left - verify them and cache results
        if (count($test)) {
          $placeholders = implode(', ', array_fill(0, count($test), '%d'));
          $result = db_query(db_rewrite_sql("SELECT t.* FROM {term_data} t WHERE t.tid IN ({$placeholders})", 't', 'tid', array(
            $test,
          )), $test);
          while ($term = db_fetch_object($result)) {
            if ($vids && empty($vids[$term->vid])) {
              $validated_cache[$term->tid] = FALSE;
              return FALSE;
            }
            $titles[] = $validated_cache[$term->tid] = check_plain($term->name);
            unset($test[$term->tid]);
          }
        }

        // Remove duplicate titles
        $titles = array_unique($titles);
        $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);

        // If this is not empty, we did not find a tid.
        return empty($test);
      case 'name':
      case 'convert':
        $and = '';
        if (!empty($vids)) {
          $and = " AND t.vid IN(" . implode(', ', $vids) . ')';
        }
        if ($transform) {
          $result = db_fetch_object(db_query(db_rewrite_sql("SELECT t.* FROM {term_data} t LEFT JOIN {term_synonym} ts ON ts.tid = t.tid WHERE (replace(t.name, ' ', '-') = '%s' OR replace(ts.name, ' ', '-') = '%s'){$and}", 't', 'tid', array(
            $argument,
            $argument,
          )), $argument, $argument));
        }
        else {
          $result = db_fetch_object(db_query(db_rewrite_sql("SELECT t.* FROM {term_data} t LEFT JOIN {term_synonym} ts ON ts.tid = t.tid WHERE (t.name = '%s' OR ts.name = '%s'){$and}", 't', 'tid', array(
            $argument,
            $argument,
          )), $argument, $argument));
        }
        if (!$result) {
          return FALSE;
        }
        if ($type == 'convert') {
          $this->argument->argument = $result->tid;
        }
        $this->argument->validated_title = check_plain($result->name);
        return TRUE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_object::$options property Except for displays, options for the object will be held here. 1
views_object::construct function Views handlers use a special construct function so that we can more easily construct them with variable arguments. 5
views_object::destroy function 2
views_object::options function Set default options on this object. Called by the constructor in a complex chain to deal with backward compatibility. 1
views_object::option_definition function Information about options for all kinds of purposes will be held here. 9
views_object::set_default_options function Set default options. For backward compatibility, it sends the options array; this is a feature that will likely disappear at some point.
views_object::set_definition function Let the handler know what its full definition is.
views_object::unpack_options function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::_set_option_defaults function 1
views_plugin::additional_theme_functions function Provide a list of additional theme functions for the theme information page
views_plugin::options_form function Provide a form to edit options for this plugin. 5
views_plugin::options_submit function Handle any special handling on the validate form. 3
views_plugin::options_validate function Validate the options form. 3
views_plugin::query function Add anything to the query that we might need to. 3
views_plugin::theme_functions function Provide a full list of possible theme templates used by this style.
views_plugin::validate function Validate that the plugin is correct and can be saved. 2
views_plugin_argument_validate::$option_name property 3
views_plugin_argument_validate::access function Determine if the administrator has the privileges to use this plugin 2
views_plugin_argument_validate::argument_form function
views_plugin_argument_validate::check_access function If we don't have access to the form but are showing it anyway, ensure that the form is safe and cannot be changed from user input.
views_plugin_argument_validate::get_argument function Return the validate argument.
views_plugin_argument_validate::init function Initialize this plugin with the view and the argument it is linked to. Overrides views_plugin::init
views_plugin_argument_validate_taxonomy_term::validate_argument function Overrides views_plugin_argument_validate::validate_argument
views_plugin_argument_validate_taxonomy_term::validate_form function Overrides views_plugin_argument_validate::validate_form