You are here

function views_plugin_argument_validate_taxonomy_term::validate_argument 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::validate_argument()
  2. 7.3 modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc \views_plugin_argument_validate_taxonomy_term::validate_argument()

Overrides views_plugin_argument_validate::validate_argument

File

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

Class

views_plugin_argument_validate_taxonomy_term
Validate whether an argument is an acceptable node.

Code

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;
  }
}