Projet

Général

Profil

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

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

1
<?php
2

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

    
8
/**
9
 * Basic argument handler to implement string arguments that may have length
10
 * limits.
11
 *
12
 * @ingroup views_argument_handlers
13
 */
14
class views_handler_argument_string extends views_handler_argument {
15
  function init(&$view, &$options) {
16
    parent::init($view, $options);
17
    if (!empty($this->definition['many to one'])) {
18
      $this->helper = new views_many_to_one_helper($this);
19

    
20
      // Ensure defaults for these, during summaries and stuff:
21
      $this->operator = 'or';
22
      $this->value = array();
23
    }
24
  }
25

    
26
  function option_definition() {
27
    $options = parent::option_definition();
28

    
29
    $options['glossary'] = array('default' => FALSE, 'bool' => TRUE);
30
    $options['limit'] = array('default' => 0);
31
    $options['case'] = array('default' => 'none');
32
    $options['path_case'] = array('default' => 'none');
33
    $options['transform_dash'] = array('default' => FALSE, 'bool' => TRUE);
34
    $options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
35

    
36
    if (!empty($this->definition['many to one'])) {
37
      $options['add_table'] = array('default' => FALSE, 'bool' => TRUE);
38
      $options['require_value'] = array('default' => FALSE, 'bool' => TRUE);
39
    }
40

    
41
    return $options;
42
  }
43

    
44
  function options_form(&$form, &$form_state) {
45
    parent::options_form($form, $form_state);
46

    
47
    $form['glossary'] = array(
48
      '#type' => 'checkbox',
49
      '#title' => t('Glossary mode'),
50
      '#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.'),
51
      '#default_value' => $this->options['glossary'],
52
      '#fieldset' => 'more',
53
    );
54

    
55
    $form['limit'] = array(
56
      '#type' => 'textfield',
57
      '#title' => t('Character limit'),
58
      '#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.'),
59
      '#default_value' => $this->options['limit'],
60
      '#dependency' => array('edit-options-glossary' => array(TRUE)),
61
      '#fieldset' => 'more',
62
    );
63

    
64
    $form['case'] = array(
65
      '#type' => 'select',
66
      '#title' => t('Case'),
67
      '#description' => t('When printing the title and summary, how to transform the case of the filter value.'),
68
      '#options' => array(
69
        'none' => t('No transform'),
70
        'upper' => t('Upper case'),
71
        'lower' => t('Lower case'),
72
        'ucfirst' => t('Capitalize first letter'),
73
        'ucwords' => t('Capitalize each word'),
74
      ),
75
      '#default_value' => $this->options['case'],
76
      '#fieldset' => 'more',
77
    );
78

    
79
    $form['path_case'] = array(
80
      '#type' => 'select',
81
      '#title' => t('Case in path'),
82
      '#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.'),
83
      '#options' => array(
84
        'none' => t('No transform'),
85
        'upper' => t('Upper case'),
86
        'lower' => t('Lower case'),
87
        'ucfirst' => t('Capitalize first letter'),
88
        'ucwords' => t('Capitalize each word'),
89
      ),
90
      '#default_value' => $this->options['path_case'],
91
      '#fieldset' => 'more',
92
    );
93

    
94
    $form['transform_dash'] = array(
95
      '#type' => 'checkbox',
96
      '#title' => t('Transform spaces to dashes in URL'),
97
      '#default_value' => $this->options['transform_dash'],
98
      '#fieldset' => 'more',
99
    );
100

    
101
    if (!empty($this->definition['many to one'])) {
102
      $form['add_table'] = array(
103
        '#type' => 'checkbox',
104
        '#title' => t('Allow multiple filter values to work together'),
105
        '#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.'),
106
        '#default_value' => !empty($this->options['add_table']),
107
        '#fieldset' => 'more',
108
      );
109

    
110
      $form['require_value'] = array(
111
        '#type' => 'checkbox',
112
        '#title' => t('Do not display items with no value in summary'),
113
        '#default_value' => !empty($this->options['require_value']),
114
        '#fieldset' => 'more',
115
      );
116
    }
117

    
118
    // allow + for or, , for and
119
    $form['break_phrase'] = array(
120
      '#type' => 'checkbox',
121
      '#title' => t('Allow multiple values'),
122
      '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
123
      '#default_value' => !empty($this->options['break_phrase']),
124
      '#fieldset' => 'more',
125
    );
