views_handler_argument_term_node_tid_depth.inc

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

Definition of views_handler_argument_term_node_tid_depth.

File

modules/taxonomy/views_handler_argument_term_node_tid_depth.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_argument_term_node_tid_depth.
  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 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. // Now build the subqueries.
  96. $subquery = db_select('taxonomy_index', 'tn');
  97. $subquery->addField('tn', 'nid');
  98. $where = db_or()->condition('tn.tid', $tids, $operator);
  99. $last = "tn";
  100. if ($this->options['depth'] > 0) {
  101. $subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
  102. $last = "th";
  103. foreach (range(1, abs($this->options['depth'])) as $count) {
  104. $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
  105. $where->condition("th$count.tid", $tids, $operator);
  106. $last = "th$count";
  107. }
  108. }
  109. elseif ($this->options['depth'] < 0) {
  110. foreach (range(1, abs($this->options['depth'])) as $count) {
  111. $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
  112. $where->condition("th$count.tid", $tids, $operator);
  113. $last = "th$count";
  114. }
  115. }
  116. $subquery->condition($where);
  117. $this->query->add_where(0, "$this->table_alias.$this->real_field", $subquery, 'IN');
  118. }
  119. function title() {
  120. $term = taxonomy_term_load($this->argument);
  121. if (!empty($term)) {
  122. return check_plain($term->name);
  123. }
  124. // TODO review text
  125. return t('No name');
  126. }
  127. }