function weather_search_location in Weather 7
Same name and namespace in other branches
- 6.5 weather.module \weather_search_location()
- 7.3 weather.forms.inc \weather_search_location()
- 7.2 weather.forms.inc \weather_search_location()
Search for a given 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 $search: 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 - Implement hook_menu().
File
- ./
weather.forms.inc, line 632 - Provide forms for configuration of weather displays.
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 (drupal_strlen($search) < 3 || drupal_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 an exact ICAO code.
if (drupal_strlen($search) == 4) {
$location = db_query('SELECT icao, country, name FROM {weather_icao} WHERE icao=:icao', array(
':icao' => drupal_strtoupper($search),
))
->fetchObject();
if ($location) {
// Use the default configuration for display.
$display = weather_get_display_settings(NULL);
$metar = weather_get_metar($location->icao);
$location->real_name = $location->name;
$output = theme('weather_theming', array(
'display' => $display,
'location' => $location,
'metar' => $metar,
));
$form = drupal_get_form('weather_search_form');
$output .= drupal_render($form);
return $output;
}
}
// Try to match on icao, name, or country.
$locations = array();
$sql = db_select('weather_icao')
->fields('weather_icao', array(
'icao',
'country',
'name',
))
->orderBy('name', 'ASC');
$or = db_or()
->where('icao LIKE UPPER(:search)', array(
':search' => "%{$search}%",
))
->where('UPPER(country) LIKE UPPER(:search)', array(
':search' => "%{$search}%",
))
->where('UPPER(name) LIKE UPPER(:search)', array(
':search' => "%{$search}%",
));
$sql
->condition($or);
$result = $sql
->execute();
foreach ($result as $location) {
$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.
// Use the default configuration for display.
$display = weather_get_display_settings(NULL);
$metar = weather_get_metar($location->icao);
$location->real_name = $location->name;
$output = theme('weather_theming', array(
'display' => $display,
'location' => $location,
'metar' => $metar,
));
$form = drupal_get_form('weather_search_form');
$output .= drupal_render($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', array(
'items' => $links,
'title' => $title,
));
$form = drupal_get_form('weather_search_form');
$output .= drupal_render($form);
return $output;
}
}
}
}