You are here

function views_handler_filter_like in Views (for Drupal 7) 5

Custom filter for LIKE operations

1 call to views_handler_filter_like()
views_handler_filter_body in modules/views_node.inc
3 string references to 'views_handler_filter_like'
node_views_tables in modules/views_node.inc
This include file implements views functionality on behalf of node.module
profile_views_add_filter in modules/views_profile.inc
Add profile filters to view table
upload_views_tables in modules/views_upload.inc
This include file implements views functionality for the file.inc and upload module

File

./views.module, line 1925

Code

function views_handler_filter_like($op, $filter, $filterinfo, &$query) {
  switch (trim($filter['value'])) {
    case '':
      return;
      break;
  }
  switch ($op) {
    case 'handler':
      $table = $filterinfo['table'];
      $column = $filterinfo['field'];
      $field = "{$table}.{$column}";
      $query
        ->ensure_table($table);
      switch ($filter['operator']) {
        case 'contains':
          $query
            ->add_where("UPPER(%s) LIKE UPPER('%%%s%%')", $field, $filter['value']);
          break;
        case 'word':
        case 'allwords':
          preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $filter['value'], $matches, PREG_SET_ORDER);
          foreach ($matches as $match) {
            $phrase = false;

            // Strip off phrase quotes
            if ($match[2][0] == '"') {
              $match[2] = substr($match[2], 1, -1);
              $phrase = true;
            }
            $words = trim($match[2], ',?!();:-');
            $words = $phrase ? array(
              $words,
            ) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
            foreach ($words as $word) {
              $where[] = "UPPER(%s) LIKE UPPER('%%%s%%')";
              $values[] = $field;
              $values[] = trim($word, " ,!?");
            }
          }
          if ($filter['operator'] == 'word') {
            $where = '(' . implode(' OR ', $where) . ')';
          }
          else {
            $where = implode(' AND ', $where);
          }

          // previously this was a call_user_func_array but that's unnecessary
          // as views will unpack an array that is a single arg.
          $query
            ->add_where($where, $values);
          break;
        case 'starts':
          $query
            ->add_where("UPPER(%s) LIKE UPPER('%s%%')", $field, $filter['value']);
          break;
        case 'ends':
          $query
            ->add_where("UPPER(%s) LIKE UPPER('%%%s')", $field, $filter['value']);
          break;
        case 'not':
          $query
            ->add_where("UPPER(%s) NOT LIKE UPPER('%%%s%%')", $field, $filter['value']);
          break;
        case '=':
          $query
            ->add_where("UPPER(%s) = UPPER('%s')", $field, $filter['value']);
          break;
      }
      break;
  }
}