Projet

Général

Profil

Paste
Télécharger (7,04 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / taxonomy / views_handler_argument_term_node_tid_depth_join.inc @ 4003efde

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

    
18
  /**
19
   * {@inheritdoc}
20
   */
21
  public function option_definition() {
22
    $options = parent::option_definition();
23

    
24
    $options['depth'] = array('default' => 0);
25
    $options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
26
    $options['set_breadcrumb'] = array('default' => FALSE, 'bool' => TRUE);
27
    $options['use_taxonomy_term_path'] = array('default' => FALSE, 'bool' => TRUE);
28

    
29
    return $options;
30
  }
31

    
32
  /**
33
   * {@inheritdoc}
34
   */
35
  public function options_form(&$form, &$form_state) {
36
    $form['depth'] = array(
37
      '#type' => 'weight',
38
      '#title' => t('Depth'),
39
      '#default_value' => $this->options['depth'],
40
      '#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).'),
41
    );
42

    
43
    $form['break_phrase'] = array(
44
      '#type' => 'checkbox',
45
      '#title' => t('Allow multiple values'),
46
      '#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.'),
47
      '#default_value' => !empty($this->options['break_phrase']),
48
    );
49

    
50
    $form['set_breadcrumb'] = array(
51
      '#type' => 'checkbox',
52
      '#title' => t("Set the breadcrumb for the term parents"),
53
      '#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.'),
54
      '#default_value' => !empty($this->options['set_breadcrumb']),
55
    );
56

    
57
    $form['use_taxonomy_term_path'] = array(
58
      '#type' => 'checkbox',
59
      '#title' => t("Use Drupal's taxonomy term path to create breadcrumb links"),
60
      '#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.'),
61
      '#default_value' => !empty($this->options['use_taxonomy_term_path']),
62
      '#dependency' => array('edit-options-set-breadcrumb' => array(TRUE)),
63
    );
64
    parent::options_form($form, $form_state);
65
  }
66

    
67
  /**
68
   * {@inheritdoc}
69
   */
70
  public function set_breadcrumb(&$breadcrumb) {
71
    if (empty($this->options['set_breadcrumb']) || !is_numeric($this->argument)) {
72
      return;
73
    }
74

    
75
    return views_taxonomy_set_breadcrumb($breadcrumb, $this);
76
  }
77

    
78
  /**
79
   * Override default_actions() to remove summary actions.
80
   */
81
  public function default_actions($which = NULL) {
82
    if ($which) {
83
      if (in_array($which, array('ignore', 'not found', 'empty', 'default'))) {
84
        return parent::default_actions($which);
85
      }
86
      return;
87
    }
88
    $actions = parent::default_actions();
89
    unset($actions['summary asc']);
90
    unset($actions['summary desc']);
91
    unset($actions['summary asc by count']);
92
    unset($actions['summary desc by count']);
93
    return $actions;
94
  }
95

    
96
  /**
97
   * {@inheritdoc}
98
   */
99
  public function query($group_by = FALSE) {
100
    $this->ensure_my_table();
101

    
102
    if (!empty($this->options['break_phrase'])) {
103
      $tids = new stdClass();
104
      $tids->value = $this->argument;
105
      $tids = views_break_phrase($this->argument, $tids);
106
      if ($tids->value == array(-1)) {
107
        return FALSE;
108
      }
109

    
110
      if (count($tids->value) > 1) {
111
        $operator = 'IN';
112
      }
113
      else {
114
        $operator = '=';
115
      }
116

    
117
      $tids = $tids->value;
118
    }
119
    else {
120
      $operator = "=";
121
      $tids = $this->argument;
122
    }
123

    
124
    // The tids variable can be an integer or an array of integers.
125
    $tids = is_array($tids) ? $tids : array($tids);
126

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

    
167
    // Check the size of the array and set the operator accordingly.
168
    if (count($tids) > 1) {
169
      $operator = 'IN';
170
    }
171
    else {
172
      $tids = current($tids);
173
      $operator = '=';
174
    }
175

    
176
    // Join on taxonomy index table.
177
    $join = new views_join();
178
    $join->table = 'taxonomy_index';
179
    $join->field = 'nid';
180
    $join->left_table = $this->table_alias;
181
    $join->left_field = $this->real_field;
182
    $join->type = 'INNER';
183
    $join->extra = array(
184
      array(
185
        'field' => 'tid',
186
        'value' => $tids,
187
        'operator' => $operator,
188
      ),
189
    );
190
    $taxonomy_index_alias = $this->query->add_relationship('taxonomy_index', $join, 'node');
191

    
192
    // Distinct is required to prevent duplicate rows.
193
    $this->query->distinct = TRUE;
194
  }
195

    
196
  /**
197
   *
198
   */
199
  public function title() {
200
    $term = taxonomy_term_load($this->argument);
201
    if (!empty($term)) {
202
      return check_plain($term->name);
203
    }
204

    
205
    return t('No name');
206
  }
207

    
208
}