1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Basic textfield filter to handle string filtering commands
|
6
|
* including equality, contains, not contains, etc.
|
7
|
*/
|
8
|
class ldap_views_handler_filter extends views_handler_filter {
|
9
|
// exposed filter options
|
10
|
var $always_multiple = TRUE;
|
11
|
|
12
|
function option_definition() {
|
13
|
$options = parent::option_definition();
|
14
|
$options['expose']['contains']['required'] = array('default' => FALSE);
|
15
|
return $options;
|
16
|
}
|
17
|
|
18
|
/**
|
19
|
* This kind of construct makes it relatively easy for a child class
|
20
|
* to add or remove functionality by overriding this function and
|
21
|
* adding/removing items from this array.
|
22
|
*/
|
23
|
function operators() {
|
24
|
$operators = array(
|
25
|
'=' => array(
|
26
|
'title' => t('Is equal to'),
|
27
|
'short' => t('='),
|
28
|
'method' => 'op_equal',
|
29
|
'values' => 1,
|
30
|
),
|
31
|
'!=' => array(
|
32
|
'title' => t('Is not equal to'),
|
33
|
'short' => t('!='),
|
34
|
'method' => 'op_equal',
|
35
|
'values' => 1,
|
36
|
),
|
37
|
'contains' => array(
|
38
|
'title' => t('Contains'),
|
39
|
'short' => t('contains'),
|
40
|
'method' => 'op_contains',
|
41
|
'values' => 1,
|
42
|
),
|
43
|
'starts' => array(
|
44
|
'title' => t('Starts with'),
|
45
|
'short' => t('begins'),
|
46
|
'method' => 'op_starts',
|
47
|
'values' => 1,
|
48
|
),
|
49
|
'not_starts' => array(
|
50
|
'title' => t('Does not start with'),
|
51
|
'short' => t('not_begins'),
|
52
|
'method' => 'op_not_starts',
|
53
|
'values' => 1,
|
54
|
),
|
55
|
'ends' => array(
|
56
|
'title' => t('Ends with'),
|
57
|
'short' => t('ends'),
|
58
|
'method' => 'op_ends',
|
59
|
'values' => 1,
|
60
|
),
|
61
|
'not_ends' => array(
|
62
|
'title' => t('Does not end with'),
|
63
|
'short' => t('not_ends'),
|
64
|
'method' => 'op_not_ends',
|
65
|
'values' => 1,
|
66
|
),
|
67
|
'not' => array(
|
68
|
'title' => t('Does not contain'),
|
69
|
'short' => t('!has'),
|
70
|
'method' => 'op_not',
|
71
|
'values' => 1,
|
72
|
),
|
73
|
'greater' => array(
|
74
|
'title' => t('Greater than or equal to'),
|
75
|
'short' => t('greater_eq'),
|
76
|
'method' => 'op_greater_eq',
|
77
|
'values' => 1,
|
78
|
),
|
79
|
'less' => array(
|
80
|
'title' => t('Less than or equal to'),
|
81
|
'short' => t('less_eq'),
|
82
|
'method' => 'op_less_eq',
|
83
|
'values' => 1,
|
84
|
),
|
85
|
);
|
86
|
|
87
|
return $operators;
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Build strings from the operators() for 'select' options
|
92
|
*/
|
93
|
function operator_options($which = 'title') {
|
94
|
$options = array();
|
95
|
foreach ($this->operators() as $id => $info) {
|
96
|
$options[$id] = $info[$which];
|
97
|
}
|
98
|
|
99
|
return $options;
|
100
|
}
|
101
|
|
102
|
function admin_summary() {
|
103
|
if (!empty($this->options['exposed'])) {
|
104
|
return t('exposed');
|
105
|
}
|
106
|
|
107
|
$options = $this->operator_options('short');
|
108
|
$output = '';
|
109
|
if (!empty($options[$this->operator])) {
|
110
|
$output = check_plain($options[$this->operator]);
|
111
|
}
|
112
|
if (in_array($this->operator, $this->operator_values(1))) {
|
113
|
$output .= ' ' . check_plain($this->value);
|
114
|
}
|
115
|
return $output;
|
116
|
}
|
117
|
|
118
|
function operator_values($values = 1) {
|
119
|
$options = array();
|
120
|
foreach ($this->operators() as $id => $info) {
|
121
|
if (isset($info['values']) && $info['values'] == $values) {
|
122
|
$options[] = $id;
|
123
|
}
|
124
|
}
|
125
|
|
126
|
return $options;
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Provide a simple textfield for equality
|
131
|
*/
|
132
|
function value_form(&$form, &$form_state) {
|
133
|
// We have to make some choices when creating this as an exposed
|
134
|
// filter form. For example, if the operator is locked and thus
|
135
|
// not rendered, we can't render dependencies; instead we only
|
136
|
// render the form items we need.
|
137
|
$which = 'all';
|
138
|
if (!empty($form['operator'])) {
|
139
|
$source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
|
140
|
}
|
141
|
if (!empty($form_state['exposed'])) {
|
142
|
$identifier = $this->options['expose']['identifier'];
|
143
|
|
144
|
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
|
145
|
// exposed and locked.
|
146
|
$which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
|
147
|
}
|
148
|
else {
|
149
|
$source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
|
150
|
}
|
151
|
}
|
152
|
|
153
|
if ($which == 'all' || $which == 'value') {
|
154
|
$form['value'] = array(
|
155
|
'#type' => 'textfield',
|
156
|
'#title' => t('Value'),
|
157
|
'#size' => 30,
|
158
|
'#default_value' => $this->value,
|
159
|
);
|
160
|
if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
|
161
|
$form_state['input'][$identifier] = $this->value;
|
162
|
}
|
163
|
|
164
|
if ($which == 'all') {
|
165
|
$form['value'] += array(
|
166
|
'#dependency' => array($source => $this->operator_values(1)),
|
167
|
);
|
168
|
}
|
169
|
}
|
170
|
|
171
|
if (!isset($form['value'])) {
|
172
|
// Ensure there is something in the 'value'.
|
173
|
$form['value'] = array(
|
174
|
'#type' => 'value',
|
175
|
'#value' => NULL
|
176
|
);
|
177
|
}
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Add this filter to the query.
|
182
|
*
|
183
|
* Due to the nature of fapi, the value and the operator have an unintended
|
184
|
* level of indirection. You will find them in $this->operator
|
185
|
* and $this->value respectively.
|
186
|
*/
|
187
|
function query() {
|
188
|
$this->ensure_my_table();
|
189
|
$field = $this->real_field;
|
190
|
|
191
|
$info = $this->operators();
|
192
|
if (!empty($info[$this->operator]['method'])) {
|
193
|
$this->{$info[$this->operator]['method']}($field);
|
194
|
}
|
195
|
}
|
196
|
|
197
|
function op_equal($field) {
|
198
|
$this->query->add_where($this->options['group'], $field, $this->value, $this->operator);
|
199
|
}
|
200
|
|
201
|
function op_contains($field) {
|
202
|
$this->query->add_where($this->options['group'], $field, "*$this->value*", '=');
|
203
|
}
|
204
|
|
205
|
function op_starts($field) {
|
206
|
$this->query->add_where($this->options['group'], $field, "$this->value*", '=');
|
207
|
}
|
208
|
|
209
|
function op_not_starts($field) {
|
210
|
$this->query->add_where($this->options['group'], $field, "$this->value*", '!=');
|
211
|
}
|
212
|
|
213
|
function op_ends($field) {
|
214
|
$this->query->add_where($this->options['group'], $field, "*$this->value", '=');
|
215
|
}
|
216
|
|
217
|
function op_not_ends($field) {
|
218
|
$this->query->add_where($this->options['group'], $field, "*$this->value", '!=');
|
219
|
}
|
220
|
|
221
|
function op_not($field) {
|
222
|
$this->query->add_where($this->options['group'], $field, "*$this->value*", '!=');
|
223
|
}
|
224
|
|
225
|
function op_greater_eq($field) {
|
226
|
$this->query->add_where($this->options['group'], $field, $this->value, '>=');
|
227
|
}
|
228
|
|
229
|
function op_less_eq($field) {
|
230
|
$this->query->add_where($this->options['group'], $field, $this->value, '<=');
|
231
|
}
|
232
|
|
233
|
function op_exists($field) {
|
234
|
$this->query->add_where($this->options['group'], $field, '*', $this->operator == 'exists' ? '=' : '!=');
|
235
|
}
|
236
|
|
237
|
}
|