You are here

function weather_search_autocomplete in Weather 7.3

Same name and namespace in other branches
  1. 6.5 weather.module \weather_search_autocomplete()
  2. 7 weather.forms.inc \weather_search_autocomplete()
  3. 7.2 weather.forms.inc \weather_search_autocomplete()

Search for a place, country, or link parts matching a partial string.

Parameters

string $input: The partial text to search for.

1 string reference to 'weather_search_autocomplete'
weather_menu in ./weather.module
Implements hook_menu().

File

./weather.forms.inc, line 892
Provide forms for configuration of weather displays.

Code

function weather_search_autocomplete($input) {
  module_load_include('inc', 'weather', 'weather.common');

  // Convert input spaces to underscores. Note that this is also
  // the wildcard for a single character in SQL.
  $input = str_replace(' ', '_', $input);
  $matches = array();

  // In this query we search for names, countries, and link parts of locations.
  $sql = db_select('weather_places')
    ->fields('weather_places', array(
    'geoid',
    'country',
    'name',
    'link',
  ))
    ->orderBy('name', 'ASC');
  $or = db_or()
    ->where('UPPER(country) LIKE UPPER(:search)', array(
    ':search' => "%{$input}%",
  ))
    ->where('UPPER(name) LIKE UPPER(:search)', array(
    ':search' => "%{$input}%",
  ))
    ->where('UPPER(link) LIKE UPPER(:search)', array(
    ':search' => "%{$input}%",
  ));
  $sql
    ->condition($or);
  $sql
    ->range(0, 10);
  $result = $sql
    ->execute();
  foreach ($result as $match) {

    // Construct a URL for the location.
    $search_link = _weather_get_link_for_geoid($match->geoid, 'autocomplete');
    $matches[$search_link] = $search_link;
  }
  drupal_json_output($matches);
}