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