Project

General

Profile

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

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

1
<?php
2

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

    
8
/**
9
 * Argument handler to implement string arguments that may have length limits.
10
 *
11
 * @ingroup views_argument_handlers
12
 */
13
class views_handler_argument_string extends views_handler_argument {
14

    
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public function init(&$view, &$options) {
19
    parent::init($view, $options);
20
    if (!empty($this->definition['many to one'])) {
21
      $this->helper = new views_many_to_one_helper($this);
22

    
23
      // Ensure defaults for these, during summaries and stuff.
24
      $this->operator = 'or';
25
      $this->value = array();
26
    }
27
  }
28

    
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function option_definition() {
33
    $options = parent::option_definition();
34

    
35
    $options['glossary'] = array('default' => FALSE, 'bool' => TRUE);
36
    $options['limit'] = array('default' => 0);
37
    $options['case'] = array('default' => 'none');
38
    $options['path_case'] = array('default' => 'none');
39
    $options['transform_dash'] = array('default' => FALSE, 'bool' => TRUE);
40
    $options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
41
    $options['not'] = array('default' => FALSE, 'bool' => TRUE);
42

    
43
    if (!empty($this->definition['many to one'])) {
44
      $options['add_table'] = array('default' => FALSE, 'bool' => TRUE);
45
      $options['require_value'] = array('default' => FALSE, 'bool' => TRUE);
46
    }
47

    
48
    return $options;
49
  }
50

    
51
  /**
52
   * {@inheritdoc}
53
   */
54
  public function options_form(&$form, &$form_state) {
55
    parent::options_form($form, $form_state);
56

    
57
    $form['glossary'] = array(
58
      '#type' => 'checkbox',
59
      '#title' => t('Glossary mode'),
60
      '#description' => t('Glossary mode applies a limit to the number of characters used in the filter value, which allows the summary view to act as a glossary.'),
61
      '#default_value' => $this->options['glossary'],
62
      '#fieldset' => 'more',
63
    );
64

    
65
    $form['limit'] = array(
66
      '#type' => 'textfield',
67
      '#title' => t('Character limit'),
68
      '#description' => t('How many characters of the filter value to filter against. If set to 1, all fields starting with the first letter in the filter value would be matched.'),
69
      '#default_value' => $this->options['limit'],
70
      '#dependency' => array('edit-options-glossary' => array(TRUE)),
71
      '#fieldset' => 'more',
72
    );
73

    
74
    $form['case'] = array(
75
      '#type' => 'select',
76
      '#title' => t('Case'),
77
      '#description' => t('When printing the title and summary, how to transform the case of the filter value.'),
78
      '#options' => array(
79
        'none' => t('No transform'),
80
        'upper' => t('Upper case'),
81
        'lower' => t('Lower case'),
82
        'ucfirst' => t('Capitalize first letter'),
83
        'ucwords' => t('Capitalize each word'),
84
      ),
85
      '#default_value' => $this->options['case'],
86
      '#fieldset' => 'more',
87
    );
88

    
89
    $form['path_case'] = array(
90
      '#type' => 'select',
91
      '#title' => t('Case in path'),
92
      '#description' => t('When printing url paths, how to transform the case of the filter value. Do not use this unless with Postgres as it uses case sensitive comparisons.'),
93
      '#options' => array(
94
        'none' => t('No transform'),
95
        'upper' => t('Upper case'),
96
        'lower' => t('Lower case'),
97
        'ucfirst' => t('Capitalize first letter'),
98
        'ucwords' => t('Capitalize each word'),
99
      ),
100
      '#default_value' => $this->options['path_case'],
101
      '#fieldset' => 'more',
102
    );
103

    
104
    $form['transform_dash'] = array(
105
      '#type' => 'checkbox',
106
      '#title' => t('Transform spaces to dashes in URL'),
107
      '#default_value' => $this->options['transform_dash'],
108
      '#fieldset' => 'more',
109
    );
110

    
111
    if (!empty($this->definition['many to one'])) {
112
      $form['add_table'] = array(
113
        '#type' => 'checkbox',
114
        '#title' => t('Allow multiple filter values to work together'),
115
        '#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.'),
116
        '#default_value' => !empty($this->options['add_table']),
117
        '#fieldset' => 'more',
118
      );
119

    
120
      $form['require_value'] = array(
121
        '#type' => 'checkbox',
122
        '#title' => t('Do not display items with no value in summary'),
123
        '#default_value' => !empty($this->options['require_value']),
124
        '#fieldset' => 'more',
125
      );
126
    }
127

    
128
    // allow + for or, , for and
129
    $form['break_phrase'] = array(
130
      '#type' => 'checkbox',
131
      '#title' => t('Allow multiple values'),
132
      '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
133
      '#default_value' => !empty($this->options['break_phrase']),
134
      '#fieldset' => 'more',
135
    );
136
    $form['not'] = array(
137
      '#type' => 'checkbox',
138
      '#title' => t('Exclude'),
139
      '#description' => t('If selected, the numbers entered for the filter will be excluded rather than limiting the view.'),
140
      '#default_value' => !empty($this->options['not']),
141
      '#fieldset' => 'more',
142
    );
143
  }
