You are here

function finder_wheres in Finder 7

Same name and namespace in other branches
  1. 6 finder.module \finder_wheres()
1 call to finder_wheres()
finder_find_query in ./finder.module
Build basic finder query arrays.

File

./finder.module, line 1076
The finder module.

Code

function finder_wheres($wheres) {
  static $placeholders = 0;
  $return = array(
    'sql' => '',
    'args' => array(),
  );
  $operator = isset($wheres['#operator']) ? $wheres['#operator'] : 'AND';
  unset($wheres['#operator']);
  foreach ($wheres as $key => $where) {
    if (is_array($where)) {
      $finder_wheres = finder_wheres($where);
      if ($finder_wheres['sql']) {
        $wheres[$key] = '(' . $finder_wheres['sql'] . ')';
        if ($finder_wheres['args']) {
          $return['args'] = array_merge($return['args'], $finder_wheres['args']);
        }
      }
      else {
        unset($wheres[$key]);
      }
    }
    elseif (is_object($where)) {
      $placeholder = ':finder_' . $placeholders++;
      $return['args'][$placeholder] = $where->value;
      if (is_array($where->value)) {
        $placeholder = '(' . $placeholder . ')';
      }
      $wheres[$key] = $where->field . ' ' . $where->match . ' ' . $placeholder;
    }
  }
  if (!empty($wheres)) {
    $return['sql'] = implode(' ' . $operator . ' ', $wheres);
  }
  return $return;
}