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

File

modules/taxonomy/views_handler_argument_term_node_tid_depth.inc
View source
  1. <?php
  2. /**
  3. * Argument 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_argument_term_node_tid_depth extends views_handler_argument {
  9. function option_definition() {
  10. $options = parent::option_definition();
  11. $options['depth'] = array('default' => 0);
  12. $options['break_phrase'] = array('default' => FALSE);
  13. $options['set_breadcrumb'] = array('default' => FALSE);
  14. $options['use_taxonomy_term_path'] = array('default' => FALSE);
  15. return $options;
  16. }
  17. function options_form(&$form, &$form_state) {
  18. parent::options_form($form, $form_state);
  19. $form['depth'] = array(
  20. '#type' => 'weight',
  21. '#title' => t('Depth'),
  22. '#default_value' => $this->options['depth'],
  23. '#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).'),
  24. );
  25. $form['break_phrase'] = array(
  26. '#type' => 'checkbox',
  27. '#title' => t('Allow multiple terms per argument'),
  28. '#description' => t('If selected, users can enter multiple arguments in the form of 1+2+3. Due to the number of JOINs it would require, AND will be treated as OR with this argument.'),
  29. '#default_value' => !empty($this->options['break_phrase']),
  30. );
  31. $form['set_breadcrumb'] = array(
  32. '#type' => 'checkbox',
  33. '#title' => t("Set the breadcrumb for the term parents"),
  34. '#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.'),
  35. '#default_value' => !empty($this->options['set_breadcrumb']),
  36. );
  37. $form['use_taxonomy_term_path'] = array(
  38. '#type' => 'checkbox',
  39. '#title' => t("Use Drupal's taxonomy term path to create breadcrumb links"),
  40. '#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.'),
  41. '#default_value' => !empty($this->options['use_taxonomy_term_path']),
  42. '#process' => array('views_process_dependency'),
  43. '#dependency' => array('edit-options-set-breadcrumb' => array(TRUE)),
  44. );
  45. }
  46. function set_breadcrumb(&$breadcrumb) {
  47. if (empty($this->options['set_breadcrumb']) || !is_numeric($this->argument)) {
  48. return;
  49. }
  50. return views_taxonomy_set_breadcrumb($breadcrumb, $this);
  51. }
  52. /**
  53. * Override default_actions() to remove summary actions.
  54. */
  55. function default_actions($which = NULL) {
  56. if ($which) {
  57. if (in_array($which, array('ignore', 'not found', 'empty', 'default'))) {
  58. return parent::default_actions($which);
  59. }
  60. return;
  61. }
  62. $actions = parent::default_actions();
  63. unset($actions['summary asc']);
  64. unset($actions['summary desc']);
  65. return $actions;
  66. }
  67. function query() {
  68. $this->ensure_my_table();
  69. if (!empty($this->options['break_phrase'])) {
  70. $tids = new stdClass();
  71. $tids->value = $this->argument;
  72. $tids = views_break_phrase($this->argument, $tids);
  73. if ($tids->value == -1) {
  74. return FALSE;
  75. }
  76. if (count($tids->value) > 1) {
  77. $placeholder = " IN (" . implode(', ', array_fill(0, sizeof($tids->value), '%d')) . ")";
  78. }
  79. else {
  80. $placeholder = " = %d";
  81. }
  82. $tids = $tids->value;
  83. }
  84. else {
  85. $placeholder = "= %d";
  86. $tids = array($this->argument);
  87. }
  88. $subquery = "\n SELECT tn.vid FROM {term_node} tn\n";
  89. $where = " WHERE tn.tid $placeholder\n";
  90. $args = $tids;
  91. $last = "tn";
  92. if ($this->options['depth'] > 0) {
  93. $subquery .= " LEFT JOIN {term_hierarchy} th ON th.tid = tn.tid\n";
  94. $last = "th";
  95. foreach (range(1, abs($this->options['depth'])) as $count) {
  96. $subquery .= " LEFT JOIN {term_hierarchy} th$count ON $last.parent = th$count.tid\n";
  97. $where .= " OR th$count.tid $placeholder\n";
  98. $args = array_merge($args, $tids);
  99. $last = "th$count";
  100. }
  101. }
  102. else if ($this->options['depth'] < 0) {
  103. foreach (range(1, abs($this->options['depth'])) as $count) {
  104. $subquery .= " LEFT JOIN {term_hierarchy} th$count ON $last.tid = th$count.parent\n";
  105. $where .= " OR th$count.tid $placeholder\n";
  106. $args = array_merge($args, $tids);
  107. $last = "th$count";
  108. }
  109. }
  110. $this->query->add_where(0, "$this->table_alias.$this->real_field IN ($subquery$where )", $args);
  111. }
  112. function title() {
  113. $term = taxonomy_get_term($this->argument);
  114. if (!empty($term)) {
  115. return check_plain($term->name);
  116. }
  117. // TODO review text
  118. return t('No name');
  119. }
  120. }