144

    
145
  /**
146
   * Build the summary query based on a string.
147
   */
148
  public function summary_query() {
149
    if (empty($this->definition['many to one'])) {
150
      $this->ensure_my_table();
151
    }
152
    else {
153
      $this->table_alias = $this->helper->summary_join();
154
    }
155

    
156
    if (empty($this->options['glossary'])) {
157
      // Add the field.
158
      $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
159
      $this->query->set_count_field($this->table_alias, $this->real_field);
160
    }
161
    else {
162
      // Add the field.
163
      $formula = $this->get_formula();
164
      $this->base_alias = $this->query->add_field(NULL, $formula, $this->field . '_truncated');
165
      $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
166
    }
167

    
168
    $this->summary_name_field();
169
    return $this->summary_basics(FALSE);
170
  }
171

    
172
  /**
173
   * Get the formula for this argument.
174
   *
175
   * $this->ensure_my_table() MUST have been called prior to this.
176
   */
177
  public function get_formula() {
178
    return "SUBSTRING($this->table_alias.$this->real_field, 1, " . intval($this->options['limit']) . ")";
179
  }
180

    
181
  /**
182
   * Build the query based upon the formula
183
   */
184
  public function query($group_by = FALSE) {
185
    $argument = $this->argument;
186
    if (!empty($this->options['transform_dash'])) {
187
      $argument = strtr($argument, '-', ' ');
188
    }
189

    
190
    if (!empty($this->options['break_phrase'])) {
191
      views_break_phrase_string($argument, $this);
192
    }
193
    else {
194
      $this->value = array($argument);
195
      $this->operator = 'or';
196
    }
197

    
198
    if (!empty($this->definition['many to one'])) {
199
      if (!empty($this->options['glossary'])) {
200
        $this->helper->formula = TRUE;
201
      }
202
      $this->helper->ensure_my_table();
203
      $this->helper->add_filter();
204
      return;
205
    }
206

    
207
    $this->ensure_my_table();
208
    $formula = FALSE;
209
    if (empty($this->options['glossary'])) {
210
      $field = "$this->table_alias.$this->real_field";
211
    }
212
    else {
213
      $formula = TRUE;
214
      $field = $this->get_formula();
215
    }
216

    
217
    if (count($this->value) > 1) {
218
      $operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
219
      $argument = $this->value;
220
    }
221
    else {
222
      $operator = empty($this->options['not']) ? '=' : '!=';
223
    }
224

    
225
    if ($formula) {
226
      $placeholder = $this->placeholder();
227
      if (count($this->value) > 1) {
228
        $placeholder = "($placeholder)";
229
      }
230
      $field .= " $operator $placeholder";
231
      $placeholders = array(
232
        $placeholder => $argument,
233
      );
234
      $this->query->add_where_expression(0, $field, $placeholders);
235
    }
236
    else {
237
      $this->query->add_where(0, $field, $argument, $operator);
238
    }
239
  }
240

    
241
  /**
242
   * {@inheritdoc}
243
   */
244
  public function summary_argument($data) {
245
    $value = $this->case_transform($data->{$this->base_alias}, $this->options['path_case']);
246
    if (!empty($this->options['transform_dash'])) {
247
      $value = strtr($value, ' ', '-');
248
    }
249
    return $value;
250
  }
251

    
252
  /**
253
   * {@inheritdoc}
254
   */
255
  public function get_sort_name() {
256
    return t('Alphabetical', array(), array('context' => 'Sort order'));
257
  }
258

    
259
  /**
260
   * {@inheritdoc}
261
   */
262
  public function title() {
263
    $this->argument = $this->case_transform($this->argument, $this->options['case']);
264
    if (!empty($this->options['transform_dash'])) {
265
      $this->argument = strtr($this->argument, '-', ' ');
266
    }
267

    
268
    if (!empty($this->options['break_phrase'])) {
269
      views_break_phrase_string($this->argument, $this);
270
    }
271
    else {
272
      $this->value = array($this->argument);
273
      $this->operator = 'or';
274
    }
275

    
276
    if (empty($this->value)) {
277
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
278
    }
279

    
280
    if ($this->value === array(-1)) {
281
      return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
282
    }
283

    
284
    return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
285
  }
286

    
287
  /**
288
   * Override for specific title lookups.
289
   */
290
  public function title_query() {
291
    return drupal_map_assoc($this->value, 'check_plain');
292
  }
293

    
294
  /**
295
   * {@inheritdoc}
296
   */
297
  public function summary_name($data) {
298
    return $this->case_transform(parent::summary_name($data), $this->options['case']);
299
  }
300

    
301
}