function theme_yr_verdata_location_page in Yr Weatherdata 6
Theming of a page with detailed forecast for a location.
Parameters
$row: A complete row returned from a query against the database {yr_verdata} table.
Return value
Returns variables to be put into the template file.
1 theme call to theme_yr_verdata_location_page()
- yr_verdata_page in ./
yr_verdata.module - Function to display a page with yr_verdata stuff.
File
- ./
yr_verdata.module, line 207 - yr_verdata.module This file provides the yr_verdata forecast module.
Code
function theme_yr_verdata_location_page($row) {
$modpath = drupal_get_path('module', 'yr_verdata');
drupal_add_css($modpath . '/yr_verdata.css');
// jquery_ui.module is not required, but highly recommended.
if (function_exists('jquery_ui_add')) {
jquery_ui_add('ui.tabs');
$ui_ver = variable_get('yr_jquery_ui_version', '17');
drupal_add_js($modpath . '/js/yr_verdata.' . $ui_ver . '.js');
}
// Make sure that there is no punctuation in the url, as this could possibly
// lead to a malicious user navigating elsewhere in the filesystem?
$row->url = str_replace('.', '_', $row->url);
// Load the xml file.
if (_yr_verdata_xml($row->url) != FALSE) {
$comps = explode('/', $row->url);
$local_dir = file_directory_path() . '/yr_verdata';
$local_file = $local_dir . '/' . implode('_', $comps) . '.xml';
$data = simplexml_load_file($local_file);
}
// Set up our forecast-array.
$location = array();
$location['info'] = t('@type, @altitude m.a.s. <br />Last updated !lastupdate', array(
'@type' => $data->location->type,
'@altitude' => $data->location->location['altitude'],
'!lastupdate' => _yr_format_time($data->meta->lastupdate),
));
// Links.
$location_at_yr = t('Forecast for <a href="!url" title="Go to @location at yr.no">@location at yr.no</a>', array(
'@location' => $data->location->name,
'!url' => check_url($data->links->link[1]['url']),
));
$gm_url = 'http://maps.google.com/maps?ie=UTF8&hl=en&z=12&ll=';
$gm_url .= $data->location->location['latitude'] . ',' . $data->location->location['longitude'];
$gmaps = t('Map over <a href="!url" title="Go to @location at Google Maps">@location at Google Maps</a>', array(
'@location' => $data->location->name,
'!url' => check_url($gm_url),
));
$wikipedia = t('Information about <a href="!url" title="Go to @location at Wikipedia">@location at Wikipedia</a>', array(
'@location' => $data->location->name,
'!url' => check_url('http://en.wikipedia.org/wiki/' . drupal_urlencode($data->location->name)),
));
$items = array(
$location_at_yr,
$gmaps,
$wikipedia,
);
$location['links'] = theme('item_list', $items);
// A credit notice.
$location['credit'] = l($data->credit->link['text'], $data->credit->link['url']);
// Sunrise and sunset times.
if ($data->sun['never_set'] == 'true') {
$location['sun'] = t('Midnight sun, the sun doesn’t set.');
}
elseif ($data->sun['never_rise'] == 'true') {
$location['sun'] = t('Polar night, the sun doesn’t rise.');
}
else {
$sunrise = _yr_format_time($data->sun['rise']);
$sunset = _yr_format_time($data->sun['set']);
$location['sun'] = t('Sunrise: !sunrise<br />Sunset: !sunset', array(
'!sunrise' => $sunrise,
'!sunset' => $sunset,
));
}
// Text forecast, if it exists (only for mainland Norway locations).
$location['text_forecast'] = array();
if (isset($data->forecast->text->location)) {
foreach ($data->forecast->text->location->time as $item) {
$location['text_forecast'][] = array(
'title' => drupal_ucfirst(check_plain($item->title)),
'body' => filter_xss($item->body),
);
}
}
// Set up an array with all the graphical forecast-periods.
$location['symbol_forecast'] = array();
$periods = _yr_periods();
$last_period = 0;
$i = 0;
foreach ($data->forecast->tabular->time as $tab) {
if ($i > 15 && $last_period == 3) {
break;
}
// Some of the values we are putting in needs to be "calculated".
// Main symbol:
$symbol = _yr_verdata_get_symbol($tab);
// Wind:
$wind = _yr_verdata_get_wind($tab);
// Temperature:
$temp = _yr_verdata_get_temp($tab);
// Pressure:
$pressure = $tab->pressure['value'] . $tab->pressure['unit'];
// Time:
$this_time = _yr_format_time($tab['from'], 'custom', 'D d M') . '<br />' . $periods[(int) $tab['period']];
// We need for the template to know if this was the last period of the day, to facilitate sexier styling.
$open = $i == 0 || $last_period == 3 || $tab['period'] == 2 && $last_period != 1 ? '<div class="yr-period-day clear-block">' : '';
$close = $tab['period'] == 3 || $tab['period'] == 2 && $last_period != 1 && $i != 0 ? '</div>' : '';
// Assign the calculated and other values.
$location['symbol_forecast'][] = array(
'open' => $open,
'close' => $close,
'class' => 'yr-period-' . (int) $tab['period'],
'time' => $this_time,
'symbol' => $symbol,
'wind' => $wind,
'precip' => t('@precip mm precipitation.', array(
'@precip' => $tab->precipitation['value'],
)),
'temp' => $temp,
'pressure' => check_plain($pressure),
);
$last_period = (int) $tab['period'];
$i++;
}
// If we are in mainland Norway, add a radarimage.
if (in_array($comps[1], array(
'Norway',
'Norge',
'Noreg',
))) {
$lat = (int) $data->location->location['latitude'];
$long = (int) $data->location->location['longitude'];
$radarsite = FALSE;
// Figure out which radarimage to use. Start narrow in the south and expand if lat/long is outside range.
// Finally set $showradar = FALSE; if no compatible lat/long range is found.
if ($lat >= 57 && $lat < 61 && $long >= 4 && $long < 7) {
$radarsite = 'southwest_norway';
$radartext = t('Southwest-Norway');
}
elseif ($lat >= 57 && $lat < 61 && $long >= 7 && $long <= 13) {
$radarsite = 'southeast_norway';
$radartext = t('Southeast-Norway');
}
elseif ($lat >= 60 && $lat < 62 && $long >= 4 && $long < 8) {
$radarsite = 'western_norway';
$radartext = t('Western Norway');
}
elseif ($lat >= 62 && $lat < 66 && $long >= 4 && $long <= 14) {
$radarsite = 'central_norway';
$radartext = t('Central Norway');
}
elseif ($lat >= 57 && $lat < 66 && $long >= 4 && $long <= 14) {
$radarsite = 'south_norway';
$radartext = t('Southern Norway');
}
elseif ($lat >= 65 && $lat < 69 && $long > 10 && $long <= 18) {
$radarsite = 'nordland_troms';
$radartext = t('Northern Norway');
}
elseif ($lat >= 68 && $lat < 72 && $long > 17 && $long <= 30) {
$radarsite = 'troms_finnmark';
$radartext = t('Northernmost Norway');
}
else {
$radarsite = FALSE;
}
$radar_url = check_url('http://api.yr.no/weatherapi/radar/1.2/?radarsite=' . $radarsite . ';width=460;type=animation');
$radar = '<img src="' . $radar_url . '" alt="' . $radartext . '" />';
$radarlink = !empty($data->links->link[5]['url']) ? check_url($data->links->link[5]['url']) : check_url($data->links->link[1]['url']);
$location['radar']['text'] = $radarsite == FALSE ? t('No image') : $radartext;
$location['radar']['image'] = $radarsite == FALSE ? t('This location is currently not covered by a radar.') : l($radar, $radarlink, array(
'html' => TRUE,
));
}
else {
$location['radar'] = '';
}
// Add the tabs titles.
$location['tabs'] = array(
t('Info'),
t('Text forecast'),
t('Detailed forecast'),
t('Radar image'),
);
// Add the location name, for those who want to customize the .tpl file.
$location['name'] = check_plain($data->location->name);
// Theme the output.
$output = theme('yr_verdata_location_sheet', $location['name'], $location['tabs'], $location['info'], $location['links'], $location['sun'], $location['text_forecast'], $location['symbol_forecast'], $location['radar'], $location['credit']);
// Set the page title.
drupal_set_title(t('Weather forecast for @location', array(
'@location' => $data->location->name,
)));
// If we don't have a valid location, $data will be empty, and the page should just reflect that.
if ($data != TRUE) {
$unavailable = t('Weather forecast unavailable');
drupal_set_title($unavailable);
$output = '';
}
return $output;
}