function weather_search_location in Weather 7.3
Same name and namespace in other branches
- 6.5 weather.module \weather_search_location()
- 7 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, country, or part of the link (usually region/province).
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 - Implements hook_menu().
File
- ./
weather.forms.inc, line 791 - 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 {
// Do some sanity checks first.
if (drupal_strlen($search) < 3) {
drupal_set_message(t('The string to search for must have at least three characters.'), 'error');
drupal_goto('weather');
}
// Convert input spaces to underscores.
$search = str_replace(' ', '_', $search);
// Try to match on GeoID, name, country, or part of the link.
$locations = 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' => "%{$search}%",
))
->where('UPPER(name) LIKE UPPER(:search)', array(
':search' => "%{$search}%",
))
->where('UPPER(link) 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 {
module_load_include('inc', 'weather', 'weather.common');
if (count($locations) == 1) {
$location = $locations[0];
// There's only one search result, so show the weather by
// redirecting to the correct URL.
drupal_goto(_weather_get_link_for_geoid($location->geoid, 'default'));
}
else {
// There is more than one result, so show all of them
// to let the user decide.
$links = array();
foreach ($locations as $location) {
$search_link = _weather_get_link_for_geoid($location->geoid, 'default');
$links[] = l($location->name, $search_link);
}
$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;
}
}
}
}