views_handler_filter_term_node_tid_depth_join.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_filter_term_node_tid_depth_join.
  5. */
  6. /**
  7. * Filter handler for taxonomy terms with depth.
  8. *
  9. * This handler is actually part of the node table and has some restrictions,
  10. * because it uses a subquery to find nodes with.
  11. *
  12. * @ingroup views_filter_handlers
  13. */
  14. class views_handler_filter_term_node_tid_depth_join extends views_handler_filter_term_node_tid {
  15. function operator_options($which = 'title') {
  16. return array(
  17. 'or' => t('Is one of'),
  18. );
  19. }
  20. function option_definition() {
  21. $options = parent::option_definition();
  22. $options['depth'] = array('default' => 0);
  23. return $options;
  24. }
  25. function extra_options_form(&$form, &$form_state) {
  26. parent::extra_options_form($form, $form_state);
  27. $form['depth'] = array(
  28. '#type' => 'weight',
  29. '#title' => t('Depth'),
  30. '#default_value' => $this->options['depth'],
  31. '#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).'),
  32. );
  33. }
  34. function query() {
  35. // If no filter values are present, then do nothing.
  36. if (count($this->value) == 0) {
  37. return;
  38. }
  39. elseif (count($this->value) == 1) {
  40. // Somethis $this->value is an array with a single element so convert it.
  41. if (is_array($this->value)) {
  42. $this->value = current($this->value);
  43. }
  44. $operator = '=';
  45. }
  46. else {
  47. $operator = 'IN';# " IN (" . implode(', ', array_fill(0, sizeof($this->value), '%d')) . ")";
  48. }
  49. // The normal use of ensure_my_table() here breaks Views.
  50. // So instead we trick the filter into using the alias of the base table.
  51. // See http://drupal.org/node/271833
  52. // If a relationship is set, we must use the alias it provides.
  53. if (!empty($this->relationship)) {
  54. $this->table_alias = $this->relationship;
  55. }
  56. // If no relationship, then use the alias of the base table.
  57. elseif (isset($this->query->table_queue[$this->query->base_table]['alias'])) {
  58. $this->table_alias = $this->query->table_queue[$this->query->base_table]['alias'];
  59. }
  60. // This should never happen, but if it does, we fail quietly.
  61. else {
  62. return;
  63. }
  64. // The tids variable can be an integer or an array of integers.
  65. $tids = is_array($this->value) ? $this->value : array($this->value);
  66. if ($this->options['depth'] > 0) {
  67. // When the depth is positive search the children.
  68. foreach ($tids as $tid) {
  69. // The term must be loaded to get vid for use in taxonomy_get_tree().
  70. if ($term = taxonomy_term_load($tid)) {
  71. // For every tid argument find all the children down to the depth set
  72. // in the options and save the tids for the condition.
  73. $tree = taxonomy_get_tree($term->vid, $term->tid, (int) $this->options['depth']);
  74. $tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $tree));
  75. }
  76. }
  77. }
  78. elseif ($this->options['depth'] < 0) {
  79. // When the depth is negative search the parents.
  80. foreach ($tids as $tid) {
  81. // For every tid argument find all the parents up to the depth set
  82. // in the options and add the tids into the array. Since there is
  83. // no taxonomy function to get all parents with a depth limit it
  84. // is done here building a multidimensional array.
  85. if ($term = taxonomy_term_load($tid)) {
  86. // A variable is needed to track the current depth level.
  87. $n = 0;
  88. // Initialise our depth based parents array with the leaf term.
  89. $parents[$n--][] = $term;
  90. while ($n >= $this->options['depth']) {
  91. // At each depth find the parents of the current terms.
  92. // It is important to note that it is possible for a term to have
  93. // multiple parents so get the parents of every parent and so on.
  94. $parents[$n] = array();
  95. foreach ($parents[$n + 1] as $term) {
  96. $parents[$n] += taxonomy_get_parents($term->tid);
  97. }
  98. // Save all the tids for the condition.
  99. $tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $parents[$n]));
  100. $n--;
  101. }
  102. }
  103. }
  104. }
  105. // Check the size of the array and set the operator accordingly.
  106. if (count($tids) > 1) {
  107. $operator = 'IN';
  108. }
  109. else {
  110. $tids = current($tids);
  111. $operator = '=';
  112. }
  113. // Join on taxonomy index table.
  114. $join = new views_join();
  115. $join->table = 'taxonomy_index';
  116. $join->field = 'nid';
  117. $join->left_table = $this->table_alias;
  118. $join->left_field = $this->real_field;
  119. $join->type = 'INNER';
  120. $join->extra = array(
  121. array(
  122. 'field' => 'tid',
  123. 'value' => $tids,
  124. 'operator' => $operator,
  125. )
  126. );
  127. $taxonomy_index_alias = $this->query->add_relationship('taxonomy_index', $join, 'node');
  128. }
  129. }