Projet

Général

Profil

Paste
Télécharger (5,17 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_handler_argument_term_node_tid_depth.inc @ 7547bb19

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_argument_term_node_tid_depth.
6
 */
7

    
8
/**
9
 * Argument handler for taxonomy terms with depth.
10
 *
11
 * This handler is actually part of the node table and has some restrictions,
12
 * because it uses a subquery to find nodes with.
13
 *
14
 * @ingroup views_argument_handlers
15
 */
16
class views_handler_argument_term_node_tid_depth extends views_handler_argument {
17
  function option_definition() {
18
    $options = parent::option_definition();
19

    
20
    $options['depth'] = array('default' => 0);
21
    $options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
22
    $options['set_breadcrumb'] = array('default' => FALSE, 'bool' => TRUE);
23
    $options['use_taxonomy_term_path'] = array('default' => FALSE, 'bool' => TRUE);
24

    
25
    return $options;
26
  }
27

    
28
  function options_form(&$form, &$form_state) {
29
    $form['depth'] = array(
30
      '#type' => 'weight',
31
      '#title' => t('Depth'),
32
      '#default_value' => $this->options['depth'],
33
      '#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).'),
34
    );
35

    
36
    $form['break_phrase'] = array(
37
      '#type' => 'checkbox',
38
      '#title' => t('Allow multiple values'),
39
      '#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.'),
40
      '#default_value' => !empty($this->options['break_phrase']),
41
    );
42

    
43
    $form['set_breadcrumb'] = array(
44
      '#type' => 'checkbox',
45
      '#title' => t("Set the breadcrumb for the term parents"),
46
      '#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.'),
47
      '#default_value' => !empty($this->options['set_breadcrumb']),
48
    );
49

    
50
    $form['use_taxonomy_term_path'] = array(
51
      '#type' => 'checkbox',
52
      '#title' => t("Use Drupal's taxonomy term path to create breadcrumb links"),
53
      '#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.'),
54
      '#default_value' => !empty($this->options['use_taxonomy_term_path']),
55
      '#dependency' => array('edit-options-set-breadcrumb' => array(TRUE)),
56
    );
57
    parent::options_form($form, $form_state);
58
  }
59

    
60
  function set_breadcrumb(&$breadcrumb) {
61
    if (empty($this->options['set_breadcrumb']) || !is_numeric($this->argument)) {
62
      return;
63
    }
64

    
65
    return views_taxonomy_set_breadcrumb($breadcrumb, $this);
66
  }
67

    
68
  /**
69
   * Override default_actions() to remove summary actions.
70
   */
71
  function default_actions($which = NULL) {
72
    if ($which) {
73
      if (in_array($which, array('ignore', 'not found', 'empty', 'default'))) {
74
        return parent::default_actions($which);
75
      }
76
      return;
77
    }
78
    $actions = parent::default_actions();
79
    unset($actions['summary asc']);
80
    unset($actions['summary desc']);
81
    unset($actions['summary asc by count']);
82
    unset($actions['summary desc by count']);
83
    return $actions;
84
  }
85

    
86
  function query($group_by = FALSE) {
87
    $this->ensure_my_table();
88

    
89
    if (!empty($this->options['break_phrase'])) {
90
      $tids = new stdClass();
91
      $tids->value = $this->argument;
92
      $tids = views_break_phrase($this->argument, $tids);
93
      if ($tids->value == array(-1)) {
94
        return FALSE;
95
      }
96

    
97
      if (count($tids->value) > 1) {
98
        $operator = 'IN';
99
      }
100
      else {
101
        $operator = '=';
102
      }
103

    
104
      $tids = $tids->value;
105
    }
106
    else {
107
      $operator = "=";
108
      $tids = $this->argument;
109
    }
110
    // Now build the subqueries.
111
    $subquery = db_select('taxonomy_index', 'tn');
112
    $subquery->addField('tn', 'nid');
113
    $where = db_or()->condition('tn.tid', $tids, $operator);
114
    $last = "tn";
115

    
116
    if ($this->options['depth'] > 0) {
117
      $subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
118
      $last = "th";
119
      foreach (range(1, abs($this->options['depth'])) as $count) {
120
        $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
121
        $where->condition("th$count.tid", $tids, $operator);
122
        $last = "th$count";
123
      }
124
    }
125
    elseif ($this->options['depth'] < 0) {
126
      foreach (range(1, abs($this->options['depth'])) as $count) {
127
        $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
128
        $where->condition("th$count.tid", $tids, $operator);
129
        $last = "th$count";
130
      }
131
    }
132

    
133
    $subquery->condition($where);
134
    $this->query->add_where(0, "$this->table_alias.$this->real_field", $subquery, 'IN');
135
  }
136

    
137
  function title() {
138
    $term = taxonomy_term_load($this->argument);
139
    if (!empty($term)) {
140
      return check_plain($term->name);
141
    }
142
    // TODO review text
143
    return t('No name');
144
  }
145
}