finder_find_query

Versions
6--1
finder_find_query($query, $finder, $finder_element_id, $keywords, $mode, $match, $pager, $join_ons, $base_table, $base_field)

Build basic finder query arrays.

This is a helper function that can be leveraged by basic hook_finder_find() implementations. It takes care of the common tasks that need to be done to build the $query array correctly, and allows the base handler modules to focus on their specialties.

Parameters

$query The query array to modify.

$finder The finder object.

$finder_element_id If $mode is 'choices', this is the finder element id to get choices for.

$keywords An array keyed by finder_element_id, where the values are any str/num/bool/null or an array of such values to be OR'd together.

$mode 'choices' or 'results' depending on what we are fetching.

$match The match method, see finder_match_operator().

$pager Used to limit choices or results per page.

$join_ons Join information in the form of an array where the key is the table string as returned when a value returned from hook_finder_fields() is put through finder_split_info() and the value is an array where the keys are the names of real tables to join, and the values are the 'ON condition' that follows the ON keyword in SQL.

$base_table The name of the base table to query.

$base_field The primary key of the base table.

Return value

The modified query array.

Code

./finder.module, line 924

<?php
function finder_find_query($query, $finder, $finder_element_id, $keywords, $mode, $match, $pager, $join_ons, $base_table, $base_field) {

  $options = array();

  $results_match = finder_match_operator($finder->settings['advanced']['match']);
  $element_match = finder_match_operator($match);

  $query['from'] = '{'. $base_table .'} '. $base_table;

  if ($mode == 'results') {
    $query['selects'][] = $base_table .'.'. $base_field;
    $query['groups'][] = $base_table .'.'. $base_field;
  }

  foreach ($keywords as $feid => $keyword_array) {

    $element = &finder_element($finder, $feid);
    $fields[$feid] = &$element->settings['choices']['field'];
    foreach ($fields[$feid] as $key => $field) {
      $field_info[$feid][$key] = finder_split_field($field);
    }
    $sort[$feid] = &$element->settings['choices']['sort'];

    /*
    if ($mode == 'choices' && $feid == $finder_element_id && $element->settings['choices']['per_result']) {
      $query['selects'][] = $base_table .'.'. $base_field;
      $query['groups'][] = $base_table .'.'. $base_field;
    }
    */

    foreach ($fields[$feid] as $key => $field) {
      $field_alias = 'finder_element_'. $feid .'_'. $field_info[$feid][$key]['table'] .'_'. $field_info[$feid][$key]['field'];
      $query['selects'][] = $field .' AS '. $field_alias;
      if ($mode == 'choices' && $feid == $finder_element_id) {
        $query['groups'][] = $field_alias;
      }
    }

    // join tables if needed
    foreach ($fields[$feid] as $key => $field) {
      if (in_array($field_info[$feid][$key]['table'], array_keys($join_ons))) {
        $join_on = $join_ons[$field_info[$feid][$key]['table']];
        foreach ($join_on as $table => $on) {
          $query['joins'][] = 'INNER JOIN {'. $table .'} '. $table .' ON '. $on;
        }
      }
    }

    $operator = $element->settings['advanced']['value_combination'] ? 'AND' : 'OR';

    // restrict by keywords on field
    $keyword_array = (array)$keyword_array;
    if (!empty($keyword_array)) {
      $query['wheres']['keywords']['#operator'] = $operator;
      foreach ($keyword_array as $keyword) {
        if (!is_null($keyword)) {
          if ($feid == $finder_element_id) {
            foreach ($fields[$feid] as $key => $field) {
              $query['wheres']['keywords'][] = $field . finder_placeholder($element_match, $field_info[$feid][$key]['table'], $field_info[$feid][$key]['field']);
              $query['arguments'][] = $keyword;
            }
          }
          else {
            $query['wheres']['restrictions'][$feid]['keywords']['#operator'] = $operator;
            foreach ($fields[$feid] as $key => $field) {
              $query['wheres']['restrictions'][$feid]['keywords'][] = $field . finder_placeholder($results_match, $field_info[$feid][$key]['table'], $field_info[$feid][$key]['field']);
              $query['arguments'][] = $keyword;
            }
          }
        }
      }
    }

  }

  // provide info for db_rewrite_sql
  $query['primary_table'] = $base_table;
  $query['primary_field'] = $base_field;

  // ensure there are no duplicate joins
  if (!empty($query['joins'])) {
    $query['joins'] = array_unique($query['joins']);
  }

  // for additional elements wheres group
  if (!empty($query['wheres']['restrictions'])) {
    $query['wheres']['restrictions']['#operator'] = $finder->settings['advanced']['element_combination'] ? 'OR' : 'AND';
  }

  // if this is a choices list add a sort if there is only one field
  if ($mode == 'choices' && $sort[$finder_element_id] && count($fields[$finder_element_id]) === 1) {
    $query['orders'][] = end($fields[$finder_element_id]);
  }

  if ($pager) {
    $query['limit'] = $pager;
    $query['pager'] = TRUE;
  }

  // Restrict to one result if $finder->go is TRUE.
  if ($mode == 'results' && isset($finder->go) && $finder->go) {
    $query['limit'] = 1;
    $query['pager'] = TRUE;
  }

  return $query;

}
?>

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Mollom CAPTCHA (play audio CAPTCHA)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.