function yr_verdata_block in Yr Weatherdata 6.2
Same name and namespace in other branches
- 6 yr_verdata.module \yr_verdata_block()
Implementation of hook_block().
File
- ./
yr_verdata.module, line 249 - 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_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
/**************
* Block info *
**************/
case 'list':
// The default block.
$blocks[0]['info'] = t('Yr.no Weather Forecast');
// If multiple blocks are enabled, create a block for each location.
if (variable_get('yr_verdata_multiblocks', 'off') == 'on') {
$result = db_query("SELECT * FROM {yr_verdata} ORDER BY name ASC");
while ($record = db_fetch_object($result)) {
$load = yr_verdata_load_location($record->yid);
$location = $load['data'];
$name = yr_verdata_resolve_name($record);
$d = $record->yid + 1;
// This is to allow a potential randomblock to have delta 1 while also having multiblocks enabled.
// Setting the randomblock to "last delta + 1" could give unexpected results if a new location is added afterwards.
if ($load['status'] == TRUE) {
$blocks[$d]['info'] = t('Yr weather forecast for @location', array(
'@location' => $name,
));
}
}
}
// If the random block option is enabled, show that one as well.
if (variable_get('yr_verdata_randomblock', 'off') == 'on') {
$blocks[1]['info'] = t('Yr random weather forecast');
}
// Return them.
return $blocks;
/**************
* Block view *
**************/
case 'view':
if (!user_access('access content')) {
return array();
}
// Users without content access shouldn't access yr_verdata either.
// First the standard block.
if (empty($delta)) {
$block['subject'] = l(t('Forecast'), 'forecast');
// Check the cache.
$cache_id = 'yr_verdata_block_all';
if ($cache = cache_get($cache_id, 'cache') && $cache->expire > time()) {
if (variable_get('yr_verdata_debug', 0) == 1) {
watchdog('yr_verdata', 'The block for all locations had a valid cache. No regeneration.', array(), WATCHDOG_DEBUG);
}
$block['content'] = $cache->data;
}
else {
// No cache found, or cache is old. Generate new.
if (variable_get('yr_verdata_debug', 0) == 1) {
watchdog('yr_verdata', 'The block for all locations was out of date. Starting regeneration...', array(), WATCHDOG_DEBUG);
}
$block['content'] = '';
// Load locations.
$records = _yr_verdata_get_all(TRUE);
if (count($records) == 0) {
return array();
}
// Ensures that no block is shown if there are no locations.
foreach ($records as $record) {
$load = yr_verdata_load_location($record->yid);
$location = $load['data'];
$name = yr_verdata_resolve_name($record);
// Unclean, but cleaned by yr_verdata_generate_forecastboxes().
$block['content'] .= yr_verdata_generate($location, 'block', $name);
}
$block['content'] .= yr_verdata_credit_link(FALSE, TRUE);
// Set the cache for the block we just generated.
$maxage = variable_get('yr_verdata_maxage', 21600);
cache_set($cache_id, $output, 'cache', time() + $maxage);
}
}
elseif ($delta == 1 && variable_get('yr_verdata_randomblock', 'off') == 'on') {
$load = yr_verdata_load_location(-1);
$location = $load['data'];
$name = yr_verdata_resolve_name($location);
// Unclean, but cleaned by l().
$block['subject'] = l(t('Forecast for !location', array(
'!location' => $name,
)), 'forecast/' . $location->yid);
$block['content'] = yr_verdata_generate($location, 'block') . yr_verdata_credit_link($location->url, TRUE);
}
else {
// Check for the multiblock setting here as well as in hook_block_info(),
// because otherwise if 'yr_multiblocks' is swithced off while multiblocks
// are assigned to regions, they won't show up on the administrative 'blocks'
// page, but still be visible in whatever region they were assigned to.
// The same goes for the randomblock above.
if (variable_get('yr_verdata_multiblocks', 'off') == 'on') {
// Get the yid from the delta.
$l = $delta - 1;
// Check the cache.
$cache_id = 'yr_verdata_block_' . $l;
if ($cache = cache_get($cache_id, 'cache') && $cache->expire > time()) {
if (variable_get('yr_verdata_debug', 0) == 1) {
watchdog('yr_verdata', '@location block was retrieved from cache.', array(
'@location' => $cache_id,
), WATCHDOG_DEBUG);
}
$block['content'] = $cache->data;
}
else {
if (variable_get('yr_verdata_debug', 0) == 1) {
watchdog('yr_verdata', '@location block was outdated, starting regeneration...', array(
'@location' => $cache_id,
), WATCHDOG_DEBUG);
}
// Load the location.
$load = yr_verdata_load_location($l);
$location = $load['data'];
if ($load['status'] == TRUE) {
$name = yr_verdata_resolve_name($location);
// Unclean, but cleaned by l().
$block['subject'] = l(t('Forecast for !location', array(
'!location' => $name,
)), 'forecast/' . $location->yid);
$block['content'] = yr_verdata_generate($location, 'block') . yr_verdata_credit_link($location->url, TRUE);
// Set the cache for the block we just generated.
$maxage = variable_get('yr_verdata_maxage', 21600);
cache_set($cache_id, $output, 'cache', time() + $maxage);
}
else {
return array();
}
}
}
}
return $block;
}
}