Projet

Général

Profil

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

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

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

    
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
    // Now build the subqueries.
124
    $subquery = db_select('taxonomy_index', 'tn');
125
    $subquery->addField('tn', 'nid');
126
    $where = db_or()->condition('tn.tid', $tids, $operator);
127
    $last = "tn";
128

    
129
    if ($this->options['depth'] > 0) {
130
      $subquery->leftJoin('taxonomy_term_hierarchy', 'th', "th.tid = tn.tid");
131
      $last = "th";
132
      foreach (range(1, abs($this->options['depth'])) as $count) {
133
        $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.parent = th$count.tid");
134
        $where->condition("th$count.tid", $tids, $operator);
135
        $last = "th$count";
136
      }
137
    }
138
    elseif ($this->options['depth'] < 0) {
139
      foreach (range(1, abs($this->options['depth'])) as $count) {
140
        $subquery->leftJoin('taxonomy_term_hierarchy', "th$count", "$last.tid = th$count.parent");
141
        $where->condition("th$count.tid", $tids, $operator);
142
        $last = "th$count";
143
      }
144
    }
145

    
146
    $subquery->condition($where);
147
    $this->query->add_where(0, "$this->table_alias.$this->real_field", $subquery, 'IN');
148
  }
149

    
150
  /**
151
   * {@inheritdoc}
152
   */
153
  public function title() {
154
    $term = taxonomy_term_load($this->argument);
155
    if (!empty($term)) {
156
      return check_plain($term->name);
157
    }
158
    // @todo review text.
159
    return t('No name');
160
  }
161

    
162
}