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
|
|
18
|
/**
|
19
|
* {@inheritdoc}
|
20
|
*/
|
21
|
public function operator_options($which = 'title') {
|
22
|
return array(
|
23
|
'or' => t('Is one of'),
|
24
|
);
|
25
|
}
|
26
|
|
27
|
/**
|
28
|
* {@inheritdoc}
|
29
|
*/
|
30
|
public function option_definition() {
|
31
|
$options = parent::option_definition();
|
32
|
|
33
|
$options['depth'] = array('default' => 0);
|
34
|
|
35
|
return $options;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* {@inheritdoc}
|
40
|
*/
|
41
|
public function extra_options_form(&$form, &$form_state) {
|
42
|
parent::extra_options_form($form, $form_state);
|
43
|
|
44
|
$form['depth'] = array(
|
45
|
'#type' => 'weight',
|
46
|
'#title' => t('Depth'),
|
47
|
'#default_value' => $this->options['depth'],
|
48
|
'#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).'),
|
49
|
);
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* {@inheritdoc}
|
54
|
*/
|
55
|
public function query() {
|
56
|
// If no filter values are present, then do nothing.
|
57
|
if (count($this->value) == 0) {
|
58
|
return;
|
59
|
}
|
60
|
elseif (count($this->value) == 1) {
|
61
|
// Somethis $this->value is an array with a single element so convert it.
|
62
|
if (is_array($this->value)) {
|
63
|
$this->value = current($this->value);
|
64
|
}
|
65
|
$operator = '=';
|
66
|
}
|
67
|
else {
|
68
|
$operator = 'IN';
|
69
|
// " IN ("
|
70
|
// . implode(', ', array_fill(0, sizeof($this->value), '%d'))
|
71
|
// . ")";
|
72
|
}
|
73
|
|
74
|
// The normal use of ensure_my_table() here breaks Views.
|
75
|
// So instead we trick the filter into using the alias of the base table.
|
76
|
// See http://drupal.org/node/271833
|
77
|
// If a relationship is set, we must use the alias it provides.
|
78
|
if (!empty($this->relationship)) {
|
79
|
$this->table_alias = $this->relationship;
|
80
|
}
|
81
|
// If no relationship, then use the alias of the base table.
|
82
|
elseif (isset($this->query->table_queue[$this->query->base_table]['alias'])) {
|
83
|
$this->table_alias = $this->query->table_queue[$this->query->base_table]['alias'];
|
84
|
}
|
85
|
// This should never happen, but if it does, we fail quietly.
|
86
|
else {
|
87
|
return;
|
88
|
}
|
89
|
|
90
|
// Now build the subqueries.
|
91
|
$subquery = db_select('taxonomy_index', 'tn');
|
92
|
$subquery->addField('tn', 'nid');
|
93
|
$where = db_or()->condition('tn.tid', $this->value, $operator);
|
94
|
$last = "tn";
|
95
|
|
96
|
if ($this->options['depth'] > 0) {
|
97
|
$subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
|
98
|
$last = "th";
|
99
|
foreach (range(1, abs($this->options['depth'])) as $count) {
|
100
|
$subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
|
101
|
$where->condition("th$count.tid", $this->value, $operator);
|
102
|
$last = "th$count";
|
103
|
}
|
104
|
}
|
105
|
elseif ($this->options['depth'] < 0) {
|
106
|
foreach (range(1, abs($this->options['depth'])) as $count) {
|
107
|
$subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
|
108
|
$where->condition("th$count.tid", $this->value, $operator);
|
109
|
$last = "th$count";
|
110
|
}
|
111
|
}
|
112
|
|
113
|
$subquery->condition($where);
|
114
|
$this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $subquery, 'IN');
|
115
|
}
|
116
|
|
117
|
}
|