[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

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

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


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7