views_handler_filter_term_node_tid_depth.inc

  1. 3.x modules/taxonomy/views_handler_filter_term_node_tid_depth.inc
  2. 2.x modules/taxonomy/views_handler_filter_term_node_tid_depth.inc

File

modules/taxonomy/views_handler_filter_term_node_tid_depth.inc
View source
  1. <?php
  2. /**
  3. * Filter handler for taxonomy terms with depth.
  4. *
  5. * This handler is actually part of the node table and has some restrictions,
  6. * because it uses a subquery to find nodes with
  7. */
  8. class views_handler_filter_term_node_tid_depth extends views_handler_filter_term_node_tid {
  9. function operator_options() {
  10. return array(
  11. 'or' => t('Is one of'),
  12. );
  13. }
  14. function option_definition() {
  15. $options = parent::option_definition();
  16. $options['depth'] = array('default' => 0);
  17. return $options;
  18. }
  19. function extra_options_form(&$form, &$form_state) {
  20. parent::extra_options_form($form, $form_state);
  21. $form['depth'] = array(
  22. '#type' => 'weight',
  23. '#title' => t('Depth'),
  24. '#default_value' => $this->options['depth'],
  25. '#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).'),
  26. );
  27. }
  28. function query() {
  29. // If no filter values are present, then do nothing.
  30. if (count($this->value) == 0) {
  31. return;
  32. }
  33. else if (count($this->value) == 1) {
  34. $placeholder = " = %d";
  35. }
  36. else {
  37. $placeholder = " IN (" . implode(', ', array_fill(0, sizeof($this->value), '%d')) . ")";
  38. }
  39. // The normal use of ensure_my_table() here breaks Views.
  40. // So instead we trick the filter into using the alias of the base table.
  41. // See http://drupal.org/node/271833
  42. // If a relationship is set, we must use the alias it provides.
  43. if (!empty($this->relationship)) {
  44. $this->table_alias = $this->relationship;
  45. }
  46. // If no relationship, then use the alias of the base table.
  47. else if (isset($this->query->table_queue[$this->query->base_table]['alias'])) {
  48. $this->table_alias = $this->query->table_queue[$this->query->base_table]['alias'];
  49. }
  50. // This should never happen, but if it does, we fail quietly.
  51. else {
  52. return;
  53. }
  54. // Now build the subqueries.
  55. $subquery = "\n SELECT tn.vid FROM {term_node} tn\n";
  56. $where = " WHERE tn.tid $placeholder\n";
  57. $args = $this->value;
  58. $last = "tn";
  59. if ($this->options['depth'] > 0) {
  60. $subquery .= " LEFT JOIN {term_hierarchy} th ON th.tid = tn.tid\n";
  61. $last = "th";
  62. foreach (range(1, abs($this->options['depth'])) as $count) {
  63. $subquery .= " LEFT JOIN {term_hierarchy} th$count ON $last.parent = th$count.tid\n";
  64. $where .= " OR th$count.tid $placeholder\n";
  65. $args = array_merge($args, $this->value);
  66. $last = "th$count";
  67. }
  68. }
  69. else if ($this->options['depth'] < 0) {
  70. foreach (range(1, abs($this->options['depth'])) as $count) {
  71. $subquery .= " LEFT JOIN {term_hierarchy} th$count ON $last.tid = th$count.parent\n";
  72. $where .= " OR th$count.tid $placeholder\n";
  73. $args = array_merge($args, $this->value);
  74. $last = "th$count";
  75. }
  76. }
  77. $this->query->add_where(0, "$this->table_alias.$this->real_field IN ($subquery$where )", $args);
  78. }
  79. }