Projet

Général

Profil

Paste
Télécharger (6,86 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_argument_term_node_tid_depth_join.
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_join 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

    
111
    // The tids variable can be an integer or an array of integers.
112
    $tids = is_array($tids) ? $tids : array($tids);
113

    
114
    if ($this->options['depth'] > 0) {
115
      // When the depth is positive search the children.
116
      foreach ($tids as $tid) {
117
        // The term must be loaded to get vid for use in taxonomy_get_tree().
118
        if ($term = taxonomy_term_load($tid)) {
119
          // For every tid argument find all the children down to the depth set
120
          // in the options and save the tids for the condition.
121
          $tree = taxonomy_get_tree($term->vid, $term->tid, (int) $this->options['depth']);
122
          $tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $tree));
123
        }
124
      }
125
    }
126
    elseif ($this->options['depth'] < 0) {
127
      // When the depth is negative search the parents.
128
      foreach ($tids as $tid) {
129
        // For every tid argument find all the parents up to the depth set
130
        // in the options and add the tids into the array. Since there is
131
        // no taxonomy function to get all parents with a depth limit it
132
        // is done here building a multidimensional array.
133
        if ($term = taxonomy_term_load($tid)) {
134
          // A variable is needed to track the current depth level.
135
          $n = 0;
136
          // Initialise our depth based parents array with the leaf term.
137
          $parents[$n--][] = $term;
138
          while ($n >= $this->options['depth']) {
139
            // At each depth find the parents of the current terms.
140
            // It is important to note that it is possible for a term to have
141
            // multiple parents so get the parents of every parent and so on.
142
            $parents[$n] = array();
143
            foreach ($parents[$n + 1] as $term) {
144
              $parents[$n] += taxonomy_get_parents($term->tid);
145
            }
146
            // Save all the tids for the condition.
147
            $tids = array_merge($tids, array_map('_taxonomy_get_tid_from_term', $parents[$n]));
148
            $n--;
149
          }
150
        }
151
      }
152
    }
153

    
154
    // Check the size of the array and set the operator accordingly.
155
    if (count($tids) > 1) {
156
      $operator = 'IN';
157
    }
158
    else {
159
      $tids = current($tids);
160
      $operator = '=';
161
    }
162

    
163
    // Join on taxonomy index table.
164
    $join = new views_join();
165
    $join->table = 'taxonomy_index';
166
    $join->field = 'nid';
167
    $join->left_table = $this->table_alias;
168
    $join->left_field = $this->real_field;
169
    $join->type = 'INNER';
170
    $join->extra = array(
171
      array(
172
        'field' => 'tid',
173
        'value' => $tids,
174
        'operator' => $operator,
175
      )
176
    );
177
    $taxonomy_index_alias = $this->query->add_relationship('taxonomy_index', $join, 'node');
178

    
179
    // Distinct is required to prevent duplicate rows.
180
    $this->query->distinct = TRUE;
181
  }
182

    
183
  function title() {
184
    $term = taxonomy_term_load($this->argument);
185
    if (!empty($term)) {
186
      return check_plain($term->name);
187
    }
188

    
189
    return t('No name');
190
  }
191
}