[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/modules/taxonomy/ -> views_plugin_argument_validate_taxonomy_term.inc (source)

   1  <?php
   2  /**
   3   * @file
   4   * Contains the 'taxonomy term' argument validator plugin.
   5   */
   6  
   7  /**
   8   * Validate whether an argument is an acceptable node.
   9   */
  10  class views_plugin_argument_validate_taxonomy_term extends views_plugin_argument_validate {
  11    function validate_form(&$form, &$form_state) {
  12      $vocabularies = taxonomy_get_vocabularies();
  13      $options = array();
  14      foreach ($vocabularies as $voc) {
  15        $options[$voc->vid] = check_plain($voc->name);
  16      }
  17  
  18      $form['validate_argument_vocabulary'] = array(
  19        '#type' => 'checkboxes',
  20        '#prefix' => '<div id="edit-options-validate-argument-vocabulary-wrapper">',
  21        '#suffix' => '</div>',
  22        '#title' => t('Vocabularies'),
  23        '#options' => $options,
  24        '#default_value' => isset($this->argument->options['validate_argument_vocabulary']) ? $this->argument->options['validate_argument_vocabulary'] : array(),
  25        '#description' => t('If you wish to validate for specific vocabularies, check them; if none are checked, all terms will pass.'),
  26        '#process' => array('expand_checkboxes', 'views_process_dependency'),
  27        '#dependency' => array('edit-options-validate-type' => array($this->id)),
  28      );
  29  
  30      $form['validate_argument_type'] = array(
  31        '#type' => 'select',
  32        '#title' => t('Argument type'),
  33        '#options' => array(
  34          'tid' => t('Term ID'),
  35          'tids' => t('Term IDs separated by , or +'),
  36          'name' => t('Term name or synonym'),
  37          'convert' => t('Term name/synonym converted to Term ID'),
  38        ),
  39        '#default_value' => isset($this->argument->options['validate_argument_type']) ? $this->argument->options['validate_argument_type'] : 'tid',
  40        '#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.'),
  41        '#process' => array('views_process_dependency'),
  42        '#dependency' => array('edit-options-validate-type' => array($this->id)),
  43      );
  44  
  45      $form['validate_argument_transform'] = array(
  46        '#type' => 'checkbox',
  47        '#title' => t('Transform dashes in URL to spaces in term name arguments'),
  48        '#default_value' => isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE,
  49        '#process' => array('views_process_dependency'),
  50        '#dependency' => array('edit-options-validate-argument-type' => array('convert', 'name')),
  51      );
  52    }
  53  
  54    function validate_argument($argument) {
  55      $vids = isset($this->argument->options['validate_argument_vocabulary']) ? array_filter($this->argument->options['validate_argument_vocabulary']) : array();
  56      $type = isset($this->argument->options['validate_argument_type']) ? $this->argument->options['validate_argument_type'] : 'tid';
  57      $transform = isset($this->argument->options['validate_argument_transform']) ? $this->argument->options['validate_argument_transform'] : FALSE;
  58  
  59      switch ($type) {
  60        case 'tid':
  61          if (!is_numeric($argument)) {
  62            return FALSE;
  63          }
  64  
  65          $result = db_fetch_object(db_query("SELECT * FROM {term_data} WHERE tid = %d", $argument));
  66          if (!$result) {
  67            return FALSE;
  68          }
  69  
  70          return empty($vids) || !empty($vids[$result->vid]);
  71        case 'tids':
  72          // An empty argument is not a term so doesn't pass.
  73          if (empty($argument)) {
  74            return FALSE;
  75          }
  76  
  77          $tids = new stdClass();
  78          $tids->value = $argument;
  79          $tids = views_break_phrase($argument, $tids);
  80          if ($tids->value == array(-1)) {
  81            return FALSE;
  82          }
  83  
  84          $test = drupal_map_assoc($tids->value);
  85          $titles = array();
  86  
  87          // check, if some tids already verified
  88          static $validated_cache = array();
  89          foreach ($test as $tid) {
  90            if (isset($validated_cache[$tid])) {
  91              if ($validated_cache[$tid] === FALSE) {
  92                return FALSE;
  93              }
  94              else {
  95                $titles[] = $validated_cache[$tid];
  96                unset($test[$tid]);
  97              }
  98            }
  99          }
 100  
 101  
 102          // if unverified tids left - verify them and cache results
 103          if (count($test)) {
 104            $placeholders = implode(', ', array_fill(0, count($test), '%d'));
 105  
 106            $result = db_query("SELECT * FROM {term_data} WHERE tid IN ($placeholders)", $test);
 107            while ($term = db_fetch_object($result)) {
 108              if ($vids && empty($vids[$term->vid])) {
 109                $validated_cache[$term->tid] = FALSE;
 110                return FALSE;
 111              }
 112  
 113              $titles[] = $validated_cache[$term->tid] = check_plain($term->name);
 114              unset($test[$term->tid]);
 115            }
 116          }
 117  
 118          // Remove duplicate titles
 119          $titles = array_unique($titles);
 120  
 121          $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);
 122          // If this is not empty, we did not find a tid.
 123          return empty($test);
 124        case 'name':
 125        case 'convert':
 126          $and = '';
 127          if (!empty($vids)) {
 128            $and = " AND td.vid IN(" . implode(', ', $vids) . ')';
 129          }
 130          if ($transform) {
 131            $result = db_fetch_object(db_query("SELECT td.* FROM {term_data} td LEFT JOIN {term_synonym} ts ON ts.tid = td.tid WHERE (replace(td.name, ' ', '-') = '%s' OR replace(ts.name, ' ', '-') = '%s')$and", $argument, $argument));
 132          }
 133          else {
 134            $result = db_fetch_object(db_query("SELECT td.* FROM {term_data} td LEFT JOIN {term_synonym} ts ON ts.tid = td.tid WHERE (td.name = '%s' OR ts.name = '%s')$and", $argument, $argument));
 135          }
 136          if (!$result) {
 137            return FALSE;
 138          }
 139  
 140          if ($type == 'convert') {
 141            $this->argument->argument = $result->tid;
 142          }
 143          $this->argument->validated_title = check_plain($result->name);
 144  
 145          return TRUE;
 146      }
 147    }
 148  }


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