Projet

Général

Profil

Paste
Télécharger (6,82 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_views / handlers / ldap_views_handler_filter.inc @ 32700c57

1
<?php
2

    
3
/**
4
 * @file
5
 * Basic textfield filter to handle string filtering commands
6
 * including equality, contains, not contains, etc.
7
 */
8

    
9
/**
10
 *
11
 */
12
class ldap_views_handler_filter extends views_handler_filter {
13
  /**
14
   * Exposed filter options.
15
   */
16
  public $always_multiple = TRUE;
17

    
18
  /**
19
   *
20
   */
21
  public function option_definition() {
22
    $options = parent::option_definition();
23
    $options['expose']['contains']['required'] = ['default' => FALSE];
24
    return $options;
25
  }
26

    
27
  /**
28
   * This kind of construct makes it relatively easy for a child class
29
   * to add or remove functionality by overriding this function and
30
   * adding/removing items from this array.
31
   */
32
  public function operators() {
33
    $operators = [
34
      '=' => [
35
        'title' => t('Is equal to'),
36
        'short' => t('='),
37
        'method' => 'op_equal',
38
        'values' => 1,
39
      ],
40
      '!=' => [
41
        'title' => t('Is not equal to'),
42
        'short' => t('!='),
43
        'method' => 'op_equal',
44
        'values' => 1,
45
      ],
46
      'contains' => [
47
        'title' => t('Contains'),
48
        'short' => t('contains'),
49
        'method' => 'op_contains',
50
        'values' => 1,
51
      ],
52
      'starts' => [
53
        'title' => t('Starts with'),
54
        'short' => t('begins'),
55
        'method' => 'op_starts',
56
        'values' => 1,
57
      ],
58
      'not_starts' => [
59
        'title' => t('Does not start with'),
60
        'short' => t('not_begins'),
61
        'method' => 'op_not_starts',
62
        'values' => 1,
63
      ],
64
      'ends' => [
65
        'title' => t('Ends with'),
66
        'short' => t('ends'),
67
        'method' => 'op_ends',
68
        'values' => 1,
69
      ],
70
      'not_ends' => [
71
        'title' => t('Does not end with'),
72
        'short' => t('not_ends'),
73
        'method' => 'op_not_ends',
74
        'values' => 1,
75
      ],
76
      'not' => [
77
        'title' => t('Does not contain'),
78
        'short' => t('!has'),
79
        'method' => 'op_not',
80
        'values' => 1,
81
      ],
82
      'greater' => [
83
        'title' => t('Greater than or equal to'),
84
        'short' => t('greater_eq'),
85
        'method' => 'op_greater_eq',
86
        'values' => 1,
87
      ],
88
      'less' => [
89
        'title' => t('Less than or equal to'),
90
        'short' => t('less_eq'),
91
        'method' => 'op_less_eq',
92
        'values' => 1,
93
      ],
94
    ];
95

    
96
    return $operators;
97
  }
98

    
99
  /**
100
   * Build strings from the operators() for 'select' options.
101
   */
102
  public function operator_options($which = 'title') {
103
    $options = [];
104
    foreach ($this->operators() as $id => $info) {
105
      $options[$id] = $info[$which];
106
    }
107

    
108
    return $options;
109
  }
110

    
111
  /**
112
   *
113
   */
114
  public function admin_summary() {
115
    if (!empty($this->options['exposed'])) {
116
      return t('exposed');
117
    }
118

    
119
    $options = $this->operator_options('short');
120
    $output = '';
121
    if (!empty($options[$this->operator])) {
122
      $output = check_plain($options[$this->operator]);
123
    }
124
    if (in_array($this->operator, $this->operator_values(1))) {
125
      $output .= ' ' . check_plain($this->value);
126
    }
127
    return $output;
128
  }
129

    
130
  /**
131
   *
132
   */
133
  public function operator_values($values = 1) {
134
    $options = [];
135
    foreach ($this->operators() as $id => $info) {
136
      if (isset($info['values']) && $info['values'] == $values) {
137
        $options[] = $id;
138
      }
139
    }
140

    
141
    return $options;
142
  }
143

    
144
  /**
145
   * Provide a simple textfield for equality.
146
   */
147
  public function value_form(&$form, &$form_state) {
148
    // We have to make some choices when creating this as an exposed
149
    // filter form. For example, if the operator is locked and thus
150
    // not rendered, we can't render dependencies; instead we only
151
    // render the form items we need.
152
    $which = 'all';
153
    if (!empty($form['operator'])) {
154
      $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
155
    }
156
    if (!empty($form_state['exposed'])) {
157
      $identifier = $this->options['expose']['identifier'];
158

    
159
      if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
160
        // Exposed and locked.
161
        $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
162
      }
163
      else {
164
        $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
165
      }
166
    }
167

    
168
    if ($which == 'all' || $which == 'value') {
169
      $form['value'] = [
170
        '#type' => 'textfield',
171
        '#title' => t('Value'),
172
        '#size' => 30,
173
        '#default_value' => $this->value,
174
      ];
175
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
176
        $form_state['input'][$identifier] = $this->value;
177
      }
178

    
179
      if ($which == 'all') {
180
        $form['value'] += [
181
          '#dependency' => [$source => $this->operator_values(1)],
182
        ];
183
      }
184
    }
185

    
186
    if (!isset($form['value'])) {
187
      // Ensure there is something in the 'value'.
188
      $form['value'] = [
189
        '#type' => 'value',
190
        '#value' => NULL,
191
      ];
192
    }
193
  }
