function finder_find_query in Finder 7
Same name and namespace in other branches
- 6 finder.module \finder_find_query()
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_condition_args().
$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.
2 calls to finder_find_query()
- finder_node_finder_find in modules/
finder_node/ finder_node.module - Implements hook_finder_find().
- finder_user_finder_find in modules/
finder_user/ finder_user.module - Implements hook_finder_find().
File
- ./
finder.module, line 954 - The finder module.
Code
function finder_find_query($prequery, $finder, $finder_element_id, $keywords, $mode, $match, $pager, $join_ons, $base_table, $base_field) {
$options = array();
$query = db_select($base_table);
if ($pager) {
$query = $query
->extend('PagerDefault')
->limit($pager);
}
if ($mode == 'results') {
$base = $query
->addField($base_table, $base_field);
$query
->addExpression("'" . $base_field . "'", 'base_field');
$query
->addExpression("'" . $base_table . "'", 'base_table');
$query
->groupBy($base);
}
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']) {
$base = $query
->addField($base_table, $base_field);
$query
->addExpression('"' . $base_field . '"', 'base_field');
$query
->addExpression('"' . $base_table . '"', 'base_table');
$query
->groupBy($base);
}
foreach ($fields[$feid] as $key => $field) {
$field_alias = finder_field_alias($feid, $field_info[$feid][$key]['table'], $field_info[$feid][$key]['field']);
$field_alias = $query
->addField($field_info[$feid][$key]['table'], $field_info[$feid][$key]['field'], $field_alias);
if ($mode == 'choices' && $feid == $finder_element_id) {
$query
->groupBy($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
->innerJoin($table, $table, $on);
}
}
}
$field_operator = $element->settings['advanced']['field_combination'] ? 'AND' : 'OR';
$value_operator = $element->settings['advanced']['value_combination'] ? 'AND' : 'OR';
$nesting_order =& $element->settings['advanced']['nesting_order'];
$outer_operator = $nesting_order ? $field_operator : $value_operator;
$inner_operator = $nesting_order ? $value_operator : $field_operator;
// Restrict by keywords on field.
$keyword_array = (array) $keyword_array;
$keyword_array = array_filter($keyword_array, 'finder_empty_keyword');
if (!empty($keyword_array)) {
$prequery['conditions']['outer']['#operator'] = $outer_operator;
foreach ($keyword_array as $keyword_index => $keyword) {
if ($feid == $finder_element_id) {
foreach ($fields[$feid] as $key => $field) {
$inner_key = $nesting_order ? $key : $keyword_index;
if (!isset($prequery['conditions']['outer']['inner' . $inner_key]['#operator'])) {
$prequery['conditions']['outer']['inner' . $inner_key]['#operator'] = $inner_operator;
}
$prequery['conditions']['outer']['inner' . $inner_key][] = finder_condition_args($field, $keyword, $match);
}
}
else {
$prequery['conditions']['restrictions'][$feid]['outer']['#operator'] = $outer_operator;
foreach ($fields[$feid] as $key => $field) {
$inner_key = $nesting_order ? $key : $keyword_index;
if (!isset($prequery['conditions']['restrictions'][$feid]['outer']['inner' . $inner_key]['#operator'])) {
$prequery['conditions']['restrictions'][$feid]['outer']['inner' . $inner_key]['#operator'] = $inner_operator;
}
$prequery['conditions']['restrictions'][$feid]['outer']['inner' . $inner_key][] = finder_condition_args($field, $keyword, $element->settings['advanced']['match']);
}
}
}
}
}
// for additional elements wheres group
if (!empty($prequery['conditions']['restrictions'])) {
$prequery['conditions']['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
->orderBy(end($fields[$finder_element_id]));
}
// Restrict to one result if $finder->go is TRUE.
if ($mode == 'results' && isset($finder->go) && $finder->go) {
$query
->range(0, 1);
}
if (!empty($prequery['conditions'])) {
$conditions = finder_wheres($prequery['conditions']);
$query
->where($conditions['sql'], $conditions['args']);
}
if (!empty($prequery['joins'])) {
foreach ($prequery['joins'] as $join_func => $joins) {
foreach ($joins as $join) {
call_user_func_array(array(
$query,
$join_func,
), $join);
}
}
}
return $query
->execute()
->fetchAll();
}