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 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Basic textfield filter to handle string filtering commands
6
 * including equality, contains, not contains, etc.
7
 */
8 bc175c27 Assos Assos
9 32700c57 Assos Assos
/**
10
 *
11
 */
12 85ad3d82 Assos Assos
class ldap_views_handler_filter extends views_handler_filter {
13 32700c57 Assos Assos
  /**
14
   * Exposed filter options.
15
   */
16
  public $always_multiple = TRUE;
17 85ad3d82 Assos Assos
18 32700c57 Assos Assos
  /**
19
   *
20
   */
21
  public function option_definition() {
22 85ad3d82 Assos Assos
    $options = parent::option_definition();
23 32700c57 Assos Assos
    $options['expose']['contains']['required'] = ['default' => FALSE];
24 85ad3d82 Assos Assos
    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 32700c57 Assos Assos
  public function operators() {
33
    $operators = [
34
      '=' => [
35 85ad3d82 Assos Assos
        'title' => t('Is equal to'),
36
        'short' => t('='),
37
        'method' => 'op_equal',
38
        'values' => 1,
39 32700c57 Assos Assos
      ],
40
      '!=' => [
41 85ad3d82 Assos Assos
        'title' => t('Is not equal to'),
42
        'short' => t('!='),
43
        'method' => 'op_equal',
44
        'values' => 1,
45 32700c57 Assos Assos
      ],
46
      'contains' => [
47 85ad3d82 Assos Assos
        'title' => t('Contains'),
48
        'short' => t('contains'),
49
        'method' => 'op_contains',
50
        'values' => 1,
51 32700c57 Assos Assos
      ],
52
      'starts' => [
53 85ad3d82 Assos Assos
        'title' => t('Starts with'),
54
        'short' => t('begins'),
55
        'method' => 'op_starts',
56
        'values' => 1,
57 32700c57 Assos Assos
      ],
58
      'not_starts' => [
59 85ad3d82 Assos Assos
        'title' => t('Does not start with'),
60
        'short' => t('not_begins'),
61
        'method' => 'op_not_starts',
62
        'values' => 1,
63 32700c57 Assos Assos
      ],
64
      'ends' => [
65 85ad3d82 Assos Assos
        'title' => t('Ends with'),
66
        'short' => t('ends'),
67
        'method' => 'op_ends',
68
        'values' => 1,
69 32700c57 Assos Assos
      ],
70
      'not_ends' => [
71 85ad3d82 Assos Assos
        'title' => t('Does not end with'),
72
        'short' => t('not_ends'),
73
        'method' => 'op_not_ends',
74
        'values' => 1,
75 32700c57 Assos Assos
      ],
76
      'not' => [
77 85ad3d82 Assos Assos
        'title' => t('Does not contain'),
78
        'short' => t('!has'),
79
        'method' => 'op_not',
80
        'values' => 1,
81 32700c57 Assos Assos
      ],
82
      'greater' => [
83 85ad3d82 Assos Assos
        'title' => t('Greater than or equal to'),
84
        'short' => t('greater_eq'),
85
        'method' => 'op_greater_eq',
86
        'values' => 1,
87 32700c57 Assos Assos
      ],
88
      'less' => [
89 85ad3d82 Assos Assos
        'title' => t('Less than or equal to'),
90
        'short' => t('less_eq'),
91
        'method' => 'op_less_eq',
92
        'values' => 1,
93 32700c57 Assos Assos
      ],
94
    ];
95 85ad3d82 Assos Assos
96
    return $operators;
97
  }
98
99
  /**
100 32700c57 Assos Assos
   * Build strings from the operators() for 'select' options.
101 85ad3d82 Assos Assos
   */
102 32700c57 Assos Assos
  public function operator_options($which = 'title') {
103
    $options = [];
104 85ad3d82 Assos Assos
    foreach ($this->operators() as $id => $info) {
105
      $options[$id] = $info[$which];
106
    }
107
108
    return $options;
109
  }
110
111 32700c57 Assos Assos
  /**
112
   *
113
   */
114
  public function admin_summary() {
115 85ad3d82 Assos Assos
    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 32700c57 Assos Assos
  /**
131
   *
132
   */
133
  public function operator_values($values = 1) {
134
    $options = [];
135 85ad3d82 Assos Assos
    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 32700c57 Assos Assos
   * Provide a simple textfield for equality.
146 85ad3d82 Assos Assos
   */
147 32700c57 Assos Assos
  public function value_form(&$form, &$form_state) {
148 85ad3d82 Assos Assos
    // 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 32700c57 Assos Assos
        // Exposed and locked.
161 85ad3d82 Assos Assos
        $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 32700c57 Assos Assos
      $form['value'] = [
170 85ad3d82 Assos Assos
        '#type' => 'textfield',
171
        '#title' => t('Value'),
172
        '#size' => 30,
173
        '#default_value' => $this->value,
174 32700c57 Assos Assos
      ];
175 85ad3d82 Assos Assos
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
176
        $form_state['input'][$identifier] = $this->value;
177
      }
178
179
      if ($which == 'all') {
180 32700c57 Assos Assos
        $form['value'] += [
181
          '#dependency' => [$source => $this->operator_values(1)],
182
        ];
183 85ad3d82 Assos Assos
      }
184
    }
185
186
    if (!isset($form['value'])) {
187
      // Ensure there is something in the 'value'.
188 32700c57 Assos Assos
      $form['value'] = [
189 85ad3d82 Assos Assos
        '#type' => 'value',
190 32700c57 Assos Assos
        '#value' => NULL,
191
      ];
192 85ad3d82 Assos Assos
    }
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 32700c57 Assos Assos
  public function query() {
203 85ad3d82 Assos Assos
    $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 32700c57 Assos Assos
  /**
213
   *
214
   */
215
  public function op_equal($field) {
216 85ad3d82 Assos Assos
    $this->query->add_where($this->options['group'], $field, $this->value, $this->operator);
217
  }
218
219 32700c57 Assos Assos
  /**
220
   *
221
   */
222
  public function op_contains($field) {
223 85ad3d82 Assos Assos
    $this->query->add_where($this->options['group'], $field, "*$this->value*", '=');
224
  }
225
226 32700c57 Assos Assos
  /**
227
   *
228
   */
229
  public function op_starts($field) {
230 85ad3d82 Assos Assos
    $this->query->add_where($this->options['group'], $field, "$this->value*", '=');
231
  }
232
233 32700c57 Assos Assos
  /**
234
   *
235
   */
236
  public function op_not_starts($field) {
237 85ad3d82 Assos Assos
    $this->query->add_where($this->options['group'], $field, "$this->value*", '!=');
238
  }
239
240 32700c57 Assos Assos
  /**
241
   *
242
   */
243
  public function op_ends($field) {
244 85ad3d82 Assos Assos
    $this->query->add_where($this->options['group'], $field, "*$this->value", '=');
245
  }
246
247 32700c57 Assos Assos
  /**
248
   *
249
   */
250
  public function op_not_ends($field) {
251 85ad3d82 Assos Assos
    $this->query->add_where($this->options['group'], $field, "*$this->value", '!=');
252
  }
253
254 32700c57 Assos Assos
  /**
255
   *
256
   */
257
  public function op_not($field) {
258 85ad3d82 Assos Assos
    $this->query->add_where($this->options['group'], $field, "*$this->value*", '!=');
259
  }
260
261 32700c57 Assos Assos
  /**
262
   *
263
   */
264
  public function op_greater_eq($field) {
265 85ad3d82 Assos Assos
    $this->query->add_where($this->options['group'], $field, $this->value, '>=');
266
  }
267
268 32700c57 Assos Assos
  /**
269
   *
270
   */
271
  public function op_less_eq($field) {
272 85ad3d82 Assos Assos
    $this->query->add_where($this->options['group'], $field, $this->value, '<=');
273
  }
274
275 32700c57 Assos Assos
  /**
276
   *
277
   */
278
  public function op_exists($field) {
279 85ad3d82 Assos Assos
    $this->query->add_where($this->options['group'], $field, '*', $this->operator == 'exists' ? '=' : '!=');
280
  }
281
282
}