You are here

function views_handler_filter_string_compare::op_word in Amazon Product Advertisement API 7

Same name and namespace in other branches
  1. 6 includes/views_handler_filter_string_compare.inc \views_handler_filter_string_compare::op_word()
  2. 7.2 includes/views_handler_filter_string_compare.inc \views_handler_filter_string_compare::op_word()

File

includes/views_handler_filter_string_compare.inc, line 237

Class

views_handler_filter_string_compare
Basic textfield filter to handle string filtering commands including equality, like, not like, etc.

Code

function op_word($field, $upper) {
  $where = array();
  preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->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 (!$where) {
    return;
  }
  if ($this->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.
  $this->query
    ->add_where($this->options['group'], $where, $values);
}