You are here

function _autocomplete_widgets_get_options_flddata in Autocomplete Widgets for Text and Number Fields 6

Same name and namespace in other branches
  1. 7 autocomplete_widgets.common.inc \_autocomplete_widgets_get_options_flddata()

Fetch an array of options for the given widget (field data).

Options are retrieved from existing values for the field.

1 call to _autocomplete_widgets_get_options_flddata()
_autocomplete_widgets_get_options in ./autocomplete_widgets.common.inc
Fetch an array of options for the given widget.

File

./autocomplete_widgets.common.inc, line 115
Common functions for Autocomplete Widgets module.

Code

function _autocomplete_widgets_get_options_flddata($field, $string = '', $match = 'contains', $keys = NULL, $limit = NULL) {
  $db_info = content_database_info($field);
  $table = $db_info['table'];
  $column = $field['field_name'] . '_' . key($db_info['columns']);
  $where = array();
  $args = array();
  if ($string !== '') {
    $lower = !empty($field['widget']['autocomplete_case']) || $match == 'equals' ? '' : 'LOWER';
    $match_operators = array(
      'contains' => "LIKE {$lower}('%%%s%%')",
      'equals' => "= '%s'",
      'starts_with' => "LIKE {$lower}('%s%%')",
    );
    $where[] = "{$lower}(f." . $column . ') ' . (isset($match_operators[$match]) ? $match_operators[$match] : $match_operators['contains']);
    $args[] = $string;
  }
  else {
    if (isset($keys) && is_array($keys)) {
      $where[] = 'f.' . $column . ' IN (' . db_placeholders($keys) . ')';
      $args = array_merge($args, $keys);
    }
  }

  // When obey_access_controls is enabled, we need to add node status
  // to the where clause before the sql is combined.
  if (!empty($field['widget']['obey_access_controls'])) {
    $where[] = 'n.status = 1 ';
  }
  $sql = 'SELECT f.' . $column . ' FROM {' . $table . '} f WHERE ' . implode(' AND ', $where) . ' ORDER BY f.' . $column;
  if (!empty($field['widget']['obey_access_controls']) || !empty($field['widget']['i18n_flddata'])) {

    // Adding a join with the node table allows the i18n rewrite the query
    // to filter values from node for the proper language.
    $sql = db_rewrite_sql(str_replace(' WHERE ', ' INNER JOIN {node} n ON f.vid = n.vid WHERE ', $sql));
  }
  $result = $limit ? db_query_range($sql, $args, 0, $limit) : db_query($sql, $args);
  $options = array();
  while ($row = db_fetch_object($result)) {
    $options[$row->{$column}] = $row->{$column};
  }
  return $options;
}