Projet

Général

Profil

Paste
Télécharger (3,52 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_filter_term_node_tid_depth.
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 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
    // Now build the subqueries.
75
    $subquery = db_select('taxonomy_index', 'tn');
76
    $subquery->addField('tn', 'nid');
77
    $where = db_or()->condition('tn.tid', $this->value, $operator);
78
    $last = "tn";
79

    
80
    if ($this->options['depth'] > 0) {
81
      $subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
82
      $last = "th";
83
      foreach (range(1, abs($this->options['depth'])) as $count) {
84
        $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
85
        $where->condition("th$count.tid", $this->value, $operator);
86
        $last = "th$count";
87
      }
88
    }
89
    elseif ($this->options['depth'] < 0) {
90
      foreach (range(1, abs($this->options['depth'])) as $count) {
91
        $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
92
        $where->condition("th$count.tid", $this->value, $operator);
93
        $last = "th$count";
94
      }
95
    }
96

    
97
    $subquery->condition($where);
98
    $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $subquery, 'IN');
99
  }
100
}