126
  }
127

    
128
  /**
129
   * Build the summary query based on a string
130
   */
131
  function summary_query() {
132
    if (empty($this->definition['many to one'])) {
133
      $this->ensure_my_table();
134
    }
135
    else {
136
      $this->table_alias = $this->helper->summary_join();
137
    }
138

    
139
    if (empty($this->options['glossary'])) {
140
      // Add the field.
141
      $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
142
      $this->query->set_count_field($this->table_alias, $this->real_field);
143
    }
144
    else {
145
      // Add the field.
146
      $formula = $this->get_formula();
147
      $this->base_alias = $this->query->add_field(NULL, $formula, $this->field . '_truncated');
148
      $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
149
    }
150

    
151
    $this->summary_name_field();
152
    return $this->summary_basics(FALSE);
153
  }
154

    
155
  /**
156
   * Get the formula for this argument.
157
   *
158
   * $this->ensure_my_table() MUST have been called prior to this.
159
   */
160
  function get_formula() {
161
    return "SUBSTRING($this->table_alias.$this->real_field, 1, " . intval($this->options['limit']) . ")";
162
  }
163

    
164
  /**
165
   * Build the query based upon the formula
166
   */
167
  function query($group_by = FALSE) {
168
    $argument = $this->argument;
169
    if (!empty($this->options['transform_dash'])) {
170
      $argument = strtr($argument, '-', ' ');
171
    }
172

    
173
    if (!empty($this->options['break_phrase'])) {
174
      views_break_phrase_string($argument, $this);
175
    }
176
    else {
177
      $this->value = array($argument);
178
      $this->operator = 'or';
179
    }
180

    
181
    if (!empty($this->definition['many to one'])) {
182
      if (!empty($this->options['glossary'])) {
183
        $this->helper->formula = TRUE;
184
      }
185
      $this->helper->ensure_my_table();
186
      $this->helper->add_filter();
187
      return;
188
    }
189

    
190
    $this->ensure_my_table();
191
    $formula = FALSE;
192
    if (empty($this->options['glossary'])) {
193
      $field = "$this->table_alias.$this->real_field";
194
    }
195
    else {
196
      $formula = TRUE;
197
      $field = $this->get_formula();
198
    }
199

    
200
    if (count($this->value) > 1) {
201
      $operator = 'IN';
202
      $argument = $this->value;
203
    }
204
    else {
205
      $operator = '=';
206
    }
207

    
208
    if ($formula) {
209
      $placeholder = $this->placeholder();
210
      if ($operator == 'IN') {
211
        $field .= " IN($placeholder)";
212
      }
213
      else {
214
        $field .= ' = ' . $placeholder;
215
      }
216
      $placeholders = array(
217
        $placeholder => $argument,
218
      );
219
      $this->query->add_where_expression(0, $field, $placeholders);
220
    }
221
    else {
222
      $this->query->add_where(0, $field, $argument, $operator);
223
    }
224
  }
225

    
226
  function summary_argument($data) {
227
    $value = $this->case_transform($data->{$this->base_alias}, $this->options['path_case']);
228
    if (!empty($this->options['transform_dash'])) {
229
      $value = strtr($value, ' ', '-');
230
    }
231
    return $value;
232
  }
233

    
234
  function get_sort_name() {
235
    return t('Alphabetical', array(), array('context' => 'Sort order'));
236
  }
237

    
238
  function title() {
239
    $this->argument = $this->case_transform($this->argument, $this->options['case']);
240
    if (!empty($this->options['transform_dash'])) {
241
      $this->argument = strtr($this->argument, '-', ' ');
242
    }
243

    
244
    if (!empty($this->options['break_phrase'])) {
245
      views_break_phrase_string($this->argument, $this);
246
    }
247
    else {
248
      $this->value = array($this->argument);
249
      $this->operator = 'or';
250
    }
251

    
252
    if (empty($this->value)) {
253
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
254
    }
255

    
256
    if ($this->value === array(-1)) {
257
      return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
258
    }
259

    
260
    return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
261
  }
262

    
263
  /**
264
   * Override for specific title lookups.
265
   */
266
  function title_query() {
267
    return drupal_map_assoc($this->value, 'check_plain');
268
  }
269

    
270
  function summary_name($data) {
271
    return $this->case_transform(parent::summary_name($data), $this->options['case']);
272
  }
273

    
274
}