• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

handlers/views_handler_filter_string.inc

00001 <?php
00002 
00009 class views_handler_filter_string extends views_handler_filter {
00010   // exposed filter options
00011   var $always_multiple = TRUE;
00012 
00013   function option_definition() {
00014     $options = parent::option_definition();
00015 
00016     $options['expose']['contains']['required'] = array('default' => FALSE);
00017 
00018     return $options;
00019   }
00020 
00026   function operators() {
00027     $operators = array(
00028       '=' => array(
00029         'title' => t('Is equal to'),
00030         'short' => t('='),
00031         'method' => 'op_equal',
00032         'values' => 1,
00033       ),
00034       '!=' => array(
00035         'title' => t('Is not equal to'),
00036         'short' => t('!='),
00037         'method' => 'op_equal',
00038         'values' => 1,
00039       ),
00040       'contains' => array(
00041         'title' => t('Contains'),
00042         'short' => t('contains'),
00043         'method' => 'op_contains',
00044         'values' => 1,
00045       ),
00046       'word' => array(
00047         'title' => t('Contains any word'),
00048         'short' => t('has word'),
00049         'method' => 'op_word',
00050         'values' => 1,
00051       ),
00052       'allwords' => array(
00053         'title' => t('Contains all words'),
00054         'short' => t('has all'),
00055         'method' => 'op_word',
00056         'values' => 1,
00057       ),
00058       'starts' => array(
00059         'title' => t('Starts with'),
00060         'short' => t('begins'),
00061         'method' => 'op_starts',
00062         'values' => 1,
00063       ),
00064       'not_starts' => array(
00065         'title' => t('Does not start with'),
00066         'short' => t('not_begins'),
00067         'method' => 'op_not_starts',
00068         'values' => 1,
00069       ),
00070       'ends' => array(
00071         'title' => t('Ends with'),
00072         'short' => t('ends'),
00073         'method' => 'op_ends',
00074         'values' => 1,
00075       ),
00076       'not_ends' => array(
00077         'title' => t('Does not end with'),
00078         'short' => t('not_ends'),
00079         'method' => 'op_not_ends',
00080         'values' => 1,
00081       ),
00082       'not' => array(
00083         'title' => t('Does not contain'),
00084         'short' => t('!has'),
00085         'method' => 'op_not',
00086         'values' => 1,
00087       ),
00088       'shorterthan' => array(
00089         'title' => t('Length is shorter than'),
00090         'short' => t('shorter than'),
00091         'method' => 'op_shorter',
00092         'values' => 1,
00093       ),
00094       'longerthan' => array(
00095         'title' => t('Length is longer than'),
00096         'short' => t('longer than'),
00097         'method' => 'op_longer',
00098         'values' => 1,
00099       ),
00100     );
00101     // if the definition allows for the empty operator, add it.
00102     if (!empty($this->definition['allow empty'])) {
00103       $operators += array(
00104         'empty' => array(
00105           'title' => t('Is empty (NULL)'),
00106           'method' => 'op_empty',
00107           'short' => t('empty'),
00108           'values' => 0,
00109         ),
00110         'not empty' => array(
00111           'title' => t('Is not empty (NOT NULL)'),
00112           'method' => 'op_empty',
00113           'short' => t('not empty'),
00114           'values' => 0,
00115         ),
00116       );
00117     }
00118     // Add regexp support for MySQL.
00119     if (Database::getConnection()->databaseType() == 'mysql') {
00120       $operators += array(
00121         'regular_expression' => array(
00122           'title' => t('Regular expression'),
00123           'short' => t('regex'),
00124           'method' => 'op_regex',
00125           'values' => 1,
00126         ),
00127       );
00128     }
00129 
00130     return $operators;
00131   }
00132 
00136   function operator_options($which = 'title') {
00137     $options = array();
00138     foreach ($this->operators() as $id => $info) {
00139       $options[$id] = $info[$which];
00140     }
00141 
00142     return $options;
00143   }
00144 
00145   function admin_summary() {
00146     if (!empty($this->options['exposed'])) {
00147       return t('exposed');
00148     }
00149 
00150     $options = $this->operator_options('short');
00151     $output = '';
00152     if(!empty($options[$this->operator])) {
00153       $output = check_plain($options[$this->operator]);
00154     }
00155     if (in_array($this->operator, $this->operator_values(1))) {
00156       $output .= ' ' . check_plain($this->value);
00157     }
00158     return $output;
00159   }
00160 
00161   function operator_values($values = 1) {
00162     $options = array();
00163     foreach ($this->operators() as $id => $info) {
00164       if (isset($info['values']) && $info['values'] == $values) {
00165         $options[] = $id;
00166       }
00167     }
00168 
00169     return $options;
00170   }
00171 
00175   function value_form(&$form, &$form_state) {
00176     // We have to make some choices when creating this as an exposed
00177     // filter form. For example, if the operator is locked and thus
00178     // not rendered, we can't render dependencies; instead we only
00179     // render the form items we need.
00180     $which = 'all';
00181     if (!empty($form['operator'])) {
00182       $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
00183     }
00184     if (!empty($form_state['exposed'])) {
00185       $identifier = $this->options['expose']['identifier'];
00186 
00187       if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
00188         // exposed and locked.
00189         $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
00190       }
00191       else {
00192         $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
00193       }
00194     }
00195 
00196     if ($which == 'all' || $which == 'value') {
00197       $form['value'] = array(
00198         '#type' => 'textfield',
00199         '#title' => t('Value'),
00200         '#size' => 30,
00201         '#default_value' => $this->value,
00202       );
00203       if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
00204         $form_state['input'][$identifier] = $this->value;
00205       }
00206 
00207       if ($which == 'all') {
00208         $form['value'] += array(
00209           '#dependency' => array($source => $this->operator_values(1)),
00210         );
00211       }
00212     }
00213 
00214     if (!isset($form['value'])) {
00215       // Ensure there is something in the 'value'.
00216       $form['value'] = array(
00217         '#type' => 'value',
00218         '#value' => NULL
00219       );
00220     }
00221   }
00222 
00223   function operator() {
00224     return $this->operator == '=' ? 'LIKE' : 'NOT LIKE';
00225   }
00226 
00234   function query() {
00235     $this->ensure_my_table();
00236     $field = "$this->table_alias.$this->real_field";
00237 
00238     $info = $this->operators();
00239     if (!empty($info[$this->operator]['method'])) {
00240       $this->{$info[$this->operator]['method']}($field);
00241     }
00242   }
00243 
00244   function op_equal($field) {
00245     $this->query->add_where($this->options['group'], $field, $this->value, $this->operator());
00246   }
00247 
00248   function op_contains($field) {
00249     $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'LIKE');
00250   }
00251 
00252   function op_word($field) {
00253     $where = $this->operator == 'word' ? db_or() : db_and();
00254 
00255     // Don't filter on empty strings.
00256     if (empty($this->value)) {
00257       return;
00258     }
00259 
00260     preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
00261     foreach ($matches as $match) {
00262       $phrase = false;
00263       // Strip off phrase quotes
00264       if ($match[2]{0} == '"') {
00265         $match[2] = substr($match[2], 1, -1);
00266         $phrase = true;
00267       }
00268       $words = trim($match[2], ',?!();:-');
00269       $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
00270       foreach ($words as $word) {
00271         $placeholder = $this->placeholder();
00272         $where->condition($field, '%' . db_like(trim($word, " ,!?")) . '%', 'LIKE');
00273       }
00274     }
00275 
00276     if (!$where) {
00277       return;
00278     }
00279 
00280     // previously this was a call_user_func_array but that's unnecessary
00281     // as views will unpack an array that is a single arg.
00282     $this->query->add_where($this->options['group'], $where);
00283   }
00284 
00285   function op_starts($field) {
00286     $this->query->add_where($this->options['group'], $field, db_like($this->value) . '%', 'LIKE');
00287   }
00288 
00289   function op_not_starts($field) {
00290     $this->query->add_where($this->options['group'], $field, db_like($this->value) . '%', 'NOT LIKE');
00291   }
00292 
00293   function op_ends($field) {
00294     $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value), 'LIKE');
00295   }
00296 
00297   function op_not_ends($field) {
00298     $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value), 'NOT LIKE');
00299   }
00300 
00301   function op_not($field) {
00302     $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'NOT LIKE');
00303   }
00304 
00305   function op_shorter($field) {
00306     $placeholder = $this->placeholder();
00307     $this->query->add_where_expression($this->options['group'], "LENGTH($field) < $placeholder", array($placeholder => $this->value));
00308   }
00309 
00310   function op_longer($field) {
00311     $placeholder = $this->placeholder();
00312     $this->query->add_where_expression($this->options['group'], "LENGTH($field) > $placeholder", array($placeholder => $this->value));
00313   }
00314 
00315   function op_regex($field) {
00316     $this->query->add_where($this->options['group'], $field, $this->value, 'RLIKE');
00317   }
00318 
00319   function op_empty($field) {
00320     if ($this->operator == 'empty') {
00321       $operator = "IS NULL";
00322     }
00323     else {
00324       $operator = "IS NOT NULL";
00325     }
00326 
00327     $this->query->add_where($this->options['group'], $field, NULL, $operator);
00328   }
00329 
00330 }

Generated on Sun Feb 26 2012 12:52:51 for Views by  doxygen 1.7.1