~spreadubuntu/spreadubuntu/devel-drupal6

« back to all changes in this revision

Viewing changes to sites/all/modules/views/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc

  • Committer: ruben
  • Date: 2009-06-08 09:38:49 UTC
  • Revision ID: ruben@captive-20090608093849-s1qtsyctv2vwp1x1
SpreadUbuntu moving to Drupal6. Based on ubuntu-drupal theme and adding our modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id: views_plugin_argument_validate_taxonomy_term.inc,v 1.4 2009/04/08 05:10:07 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-type' => array($this->id)),
 
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
        $placeholders = implode(', ', array_fill(0, sizeof($tids->value), '%d'));
 
81
 
 
82
        $test = drupal_map_assoc($tids->value);
 
83
        $titles = array();
 
84
 
 
85
        $result = db_query("SELECT * FROM {term_data} WHERE tid IN ($placeholders)", $tids->value);
 
86
        while ($term = db_fetch_object($result)) {
 
87
          if ($vids && empty($vids[$term->vid])) {
 
88
            return FALSE;
 
89
          }
 
90
 
 
91
          $titles[] = check_plain($term->name);
 
92
          unset($test[$term->tid]);
 
93
        }
 
94
 
 
95
        $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);
 
96
        // If this is not empty, we did not find a tid.
 
97
        return empty($test);
 
98
      case 'name':
 
99
      case 'convert':
 
100
        $and = '';
 
101
        if (!empty($vids)) {
 
102
          $and = " AND td.vid IN(" . implode(', ', $vids) . ')';
 
103
        }
 
104
        if ($transform) {
 
105
          $argument = str_replace('-', ' ', $argument);
 
106
        }
 
107
        $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));
 
108
        if (!$result) {
 
109
          return FALSE;
 
110
        }
 
111
 
 
112
        if ($type == 'convert') {
 
113
          $this->argument->argument = $result->tid;
 
114
          $this->argument->validated_title = check_plain($result->name);
 
115
        }
 
116
 
 
117
        return TRUE;
 
118
    }
 
119
  }
 
120
}