You are here

function yr_verdata_page_single in Yr Weatherdata 7

Same name and namespace in other branches
  1. 6.2 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 217
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) {
  $page_array = array(
    '#cache' => array(
      'keys' => array(
        'yr_verdata',
        'page',
        'single',
        $yid,
      ),
      'bin' => 'cache',
      'expire' => REQUEST_TIME + variable_get('yr_verdata_maxage', 21600),
    ),
  );
  $cid = drupal_render_cid_create($page_array);
  $cache = cache_get($cid);
  if (!is_object($cache)) {
    $cache = new stdClass();
  }

  // To prevent strict warning.
  $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 = yr_verdata_load_location($yid);
    if ($load['status'] == TRUE) {
      $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 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,
        )),
      );
      $page_array = array(
        'information' => array(
          '#markup' => $location->information,
          '#prefix' => '<div id="yr-info" class="yr-page-tab">',
          '#suffix' => '</div>',
          '#weight' => 1,
        ),
        'forecast' => array(
          '#markup' => $location->forecast,
          '#prefix' => '<div id="yr-symbol" class="yr-page-tab">',
          '#suffix' => '</div>',
          '#weight' => 2,
        ),
        'credit' => array(
          '#markup' => yr_verdata_credit_link($location->url, FALSE, $location->xml->credit->link['text']),
          '#prefix' => '<p class="yr-credit">',
          '#suffix' => '</p>',
          '#weight' => 4,
        ),
      );

      // If we are in mainland Norway, add a radarimage.
      $url = drupal_substr($location->url, 17);
      $comps = explode('/', $url);
      if (in_array($comps[1], array(
        'Norway',
        'Norge',
        'Noreg',
        'Norga',
        'Norja',
      ))) {
        $page_array['radar'] = array(
          '#markup' => yr_verdata_generate($location, 'radar'),
          '#prefix' => '<div id="yr-radar" class="yr-page-tab">',
          '#suffix' => '</div></div>',
          '#weight' => 3,
        );
        $items[] = l(t('Radar'), NULL, array(
          'fragment' => 'yr-radar',
          'external' => TRUE,
        ));
      }
      else {
        $page_array['forecast']['#suffix'] = '</div></div>';
      }

      // Add the links for the tabs.
      $page_array['tabs'] = array(
        '#theme' => 'item_list',
        '#items' => $items,
        '#prefix' => '<div id="yr-content">',
        '#weight' => 0,
      );

      // Add the stylesheet and js.
      $page_array['#attached']['css'] = array(
        drupal_get_path('module', 'yr_verdata') . '/yr_verdata.css',
      );
      drupal_add_js(drupal_get_path('module', 'yr_verdata') . '/yr_verdata.js', array(
        'type' => 'file',
        'scope' => 'footer',
      ));
      drupal_add_library('system', 'ui.tabs');
    }
    else {
      return FALSE;
    }
  }
  return $page_array;
}