Project

General

Profile

Paste
Download (5.71 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_argument_many_to_one.inc @ 8be7bf84

1
<?php
2

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

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

    
26
  /**
27
   * {@inheritdoc}
28
   */
29
  public function init(&$view, &$options) {
30
    parent::init($view, $options);
31
    $this->helper = new views_many_to_one_helper($this);
32

    
33
    // Ensure defaults for these, during summaries and stuff.
34
    $this->operator = 'or';
35
    $this->value = array();
36
  }
37

    
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public function option_definition() {
42
    $options = parent::option_definition();
43

    
44
    if (!empty($this->definition['numeric'])) {
45
      $options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
46
    }
47

    
48
    $options['add_table'] = array('default' => FALSE, 'bool' => TRUE);
49
    $options['require_value'] = array('default' => FALSE, 'bool' => TRUE);
50

    
51
    if (isset($this->helper)) {
52
      $this->helper->option_definition($options);
53
    }
54
    else {
55
      $helper = new views_many_to_one_helper($this);
56
      $helper->option_definition($options);
57
    }
58

    
59
    return $options;
60
  }
61

    
62
  /**
63
   * {@inheritdoc}
64
   */
65
  public function options_form(&$form, &$form_state) {
66
    parent::options_form($form, $form_state);
67

    
68
    // allow + for or, , for and
69
    if (!empty($this->definition['numeric'])) {
70
      $form['break_phrase'] = array(
71
        '#type' => 'checkbox',
72
        '#title' => t('Allow multiple values'),
73
        '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
74
        '#default_value' => !empty($this->options['break_phrase']),
75
        '#fieldset' => 'more',
76
      );
77
    }
78

    
79
    $form['add_table'] = array(
80
      '#type' => 'checkbox',
81
      '#title' => t('Allow multiple filter values to work together'),
82
      '#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.'),
83
      '#default_value' => !empty($this->options['add_table']),
84
      '#fieldset' => 'more',
85
    );
86

    
87
    $form['require_value'] = array(
88
      '#type' => 'checkbox',
89
      '#title' => t('Do not display items with no value in summary'),
90
      '#default_value' => !empty($this->options['require_value']),
91
      '#fieldset' => 'more',
92
    );
93

    
94
    $this->helper->options_form($form, $form_state);
95
  }
96

    
97
  /**
98
   * Override ensure_my_table so we can control how this joins in.
99
   * The operator actually has influence over joining.
100
   */
101
  public function ensure_my_table() {
102
    $this->helper->ensure_my_table();
103
  }
104

    
105
  /**
106
   * {@inheritdoc}
107
   */
108
  public function query($group_by = FALSE) {
109
    $empty = FALSE;
110
    if (isset($this->definition['zero is null']) && $this->definition['zero is null']) {
111
      if (empty($this->argument)) {
112
        $empty = TRUE;
113
      }
114
    }
115
    else {
116
      if (!isset($this->argument)) {
117
        $empty = TRUE;
118
      }
119
    }
120
    if ($empty) {
121
      parent::ensure_my_table();
122
      $this->query->add_where(0, "$this->table_alias.$this->real_field", NULL, 'IS NULL');
123
      return;
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
    $this->helper->add_filter();
135
  }
136

    
137
  /**
138
   * {@inheritdoc}
139
   */
140
  public function title() {
141
    if (!$this->argument) {
142
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
143
    }
144

    
145
    if (!empty($this->options['break_phrase'])) {
146
      views_break_phrase($this->argument, $this);
147
    }
148
    else {
149
      $this->value = array($this->argument);
150
      $this->operator = 'or';
151
    }
152

    
153
    // @todo -- both of these should check definition for alternate keywords.
154

    
155
    if (empty($this->value)) {
156
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
157
    }
158

    
159
    if ($this->value === array(-1)) {
160
      return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
161
    }
162

    
163
    return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
164
  }
165

    
166
  /**
167
   * {@inheritdoc}
168
   */
169
  public function summary_query() {
170
    $field = $this->table . '.' . $this->field;
171
    $join = $this->get_join();
172

    
173
    if (!empty($this->options['require_value'])) {
174
      $join->type = 'INNER';
175
    }
176

    
177
    if (empty($this->options['add_table']) || empty($this->view->many_to_one_tables[$field])) {
178
      $this->table_alias = $this->query->ensure_table($this->table, $this->relationship, $join);
179
    }
180
    else {
181
      $this->table_alias = $this->helper->summary_join();
182
    }
183

    
184
    // Add the field.
185
    $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
186

    
187
    $this->summary_name_field();
188

    
189
    return $this->summary_basics();
190
  }
191

    
192
  /**
193
   * {@inheritdoc}
194
   */
195
  public function summary_argument($data) {
196
    $value = $data->{$this->base_alias};
197
    if (empty($value)) {
198
      $value = 0;
199
    }
200

    
201
    return $value;
202
  }
203

    
204
  /**
205
   * Override for specific title lookups.
206
   */
207
  public function title_query() {
208
    return $this->value;
209
  }
210

    
211
}