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
|
|
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
|
// The tids variable can be an integer or an array of integers.
|
91
|
$tids = is_array($this->value) ? $this->value : array($this->value);
|
92
|
|
93
|
if ($this->options['depth'] > 0) {
|
94
|
// When the depth is positive search the children.
|
95
|
foreach ($tids as $tid) {
|
96
|
// The term must be loaded to get vid for use in taxonomy_get_tree().
|
97
|
if ($term = taxonomy_term_load($tid)) {
|
98
|
// For every tid argument find all the children down to the depth set
|
99
|
// in the options and save the tids for the condition.
|
100
|
$tree = taxonomy_get_tree($term->vid, $term->tid, (int) $this->options['depth']);
|
101
|
$tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $tree));
|
102
|
}
|
103
|
}
|
104
|
}
|
105
|
elseif ($this->options['depth'] < 0) {
|
106
|
// When the depth is negative search the parents.
|
107
|
foreach ($tids as $tid) {
|
108
|
// For every tid argument find all the parents up to the depth set
|
109
|
// in the options and add the tids into the array. Since there is
|
110
|
// no taxonomy function to get all parents with a depth limit it
|
111
|
// is done here building a multidimensional array.
|
112
|
if ($term = taxonomy_term_load($tid)) {
|
113
|
// A variable is needed to track the current depth level.
|
114
|
$n = 0;
|
115
|
// Initialise our depth based parents array with the leaf term.
|
116
|
$parents[$n--][] = $term;
|
117
|
while ($n >= $this->options['depth']) {
|
118
|
// At each depth find the parents of the current terms.
|
119
|
// It is important to note that it is possible for a term to have
|
120
|
// multiple parents so get the parents of every parent and so on.
|
121
|
$parents[$n] = array();
|
122
|
foreach ($parents[$n + 1] as $term) {
|
123
|
$parents[$n] += taxonomy_get_parents($term->tid);
|
124
|
}
|
125
|
// Save all the tids for the condition.
|
126
|
$tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $parents[$n]));
|
127
|
$n--;
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
}
|
132
|
|
133
|
// Check the size of the array and set the operator accordingly.
|
134
|
if (count($tids) > 1) {
|
135
|
$operator = 'IN';
|
136
|
}
|
137
|
else {
|
138
|
$tids = current($tids);
|
139
|
$operator = '=';
|
140
|
}
|
141
|
|
142
|
// Join on taxonomy index table.
|
143
|
$join = new views_join();
|
144
|
$join->table = 'taxonomy_index';
|
145
|
$join->field = 'nid';
|
146
|
$join->left_table = $this->table_alias;
|
147
|
$join->left_field = $this->real_field;
|
148
|
$join->type = 'INNER';
|
149
|
$join->extra = array(
|
150
|
array(
|
151
|
'field' => 'tid',
|
152
|
'value' => $tids,
|
153
|
'operator' => $operator,
|
154
|
),
|
155
|
);
|
156
|
$taxonomy_index_alias = $this->query->add_relationship('taxonomy_index', $join, 'node');
|
157
|
}
|
158
|
|
159
|
}
|