You are here

function weather_search_location in Weather 6.5

Same name and namespace in other branches
  1. 7.3 weather.forms.inc \weather_search_location()
  2. 7 weather.forms.inc \weather_search_location()
  3. 7.2 weather.forms.inc \weather_search_location()

Searches for the specified location, whether it is a place name or an ICAO code. For example, weather/fuhlsbüttel will display the weather for Hamburg-Fuhlsbüttel.

Parameters

string The argument passed in the URL that specifies the: location which should be searched for.

1 string reference to 'weather_search_location'
weather_menu in ./weather.module
Implementation of hook_menu().

File

./weather.module, line 1071
Display <acronym title="METeorological Aerodrome Report">METAR</acronym> weather data from anywhere in the world

Code

function weather_search_location($search = NULL) {
  if ($search == NULL) {

    // The user did not enter a search string in the URL, so just
    // display the search form.
    return drupal_get_form('weather_search_form');
  }
  else {
    $search = urldecode($search);

    // Do some sanity checks first
    if (strlen($search) < 3 || strlen($search) > 64) {
      drupal_set_message(t('The string to search for must be between 3 and 64 characters.'), 'error');
      drupal_goto('weather');
    }

    // Try to match the ICAO code
    if (strlen($search) == 4) {
      $sql = "SELECT icao, country, name FROM {weather_icao} WHERE icao = '%s'";
      $result = db_query($sql, strtoupper($search));
      if ($location = db_fetch_object($result)) {

        // Use the default configuration for display
        $config = _weather_get_config(0, 0);
        $config['icao'] = $location->icao;
        $config['real_name'] = $location->name;
        $metar = weather_get_metar($location->icao);
        $output = theme('weather_theming', $config, $metar);
        $output .= drupal_get_form('weather_search_form');
        return $output;
      }
    }

    // Try to match on icao, name, or country
    $locations = array();
    $sql = "SELECT icao, country, name FROM {weather_icao}\n      WHERE icao LIKE UPPER('%%%s%%')\n      OR UPPER(country) LIKE UPPER('%%%s%%')\n      OR UPPER(name) LIKE UPPER('%%%s%%')\n      ORDER BY name ASC";
    $result = db_query($sql, $search, $search, $search);
    while ($location = db_fetch_object($result)) {
      $locations[] = $location;
    }

    // If there are no results, notify user
    if (empty($locations)) {
      drupal_set_message(t('Your search did not return any results.'), 'error');
      drupal_goto('weather');
    }
    else {
      if (count($locations) == 1) {
        $location = $locations[0];

        // There's only one search result, so show the weather directly
        // using the default configuration for display
        $config = _weather_get_config(0, 0);
        $config['icao'] = $location->icao;
        $config['real_name'] = $location->name;
        $metar = weather_get_metar($location->icao);
        $output = theme('weather_theming', $config, $metar);
        $output .= drupal_get_form('weather_search_form');
        return $output;
      }
      else {

        // There is more than one result, so show all of them
        // to let the user decide
        $links = array();
        foreach ($locations as $location) {
          $links[] = l($location->name, 'weather/' . $location->icao);
        }
        $title = t('Search results for <q>@search</q>', array(
          '@search' => $search,
        ));
        $output = theme('item_list', $links, $title);
        $output .= drupal_get_form('weather_search_form');
        return $output;
      }
    }
  }
}