1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Definition of views_handler_filter_term_node_tid_depth.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Filter for taxonomy terms with depth (including selection by simple
|
10 |
|
|
* hierarchical select).
|
11 |
|
|
*
|
12 |
|
|
* This handler is actually part of the node table and has some restrictions,
|
13 |
|
|
* because it uses a subquery to find nodes with.
|
14 |
|
|
*
|
15 |
|
|
* @ingroup views_filter_handlers
|
16 |
|
|
*/
|
17 |
|
|
class shs_handler_filter_term_node_tid_depth extends shs_handler_filter_term_node_tid {
|
18 |
|
|
function operator_options($which = 'title') {
|
19 |
|
|
return array(
|
20 |
|
|
'or' => t('Is one of'),
|
21 |
|
|
);
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
function option_definition() {
|
25 |
|
|
$options = parent::option_definition();
|
26 |
|
|
|
27 |
|
|
$options['depth'] = array('default' => 0);
|
28 |
|
|
|
29 |
|
|
return $options;
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
function extra_options_form(&$form, &$form_state) {
|
33 |
|
|
parent::extra_options_form($form, $form_state);
|
34 |
|
|
|
35 |
|
|
$form['depth'] = array(
|
36 |
|
|
'#type' => 'weight',
|
37 |
|
|
'#title' => t('Depth'),
|
38 |
|
|
'#default_value' => $this->options['depth'],
|
39 |
|
|
'#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).'),
|
40 |
|
|
);
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
function query() {
|
44 |
|
|
// Check if multiple select allowed.
|
45 |
|
|
if ($this->options['expose']['multiple']) {
|
46 |
|
|
// If no filter values are present, then do nothing.
|
47 |
|
|
if ($this->value == 'All') {
|
48 |
|
|
return;
|
49 |
|
|
}
|
50 |
|
|
else {
|
51 |
|
|
$this->value = explode('+', $this->value[0]);
|
52 |
|
|
$this->value = explode(',', end($this->value));
|
53 |
|
|
$operator = 'IN';
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
else {
|
57 |
|
|
// If no filter values are present, then do nothing.
|
58 |
|
|
if (count($this->value) == 0) {
|
59 |
|
|
return;
|
60 |
|
|
}
|
61 |
|
|
elseif (count($this->value) == 1) {
|
62 |
|
|
// $this->value is an array with a single element so convert it.
|
63 |
|
|
if (is_array($this->value)) {
|
64 |
|
|
$this->value = current($this->value);
|
65 |
|
|
}
|
66 |
|
|
$operator = '=';
|
67 |
|
|
}
|
68 |
|
|
else {
|
69 |
|
|
$operator = 'IN';
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
// The normal use of ensure_my_table() here breaks Views.
|
74 |
|
|
// So instead we trick the filter into using the alias of the base table.
|
75 |
|
|
// See http://drupal.org/node/271833
|
76 |
|
|
// If a relationship is set, we must use the alias it provides.
|
77 |
|
|
if (!empty($this->relationship)) {
|
78 |
|
|
$this->table_alias = $this->relationship;
|
79 |
|
|
}
|
80 |
|
|
// If no relationship, then use the alias of the base table.
|
81 |
|
|
elseif (isset($this->query->table_queue[$this->query->base_table]['alias'])) {
|
82 |
|
|
$this->table_alias = $this->query->table_queue[$this->query->base_table]['alias'];
|
83 |
|
|
}
|
84 |
|
|
// This should never happen, but if it does, we fail quietly.
|
85 |
|
|
else {
|
86 |
|
|
return;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
// Now build the subqueries.
|
90 |
|
|
$subquery = db_select('taxonomy_index', 'tn');
|
91 |
|
|
$subquery->addField('tn', 'nid');
|
92 |
|
|
$where = db_or()->condition('tn.tid', $this->value, $operator);
|
93 |
|
|
$last = "tn";
|
94 |
|
|
|
95 |
|
|
if ($this->options['depth'] > 0) {
|
96 |
|
|
$subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
|
97 |
|
|
$last = "th";
|
98 |
|
|
foreach (range(1, abs($this->options['depth'])) as $count) {
|
99 |
|
|
$subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
|
100 |
|
|
$where->condition("th$count.tid", $this->value, $operator);
|
101 |
|
|
$last = "th$count";
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
elseif ($this->options['depth'] < 0) {
|
105 |
|
|
foreach (range(1, abs($this->options['depth'])) as $count) {
|
106 |
|
|
$subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
|
107 |
|
|
$where->condition("th$count.tid", $this->value, $operator);
|
108 |
|
|
$last = "th$count";
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
$subquery->condition($where);
|
113 |
|
|
$this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $subquery, 'IN');
|
114 |
|
|
}
|
115 |
|
|
} |