Projet

Général

Profil

Paste
Télécharger (8,81 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_argument_string.inc @ 5d12d676

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

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

    
47
    return $options;
48
  }
49

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

    
56
    $form['glossary'] = array(
57
      '#type' => 'checkbox',
58
      '#title' => t('Glossary mode'),
59
      '#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.'),
60
      '#default_value' => $this->options['glossary'],
61
      '#fieldset' => 'more',
62
    );
63

    
64
    $form['limit'] = array(
65
      '#type' => 'textfield',
66
      '#title' => t('Character limit'),
67
      '#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.'),
68
      '#default_value' => $this->options['limit'],
69
      '#dependency' => array('edit-options-glossary' => array(TRUE)),
70
      '#fieldset' => 'more',
71
    );
72

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

    
88
    $form['path_case'] = array(
89
      '#type' => 'select',
90
      '#title' => t('Case in path'),
91
      '#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.'),
92
      '#options' => array(
93
        'none' => t('No transform'),
94
        'upper' => t('Upper case'),
95
        'lower' => t('Lower case'),
96
        'ucfirst' => t('Capitalize first letter'),
97
        'ucwords' => t('Capitalize each word'),
98
      ),
99
      '#default_value' => $this->options['path_case'],
100
      '#fieldset' => 'more',
101
    );
102

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

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

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

    
127
    // allow + for or, , for and
128
    $form['break_phrase'] = array(
129
      '#type' => 'checkbox',
130
      '#title' => t('Allow multiple values'),
131
      '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
132
      '#default_value' => !empty($this->options['break_phrase']),
133
      '#fieldset' => 'more',
134
    );
135
  }
136

    
137
  /**
138
   * Build the summary query based on a string.
139
   */
140
  public function summary_query() {
141
    if (empty($this->definition['many to one'])) {
142
      $this->ensure_my_table();
143
    }
144
    else {
145
      $this->table_alias = $this->helper->summary_join();
146
    }
147

    
148
    if (empty($this->options['glossary'])) {
149
      // Add the field.
150
      $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
151
      $this->query->set_count_field($this->table_alias, $this->real_field);
152
    }
153
    else {
154
      // Add the field.
155
      $formula = $this->get_formula();
156
      $this->base_alias = $this->query->add_field(NULL, $formula, $this->field . '_truncated');
157
      $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
158
    }
159

    
160
    $this->summary_name_field();
161
    return $this->summary_basics(FALSE);
162
  }
163

    
164
  /**
165
   * Get the formula for this argument.
166
   *
167
   * $this->ensure_my_table() MUST have been called prior to this.
168
   */
169
  public function get_formula() {
170
    return "SUBSTRING($this->table_alias.$this->real_field, 1, " . intval($this->options['limit']) . ")";
171
  }
172

    
173
  /**
174
   * Build the query based upon the formula
175
   */
176
  public function query($group_by = FALSE) {
177
    $argument = $this->argument;
178
    if (!empty($this->options['transform_dash'])) {
179
      $argument = strtr($argument, '-', ' ');
180
    }
181

    
182
    if (!empty($this->options['break_phrase'])) {
183
      views_break_phrase_string($argument, $this);
184
    }
185
    else {
186
      $this->value = array($argument);
187
      $this->operator = 'or';
188
    }
189

    
190
    if (!empty($this->definition['many to one'])) {
191
      if (!empty($this->options['glossary'])) {
192
        $this->helper->formula = TRUE;
193
      }
194
      $this->helper->ensure_my_table();
195
      $this->helper->add_filter();
196
      return;
197
    }
198

    
199
    $this->ensure_my_table();
200
    $formula = FALSE;
201
    if (empty($this->options['glossary'])) {
202
      $field = "$this->table_alias.$this->real_field";
203
    }
204
    else {
205
      $formula = TRUE;
206
      $field = $this->get_formula();
207
    }
208

    
209
    if (count($this->value) > 1) {
210
      $operator = 'IN';
211
      $argument = $this->value;
212
    }
213
    else {
214
      $operator = '=';
215
    }
216

    
217
    if ($formula) {
218
      $placeholder = $this->placeholder();
219
      if ($operator == 'IN') {
220
        $field .= " IN($placeholder)";
221
      }
222
      else {
223
        $field .= ' = ' . $placeholder;
224
      }
225
      $placeholders = array(
226
        $placeholder => $argument,
227
      );
228
      $this->query->add_where_expression(0, $field, $placeholders);
229
    }
230
    else {
231
      $this->query->add_where(0, $field, $argument, $operator);
232
    }
233
  }
234

    
235
  /**
236
   * {@inheritdoc}
237
   */
238
  public function summary_argument($data) {
239
    $value = $this->case_transform($data->{$this->base_alias}, $this->options['path_case']);
240
    if (!empty($this->options['transform_dash'])) {
241
      $value = strtr($value, ' ', '-');
242
    }
243
    return $value;
244
  }
245

    
246
  /**
247
   * {@inheritdoc}
248
   */
249
  public function get_sort_name() {
250
    return t('Alphabetical', array(), array('context' => 'Sort order'));
251
  }
252

    
253
  /**
254
   * {@inheritdoc}
255
   */
256
  public function title() {
257
    $this->argument = $this->case_transform($this->argument, $this->options['case']);
258
    if (!empty($this->options['transform_dash'])) {
259
      $this->argument = strtr($this->argument, '-', ' ');
260
    }
261

    
262
    if (!empty($this->options['break_phrase'])) {
263
      views_break_phrase_string($this->argument, $this);
264
    }
265
    else {
266
      $this->value = array($this->argument);
267
      $this->operator = 'or';
268
    }
269

    
270
    if (empty($this->value)) {
271
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
272
    }
273

    
274
    if ($this->value === array(-1)) {
275
      return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
276
    }
277

    
278
    return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
279
  }
280

    
281
  /**
282
   * Override for specific title lookups.
283
   */
284
  public function title_query() {
285
    return drupal_map_assoc($this->value, 'check_plain');
286
  }
287

    
288
  /**
289
   * {@inheritdoc}
290
   */
291
  public function summary_name($data) {
292
    return $this->case_transform(parent::summary_name($data), $this->options['case']);
293
  }
294

    
295
}