function yr_verdata_page_all in Yr Weatherdata 7
Same name and namespace in other branches
- 6.2 yr_verdata.module \yr_verdata_page_all()
Function for generating the main forecast page.
Return value
Returns a themed list of locations with upcoming forecasts.
1 string reference to 'yr_verdata_page_all'
- yr_verdata_menu in ./
yr_verdata.module - Implementation of hook_menu().
File
- ./
yr_verdata.module, line 117 - yr_verdata.module This file contains the code for getting the forecast from yr.no and displaying it on a Drupal site.
Code
function yr_verdata_page_all() {
// First, we see if we have a cache. No need to ask the database,
// filesystem/PHP SimpleXML and yr.no for lots of locations if
// the cache is up to date.
$page_array = array(
'#cache' => array(
'keys' => array(
'yr_verdata',
'page',
'all',
),
'bin' => 'cache',
'expire' => REQUEST_TIME + variable_get('yr_verdata_maxage', 21600),
'granularity' => DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE,
),
);
// Get the layout setting.
$layout = variable_get('yr_verdata_overview_layout', 'table');
// Assign it to the page array, to get the correct cache.
$page_array['#cache']['keys'][] = $layout;
// Generate the cache id and get the cache, if it exists.
$cid = drupal_render_cid_create($page_array);
$cache = cache_get($cid);
if (!is_object($cache)) {
$cache = new stdClass();
}
// To prevent strict warning in the next step.
$cache->expire = isset($cache->expire) ? $cache->expire : 0;
// This is to prevent a PHP notice when the cache is cleared.
if (REQUEST_TIME > $cache->expire) {
// No valid cache, generate new.
// Load locations.
$grouping = variable_get('yr_verdata_group', 'off');
// Are we grouping locations in some way?
$records = _yr_verdata_get_all();
$langs = _yr_verdata_langs();
$last_group = '';
// Create either the table or the box-overview.
if ($layout == 'table') {
// First we set the header and row arrays for the table, and the empty message.
$header = array(
t('Location'),
t('Forecast'),
);
$rows = array();
$empty = t('No locations are stored in the database.');
foreach ($records as $record) {
// Go over the array of locations and generate a row for use in theme_table().
// If we are grouping by something, add pretty "header" rows for each group.
if ($grouping != 'off' && $record->{$grouping} != $last_group) {
$colspan = user_access('administer yr_verdata') ? 3 : 2;
$thegroup = $grouping == 'lang' ? $langs[$record->lang] : check_plain($record->{$grouping});
$rows[$thegroup] = array(
'data' => array(
array(
'data' => $thegroup,
'header' => TRUE,
'colspan' => $colspan,
),
),
);
$last_group = $record->{$grouping};
}
$name = yr_verdata_resolve_name($record);
// Unclean, but cleaned by l().
$rows[$record->yid] = array(
l($name, "forecast/{$record->yid}"),
yr_verdata_generate($record, 'table'),
);
// yid is a serial from the database, so it's safe.
if (user_access('administer yr_verdata')) {
$rows[$record->yid][] = l(t('Delete'), 'forecast/delete/' . $record->yid);
}
}
if (user_access('administer yr_verdata')) {
$header[] = t('Delete location');
$empty .= ' ' . _yr_verdata_addmore_msg();
}
// Add necessary stuff to the page array.
$page_array['content']['#theme'] = 'table';
$page_array['content']['#header'] = $header;
$page_array['content']['#rows'] = $rows;
$page_array['content']['#empty'] = $empty;
}
else {
// No table, create the box-overview.
$boxes = array();
foreach ($records as $record) {
// Go over the array of locations and generate a box with the upcoming/current weather.
$data = simplexml_load_file(_yr_verdata_local_file($record));
$forecast = $data->forecast->tabular->time[0];
$name = yr_verdata_resolve_name($record);
// Unclean, cleaned later.
$box['time'] = date(_yr_verdata_date_format(), strtotime($forecast['from']));
// Clean.
$box['symbol'] = theme('yr_verdata_symbol', array(
'symbol' => $forecast->symbol,
'period' => $forecast['period'],
));
// Clean.
$box['wind'] = theme('yr_verdata_wind', array(
'dir' => $forecast->windDirection,
'speed' => $forecast->windSpeed,
));
// Clean.
$box['temp'] = theme('yr_verdata_temp', array(
'temp' => $forecast->temperature,
));
// Clean.
$box['precip'] = theme('yr_verdata_precip', array(
'precip' => $forecast->precipitation,
));
// Clean.
$box['pressure'] = theme('yr_verdata_pressure', array(
'pressure' => $forecast->pressure,
));
// Clean.
$boxes[$record->yid]['title']['#markup'] = l($name, 'forecast/' . $record->yid);
$boxes[$record->yid]['title']['#prefix'] = '<h4>';
$boxes[$record->yid]['title']['#suffix'] = '</h4>';
$boxes[$record->yid]['forecast'] = yr_verdata_forecastbox($box);
// Add the delete link for admins to the box layout as well.
if (user_access('administer yr_verdata')) {
$boxes[$record->yid]['delete']['#markup'] = l(t('Delete @location', array(
'@location' => $name,
)), 'forecast/delete/' . $record->yid);
$boxes[$record->yid]['delete']['#prefix'] = '<p class="yr-box-delete-link">';
$boxes[$record->yid]['delete']['#suffix'] = '</p>';
}
$boxes[$record->yid]['#prefix'] = '<div class="yr-overview-box">';
$boxes[$record->yid]['#suffix'] = '</div>';
}
// Put the boxes in the output.
$page_array['content']['#markup'] = drupal_render($boxes);
$page_array['content']['#prefix'] = '<div class="yr-forecast-boxes clearfix">';
$page_array['content']['#suffix'] = '</div>';
}
$page_array['pager'] = array(
'#theme' => 'pager',
);
$page_array['credit']['#markup'] = yr_verdata_credit_link();
$page_array['#attached'] = array(
'css' => array(
drupal_get_path('module', 'yr_verdata') . '/yr_verdata.css',
),
);
}
return $page_array;
}