views_handler_argument_term_node_tid_depth_join.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_argument_term_node_tid_depth_join.
  5. */
  6. /**
  7. * Argument 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_argument_handlers
  13. */
  14. class views_handler_argument_term_node_tid_depth_join extends views_handler_argument {
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['depth'] = array('default' => 0);
  18. $options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
  19. $options['set_breadcrumb'] = array('default' => FALSE, 'bool' => TRUE);
  20. $options['use_taxonomy_term_path'] = array('default' => FALSE, 'bool' => TRUE);
  21. return $options;
  22. }
  23. function options_form(&$form, &$form_state) {
  24. $form['depth'] = array(
  25. '#type' => 'weight',
  26. '#title' => t('Depth'),
  27. '#default_value' => $this->options['depth'],
  28. '#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).'),
  29. );
  30. $form['break_phrase'] = array(
  31. '#type' => 'checkbox',
  32. '#title' => t('Allow multiple values'),
  33. '#description' => t('If selected, users can enter multiple values in the form of 1+2+3. Due to the number of JOINs it would require, AND will be treated as OR with this filter.'),
  34. '#default_value' => !empty($this->options['break_phrase']),
  35. );
  36. $form['set_breadcrumb'] = array(
  37. '#type' => 'checkbox',
  38. '#title' => t("Set the breadcrumb for the term parents"),
  39. '#description' => t('If selected, the breadcrumb trail will include all parent terms, each one linking to this view. Note that this only works if just one term was received.'),
  40. '#default_value' => !empty($this->options['set_breadcrumb']),
  41. );
  42. $form['use_taxonomy_term_path'] = array(
  43. '#type' => 'checkbox',
  44. '#title' => t("Use Drupal's taxonomy term path to create breadcrumb links"),
  45. '#description' => t('If selected, the links in the breadcrumb trail will be created using the standard drupal method instead of the custom views method. This is useful if you are using modules like taxonomy redirect to modify your taxonomy term links.'),
  46. '#default_value' => !empty($this->options['use_taxonomy_term_path']),
  47. '#dependency' => array('edit-options-set-breadcrumb' => array(TRUE)),
  48. );
  49. parent::options_form($form, $form_state);
  50. }
  51. function set_breadcrumb(&$breadcrumb) {
  52. if (empty($this->options['set_breadcrumb']) || !is_numeric($this->argument)) {
  53. return;
  54. }
  55. return views_taxonomy_set_breadcrumb($breadcrumb, $this);
  56. }
  57. /**
  58. * Override default_actions() to remove summary actions.
  59. */
  60. function default_actions($which = NULL) {
  61. if ($which) {
  62. if (in_array($which, array('ignore', 'not found', 'empty', 'default'))) {
  63. return parent::default_actions($which);
  64. }
  65. return;
  66. }
  67. $actions = parent::default_actions();
  68. unset($actions['summary asc']);
  69. unset($actions['summary desc']);
  70. unset($actions['summary asc by count']);
  71. unset($actions['summary desc by count']);
  72. return $actions;
  73. }
  74. function query($group_by = FALSE) {
  75. $this->ensure_my_table();
  76. if (!empty($this->options['break_phrase'])) {
  77. $tids = new stdClass();
  78. $tids->value = $this->argument;
  79. $tids = views_break_phrase($this->argument, $tids);
  80. if ($tids->value == array(-1)) {
  81. return FALSE;
  82. }
  83. if (count($tids->value) > 1) {
  84. $operator = 'IN';
  85. }
  86. else {
  87. $operator = '=';
  88. }
  89. $tids = $tids->value;
  90. }
  91. else {
  92. $operator = "=";
  93. $tids = $this->argument;
  94. }
  95. // The tids variable can be an integer or an array of integers.
  96. $tids = is_array($tids) ? $tids : array($tids);
  97. if ($this->options['depth'] > 0) {
  98. // When the depth is positive search the children.
  99. foreach ($tids as $tid) {
  100. // The term must be loaded to get vid for use in taxonomy_get_tree().
  101. if ($term = taxonomy_term_load($tid)) {
  102. // For every tid argument find all the children down to the depth set
  103. // in the options and save the tids for the condition.
  104. $tree = taxonomy_get_tree($term->vid, $term->tid, (int) $this->options['depth']);
  105. $tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $tree));
  106. }
  107. }
  108. }
  109. elseif ($this->options['depth'] < 0) {
  110. // When the depth is negative search the parents.
  111. foreach ($tids as $tid) {
  112. // For every tid argument find all the parents up to the depth set
  113. // in the options and add the tids into the array. Since there is
  114. // no taxonomy function to get all parents with a depth limit it
  115. // is done here building a multidimensional array.
  116. if ($term = taxonomy_term_load($tid)) {
  117. // A variable is needed to track the current depth level.
  118. $n = 0;
  119. // Initialise our depth based parents array with the leaf term.
  120. $parents[$n--][] = $term;
  121. while ($n >= $this->options['depth']) {
  122. // At each depth find the parents of the current terms.
  123. // It is important to note that it is possible for a term to have
  124. // multiple parents so get the parents of every parent and so on.
  125. $parents[$n] = array();
  126. foreach ($parents[$n + 1] as $term) {
  127. $parents[$n] += taxonomy_get_parents($term->tid);
  128. }
  129. // Save all the tids for the condition.
  130. $tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $parents[$n]));
  131. $n--;
  132. }
  133. }
  134. }
  135. }
  136. // Check the size of the array and set the operator accordingly.
  137. if (count($tids) > 1) {
  138. $operator = 'IN';
  139. }
  140. else {
  141. $tids = current($tids);
  142. $operator = '=';
  143. }
  144. // Join on taxonomy index table.
  145. $join = new views_join();
  146. $join->table = 'taxonomy_index';
  147. $join->field = 'nid';
  148. $join->left_table = $this->table_alias;
  149. $join->left_field = $this->real_field;
  150. $join->type = 'INNER';
  151. $join->extra = array(
  152. array(
  153. 'field' => 'tid',
  154. 'value' => $tids,
  155. 'operator' => $operator,
  156. )
  157. );
  158. $taxonomy_index_alias = $this->query->add_relationship('taxonomy_index', $join, 'node');
  159. // Distinct is required to prevent duplicate rows.
  160. $this->query->distinct = TRUE;
  161. }
  162. function title() {
  163. $term = taxonomy_term_load($this->argument);
  164. if (!empty($term)) {
  165. return check_plain($term->name);
  166. }
  167. return t('No name');
  168. }
  169. }