You are here

function yr_verdata_page_single in Yr Weatherdata 6.2

Same name and namespace in other branches
  1. 7 yr_verdata.module \yr_verdata_page_single()

Function for generating a forecast page for a single location.

Parameters

$yid: The unique ID for this location in our local database. This is provided in the URL.

Return value

Returns the themed output of the forecast for the given location.

1 string reference to 'yr_verdata_page_single'
yr_verdata_menu in ./yr_verdata.module
Implementation of hook_menu()

File

./yr_verdata.module, line 179
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_single($yid) {
  if (!user_access('access content')) {
    drupal_access_denied();
  }

  // Users without content access shouldn't access yr_verdata either. Can't use hook_access, because this is not a node module.
  // 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.
  $yid = (int) $yid;
  $cache_id = 'yr_verdata_page_single_' . $yid;
  if ($yid == 0) {
    drupal_set_message(t('No location found!'));
    $output = '';
  }
  elseif ($cache = cache_get($cache_id, 'cache') && $cache->expire > time()) {
    $output = $cache->data;
    if (variable_get('yr_verdata_debug', 0) == 1) {
      watchdog('yr_verdata', '@location page was retrieved from cache.', array(
        '@location' => $cache_id,
      ), WATCHDOG_DEBUG);
    }
  }
  else {

    // No valid cache, generate new.
    $load = yr_verdata_load_location($yid);
    if (variable_get('yr_verdata_debug', 0) == 1) {
      watchdog('yr_verdata', '@location page was outdated. Starting regeneration...', array(
        '@location' => $cache_id,
      ), WATCHDOG_DEBUG);
    }
    if ($load['status'] == TRUE) {
      $output = '';
      $location = $load['data'];
      $location->information = yr_verdata_generate($location, 'information');
      $location->forecast = yr_verdata_generate($location, 'forecast');
      drupal_set_breadcrumb(array(
        l(t('Home'), '<front>'),
        l(t('Forecast'), 'forecast'),
      ));
      $name = yr_verdata_resolve_name($location);

      // Unclean, but cleaned by t() in drupal_set_title().
      drupal_set_title(t('Forecast for @location', array(
        '@location' => $name,
      )));
      $items = array(
        l(t('Overview'), NULL, array(
          'fragment' => 'yr-info',
          'external' => TRUE,
        )),
        l(t('Week-long forecast'), NULL, array(
          'fragment' => 'yr-symbol',
          'external' => TRUE,
        )),
      );

      // If we are in mainland Norway, add a radarimage. Here is the link to it.
      $url = drupal_substr($location->url, 17);
      $comps = explode('/', $url);
      if (in_array($comps[1], array(
        'Norway',
        'Norge',
        'Noreg',
        'Norga',
        'Norja',
      ))) {
        $items[] = l(t('Radar'), NULL, array(
          'fragment' => 'yr-radar',
          'external' => TRUE,
        ));
      }

      // Start the output.
      $output .= '<div id="yr-content">';

      // Nav links, or tab links if jquery_ui is available.
      $output .= theme('item_list', $items);

      // The information about the location.
      $output .= '<div id="yr-info" class="yr-page-tab">';
      $output .= $location->information;
      $output .= '</div>';

      // The symbol forecast.
      $output .= '<div id="yr-symbol" class="yr-page-tab">';
      $output .= $location->forecast;
      $output .= '</div>';

      // If we are in mainland Norway, add a radarimage. Here is the image.
      if (in_array($comps[1], array(
        'Norway',
        'Norge',
        'Noreg',
        'Norga',
        'Norja',
      ))) {
        $output .= '<div id="yr-radar" class="yr-page-tab">';
        $output .= yr_verdata_generate($location, 'radar');
        $output .= '</div>';
      }

      // Credit link.
      $output .= '<p class="yr-credit">';
      $output .= yr_verdata_credit_link($location->url, FALSE, $location->xml->credit->link['text']);
      $output .= '</p>';

      // Close the #yr-content container div.
      $output .= '</div>';

      // Set the cache.
      $maxage = variable_get('yr_verdata_maxage', 21600);
      cache_set($cache_id, $output, 'cache', time() + $maxage);
    }
  }
  return $output;
}