194

    
195
  /**
196
   * Add this filter to the query.
197
   *
198
   * Due to the nature of fapi, the value and the operator have an unintended
199
   * level of indirection. You will find them in $this->operator
200
   * and $this->value respectively.
201
   */
202
  public function query() {
203
    $this->ensure_my_table();
204
    $field = $this->real_field;
205

    
206
    $info = $this->operators();
207
    if (!empty($info[$this->operator]['method'])) {
208
      $this->{$info[$this->operator]['method']}($field);
209
    }
210
  }
211

    
212
  /**
213
   *
214
   */
215
  public function op_equal($field) {
216
    $this->query->add_where($this->options['group'], $field, $this->value, $this->operator);
217
  }
218

    
219
  /**
220
   *
221
   */
222
  public function op_contains($field) {
223
    $this->query->add_where($this->options['group'], $field, "*$this->value*", '=');
224
  }
225

    
226
  /**
227
   *
228
   */
229
  public function op_starts($field) {
230
    $this->query->add_where($this->options['group'], $field, "$this->value*", '=');
231
  }
232

    
233
  /**
234
   *
235
   */
236
  public function op_not_starts($field) {
237
    $this->query->add_where($this->options['group'], $field, "$this->value*", '!=');
238
  }
239

    
240
  /**
241
   *
242
   */
243
  public function op_ends($field) {
244
    $this->query->add_where($this->options['group'], $field, "*$this->value", '=');
245
  }
246

    
247
  /**
248
   *
249
   */
250
  public function op_not_ends($field) {
251
    $this->query->add_where($this->options['group'], $field, "*$this->value", '!=');
252
  }
253

    
254
  /**
255
   *
256
   */
257
  public function op_not($field) {
258
    $this->query->add_where($this->options['group'], $field, "*$this->value*", '!=');
259
  }
260

    
261
  /**
262
   *
263
   */
264
  public function op_greater_eq($field) {
265
    $this->query->add_where($this->options['group'], $field, $this->value, '>=');
266
  }
267

    
268
  /**
269
   *
270
   */
271
  public function op_less_eq($field) {
272
    $this->query->add_where($this->options['group'], $field, $this->value, '<=');
273
  }
274

    
275
  /**
276
   *
277
   */
278
  public function op_exists($field) {
279
    $this->query->add_where($this->options['group'], $field, '*', $this->operator == 'exists' ? '=' : '!=');
280
  }
281

    
282
}