5 * Definition of views_handler_filter_string.
9 * Basic textfield filter to handle string filtering commands
10 * including equality, like, not like, etc.
12 * @ingroup views_filter_handlers
14 class views_handler_filter_string extends views_handler_filter {
15 // exposed filter options
16 var $always_multiple = TRUE;
18 function option_definition() {
19 $options = parent::option_definition();
21 $options['expose']['contains']['required'] = array('default' => FALSE, 'bool' => TRUE);
27 * This kind of construct makes it relatively easy for a child class
28 * to add or remove functionality by overriding this function and
29 * adding/removing items from this array.
31 function operators() {
34 'title' => t('Is equal to'),
36 'method' => 'op_equal',
40 'title' => t('Is not equal to'),
42 'method' => 'op_equal',
46 'title' => t('Contains'),
47 'short' => t('contains'),
48 'method' => 'op_contains',
52 'title' => t('Contains any word'),
53 'short' => t('has word'),
54 'method' => 'op_word',
58 'title' => t('Contains all words'),
59 'short' => t('has all'),
60 'method' => 'op_word',
64 'title' => t('Starts with'),
65 'short' => t('begins'),
66 'method' => 'op_starts',
69 'not_starts' => array(
70 'title' => t('Does not start with'),
71 'short' => t('not_begins'),
72 'method' => 'op_not_starts',
76 'title' => t('Ends with'),
78 'method' => 'op_ends',
82 'title' => t('Does not end with'),
83 'short' => t('not_ends'),
84 'method' => 'op_not_ends',
88 'title' => t('Does not contain'),
93 'shorterthan' => array(
94 'title' => t('Length is shorter than'),
95 'short' => t('shorter than'),
96 'method' => 'op_shorter',
99 'longerthan' => array(
100 'title' => t('Length is longer than'),
101 'short' => t('longer than'),
102 'method' => 'op_longer',
106 // if the definition allows for the empty operator, add it.
107 if (!empty($this->definition['allow empty'])) {
110 'title' => t('Is empty (NULL)'),
111 'method' => 'op_empty',
112 'short' => t('empty'),
115 'not empty' => array(
116 'title' => t('Is not empty (NOT NULL)'),
117 'method' => 'op_empty',
118 'short' => t('not empty'),
123 // Add regexp support for MySQL.
124 if (Database::getConnection()->databaseType() == 'mysql') {
126 'regular_expression' => array(
127 'title' => t('Regular expression'),
128 'short' => t('regex'),
129 'method' => 'op_regex',
139 * Build strings from the operators() for 'select' options
141 function operator_options($which = 'title') {
143 foreach ($this->operators() as $id => $info) {
144 $options[$id] = $info[$which];
150 function admin_summary() {
151 if ($this->is_a_group()) {
154 if (!empty($this->options['exposed'])) {
158 $options = $this->operator_options('short');
160 if(!empty($options[$this->operator])) {
161 $output = check_plain($options[$this->operator]);
163 if (in_array($this->operator, $this->operator_values(1))) {
164 $output .= ' ' . check_plain($this->value);
169 function operator_values($values = 1) {
171 foreach ($this->operators() as $id => $info) {
172 if (isset($info['values']) && $info['values'] == $values) {
181 * Provide a simple textfield for equality
183 function value_form(&$form, &$form_state) {
184 // We have to make some choices when creating this as an exposed
185 // filter form. For example, if the operator is locked and thus
186 // not rendered, we can't render dependencies; instead we only
187 // render the form items we need.
189 if (!empty($form['operator'])) {
190 $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
192 if (!empty($form_state['exposed'])) {
193 $identifier = $this->options['expose']['identifier'];
195 if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
196 // exposed and locked.
197 $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
200 $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
204 if ($which == 'all' || $which == 'value') {
205 $form['value'] = array(
206 '#type' => 'textfield',
207 '#title' => t('Value'),
209 '#default_value' => $this->value,
211 if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
212 $form_state['input'][$identifier] = $this->value;
215 if ($which == 'all') {
216 $form['value'] += array(
217 '#dependency' => array($source => $this->operator_values(1)),
222 if (!isset($form['value'])) {
223 // Ensure there is something in the 'value'.
224 $form['value'] = array(
231 function operator() {
232 return $this->operator == '=' ? 'LIKE' : 'NOT LIKE';
236 * Add this filter to the query.
238 * Due to the nature of fapi, the value and the operator have an unintended
239 * level of indirection. You will find them in $this->operator
240 * and $this->value respectively.
243 $this->ensure_my_table();
244 $field = "$this->table_alias.$this->real_field";
246 $info = $this->operators();
247 if (!empty($info[$this->operator]['method'])) {
248 $this->{$info[$this->operator]['method']}($field);
252 function op_equal($field) {
253 $this->query->add_where($this->options['group'], $field, $this->value, $this->operator());
256 function op_contains($field) {
257 $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'LIKE');
260 function op_word($field) {
261 $where = $this->operator == 'word' ? db_or() : db_and();
263 // Don't filter on empty strings.
264 if (empty($this->value)) {
268 preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
269 foreach ($matches as $match) {
271 // Strip off phrase quotes
272 if ($match[2]{0} == '"') {
273 $match[2] = substr($match[2], 1, -1);
276 $words = trim($match[2], ',?!();:-');
277 $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
278 foreach ($words as $word) {
279 $placeholder = $this->placeholder();
280 $where->condition($field, '%' . db_like(trim($word, " ,!?")) . '%', 'LIKE');
288 // previously this was a call_user_func_array but that's unnecessary
289 // as views will unpack an array that is a single arg.
290 $this->query->add_where($this->options['group'], $where);
293 function op_starts($field) {
294 $this->query->add_where($this->options['group'], $field, db_like($this->value) . '%', 'LIKE');
297 function op_not_starts($field) {
298 $this->query->add_where($this->options['group'], $field, db_like($this->value) . '%', 'NOT LIKE');
301 function op_ends($field) {
302 $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value), 'LIKE');
305 function op_not_ends($field) {
306 $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value), 'NOT LIKE');
309 function op_not($field) {
310 $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'NOT LIKE');
313 function op_shorter($field) {
314 $placeholder = $this->placeholder();
315 $this->query->add_where_expression($this->options['group'], "LENGTH($field) < $placeholder", array($placeholder => $this->value));
318 function op_longer($field) {
319 $placeholder = $this->placeholder();
320 $this->query->add_where_expression($this->options['group'], "LENGTH($field) > $placeholder", array($placeholder => $this->value));
323 function op_regex($field) {
324 $this->query->add_where($this->options['group'], $field, $this->value, 'RLIKE');
327 function op_empty($field) {
328 if ($this->operator == 'empty') {
329 $operator = "IS NULL";
332 $operator = "IS NOT NULL";
335 $this->query->add_where($this->options['group'], $field, NULL, $operator);