Projet

Général

Profil

Paste
Télécharger (5,12 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_handler_filter_term_node_tid_depth_join.inc @ 7547bb19

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_filter_term_node_tid_depth_join.
6
 */
7

    
8
/**
9
 * Filter handler for taxonomy terms with depth.
10
 *
11
 * This handler is actually part of the node table and has some restrictions,
12
 * because it uses a subquery to find nodes with.
13
 *
14
 * @ingroup views_filter_handlers
15
 */
16
class views_handler_filter_term_node_tid_depth_join extends views_handler_filter_term_node_tid {
17
  function operator_options($which = 'title') {
18
    return array(
19
      'or' => t('Is one of'),
20
    );
21
  }
22

    
23
  function option_definition() {
24
    $options = parent::option_definition();
25

    
26
    $options['depth'] = array('default' => 0);
27

    
28
    return $options;
29
  }
30

    
31
  function extra_options_form(&$form, &$form_state) {
32
    parent::extra_options_form($form, $form_state);
33

    
34
    $form['depth'] = array(
35
      '#type' => 'weight',
36
      '#title' => t('Depth'),
37
      '#default_value' => $this->options['depth'],
38
      '#description' => t('The depth will match nodes tagged with terms in the hierarchy. For example, if you have the term "fruit" and a child term "apple", with a depth of 1 (or higher) then filtering for the term "fruit" will get nodes that are tagged with "apple" as well as "fruit". If negative, the reverse is true; searching for "apple" will also pick up nodes tagged with "fruit" if depth is -1 (or lower).'),
39
    );
40
  }
41

    
42
  function query() {
43
    // If no filter values are present, then do nothing.
44
    if (count($this->value) == 0) {
45
      return;
46
    }
47
    elseif (count($this->value) == 1) {
48
      // Somethis $this->value is an array with a single element so convert it.
49
      if (is_array($this->value)) {
50
        $this->value = current($this->value);
51
      }
52
      $operator = '=';
53
    }
54
    else {
55
      $operator = 'IN';# " IN (" . implode(', ', array_fill(0, sizeof($this->value), '%d')) . ")";
56
    }
57

    
58
    // The normal use of ensure_my_table() here breaks Views.
59
    // So instead we trick the filter into using the alias of the base table.
60
    // See http://drupal.org/node/271833
61
    // If a relationship is set, we must use the alias it provides.
62
    if (!empty($this->relationship)) {
63
      $this->table_alias = $this->relationship;
64
    }
65
    // If no relationship, then use the alias of the base table.
66
    elseif (isset($this->query->table_queue[$this->query->base_table]['alias'])) {
67
      $this->table_alias = $this->query->table_queue[$this->query->base_table]['alias'];
68
    }
69
    // This should never happen, but if it does, we fail quietly.
70
    else {
71
      return;
72
    }
73

    
74
    // The tids variable can be an integer or an array of integers.
75
    $tids = is_array($this->value) ?  $this->value : array($this->value);
76

    
77
    if ($this->options['depth'] > 0) {
78
      // When the depth is positive search the children.
79
      foreach ($tids as $tid) {
80
        // The term must be loaded to get vid for use in taxonomy_get_tree().
81
        if ($term = taxonomy_term_load($tid)) {
82
          // For every tid argument find all the children down to the depth set
83
          // in the options and save the tids for the condition.
84
          $tree = taxonomy_get_tree($term->vid, $term->tid, (int) $this->options['depth']);
85
          $tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $tree));
86
        }
87
      }
88
    }
89
    elseif ($this->options['depth'] < 0) {
90
      // When the depth is negative search the parents.
91
      foreach ($tids as $tid) {
92
        // For every tid argument find all the parents up to the depth set
93
        // in the options and add the tids into the array. Since there is
94
        // no taxonomy function to get all parents with a depth limit it
95
        // is done here building a multidimensional array.
96
        if ($term = taxonomy_term_load($tid)) {
97
          // A variable is needed to track the current depth level.
98
          $n = 0;
99
          // Initialise our depth based parents array with the leaf term.
100
          $parents[$n--][] = $term;
101
          while ($n >= $this->options['depth']) {
102
            // At each depth find the parents of the current terms.
103
            // It is important to note that it is possible for a term to have
104
            // multiple parents so get the parents of every parent and so on.
105
            $parents[$n] = array();
106
            foreach ($parents[$n + 1] as $term) {
107
              $parents[$n] += taxonomy_get_parents($term->tid);
108
            }
109
            // Save all the tids for the condition.
110
            $tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $parents[$n]));
111
            $n--;
112
          }
113
        }
114
      }
115
    }
116

    
117
    // Check the size of the array and set the operator accordingly.
118
    if (count($tids) > 1) {
119
      $operator = 'IN';
120
    }
121
    else {
122
      $tids = current($tids);
123
      $operator = '=';
124
    }
125

    
126
    // Join on taxonomy index table.
127
    $join = new views_join();
128
    $join->table = 'taxonomy_index';
129
    $join->field = 'nid';
130
    $join->left_table = $this->table_alias;
131
    $join->left_field = $this->real_field;
132
    $join->type = 'INNER';
133
    $join->extra = array(
134
      array(
135
        'field' => 'tid',
136
        'value' => $tids,
137
        'operator' => $operator,
138
      )
139
    );
140
    $taxonomy_index_alias = $this->query->add_relationship('taxonomy_index', $join, 'node');
141
  }
142
}