Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_argument_many_to_one.inc @ 7547bb19

1
<?php
2

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

    
8
/**
9
 * An argument handler for use in fields that have a many to one relationship
10
 * with the table(s) to the left. This adds a bunch of options that are
11
 * reasonably common with this type of relationship.
12
 * Definition terms:
13
 * - numeric: If true, the field will be considered numeric. Probably should
14
 *   always be set TRUE as views_handler_argument_string has many to one
15
 *   capabilities.
16
 * - zero is null: If true, a 0 will be handled as empty, so for example
17
 *   a default argument can be provided or a summary can be shown.
18
 *
19
 * @ingroup views_argument_handlers
20
 */
21
class views_handler_argument_many_to_one extends views_handler_argument {
22
  function init(&$view, &$options) {
23
    parent::init($view, $options);
24
    $this->helper = new views_many_to_one_helper($this);
25

    
26
    // Ensure defaults for these, during summaries and stuff:
27
    $this->operator = 'or';
28
    $this->value = array();
29
  }
30

    
31
  function option_definition() {
32
    $options = parent::option_definition();
33

    
34
    if (!empty($this->definition['numeric'])) {
35
      $options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
36
    }
37

    
38
    $options['add_table'] = array('default' => FALSE, 'bool' => TRUE);
39
    $options['require_value'] = array('default' => FALSE, 'bool' => TRUE);
40

    
41
    if (isset($this->helper)) {
42
      $this->helper->option_definition($options);
43
    }
44
    else {
45
      $helper = new views_many_to_one_helper($this);
46
      $helper->option_definition($options);
47
    }
48

    
49
    return $options;
50
  }
51

    
52
  function options_form(&$form, &$form_state) {
53
    parent::options_form($form, $form_state);
54

    
55
    // allow + for or, , for and
56
    if (!empty($this->definition['numeric'])) {
57
      $form['break_phrase'] = array(
58
        '#type' => 'checkbox',
59
        '#title' => t('Allow multiple values'),
60
        '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
61
        '#default_value' => !empty($this->options['break_phrase']),
62
        '#fieldset' => 'more',
63
      );
64
    }
65

    
66
    $form['add_table'] = array(
67
      '#type' => 'checkbox',
68
      '#title' => t('Allow multiple filter values to work together'),
69
      '#description' => t('If selected, multiple instances of this filter can work together, as though multiple values were supplied to the same filter. This setting is not compatible with the "Reduce duplicates" setting.'),
70
      '#default_value' => !empty($this->options['add_table']),
71
      '#fieldset' => 'more',
72
    );
73

    
74
    $form['require_value'] = array(
75
      '#type' => 'checkbox',
76
      '#title' => t('Do not display items with no value in summary'),
77
      '#default_value' => !empty($this->options['require_value']),
78
      '#fieldset' => 'more',
79
    );
80

    
81
    $this->helper->options_form($form, $form_state);
82
  }
83

    
84
  /**
85
   * Override ensure_my_table so we can control how this joins in.
86
   * The operator actually has influence over joining.
87
   */
88
  function ensure_my_table() {
89
    $this->helper->ensure_my_table();
90
  }
91

    
92
  function query($group_by = FALSE) {
93
    $empty = FALSE;
94
    if (isset($this->definition['zero is null']) && $this->definition['zero is null']) {
95
      if (empty($this->argument)) {
96
        $empty = TRUE;
97
      }
98
    }
99
    else {
100
      if (!isset($this->argument)) {
101
        $empty = TRUE;
102
      }
103
    }
104
    if ($empty) {
105
      parent::ensure_my_table();
106
      $this->query->add_where(0, "$this->table_alias.$this->real_field", NULL, 'IS NULL');
107
      return;
108
    }
109

    
110
    if (!empty($this->options['break_phrase'])) {
111
      views_break_phrase($this->argument, $this);
112
    }
113
    else {
114
      $this->value = array($this->argument);
115
      $this->operator = 'or';
116
    }
117

    
118
    $this->helper->add_filter();
119
  }
120

    
121
  function title() {
122
    if (!$this->argument) {
123
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
124
    }
125

    
126
    if (!empty($this->options['break_phrase'])) {
127
      views_break_phrase($this->argument, $this);
128
    }
129
    else {
130
      $this->value = array($this->argument);
131
      $this->operator = 'or';
132
    }
133

    
134
    // @todo -- both of these should check definition for alternate keywords.
135

    
136
    if (empty($this->value)) {
137
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
138
    }
139

    
140
    if ($this->value === array(-1)) {
141
      return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
142
    }
143

    
144
    return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
145
  }
146

    
147
  function summary_query() {
148
    $field = $this->table . '.' . $this->field;
149
    $join = $this->get_join();
150

    
151
    if (!empty($this->options['require_value'])) {
152
      $join->type = 'INNER';
153
    }
154

    
155
    if (empty($this->options['add_table']) || empty($this->view->many_to_one_tables[$field])) {
156
      $this->table_alias = $this->query->ensure_table($this->table, $this->relationship, $join);
157
    }
158
    else {
159
      $this->table_alias = $this->helper->summary_join();
160
    }
161

    
162
    // Add the field.
163
    $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
164

    
165
    $this->summary_name_field();
166

    
167
    return $this->summary_basics();
168
  }
169

    
170
  function summary_argument($data) {
171
    $value = $data->{$this->base_alias};
172
    if (empty($value)) {
173
      $value = 0;
174
    }
175

    
176
    return $value;
177
  }
178

    
179
  /**
180
   * Override for specific title lookups.
181
   */
182
  function title_query() {
183
    return $this->value;
184
  }
185
}