You are here

function yr_verdata_generate in Yr Weatherdata 7

Same name and namespace in other branches
  1. 6.2 yr_verdata.module \yr_verdata_generate()

Function for generating a forecast for a location. This function should only be called after having checked if there is an up-to-date cache available, as this will load and parse an xml-file, possibly getting it from a remote host first, which is resource intensive.

Parameters

$location: The location object as loaded from the database.

$context: The context this forecast will be used in. Can be one of 'table', 'block', 'information'.

$name: The name of the location, if this is supposed to be used for viewing a block with multiple locations in it. Defaults to emtpy.

Return value

Returns marked-up forecast with text and symbols for display in the given context.

3 calls to yr_verdata_generate()
yr_verdata_block_view in ./yr_verdata.module
Implementation of hook_block_view().
yr_verdata_page_all in ./yr_verdata.module
Function for generating the main forecast page.
yr_verdata_page_single in ./yr_verdata.module
Function for generating a forecast page for a single location.

File

./yr_verdata.module, line 472
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_generate(&$location, $context, $name = '') {

  // First, check to see if the XML file is up to date. If not, get a new one.
  _yr_verdata_refresh_xml($location);

  // Load the xml for use later on.
  $data = simplexml_load_file(_yr_verdata_local_file($location));
  $location->xml = $data;

  // Set up an array to send to the theme function.
  $variables = array();
  switch ($context) {

    // Based on what context, we pick parts of the xml and prepare it for display.
    case 'table':
      $variables['symbol'] = theme('yr_verdata_symbol', array(
        'symbol' => $data->forecast->tabular->time[0]->symbol,
        'period' => $data->forecast->tabular->time[0]['period'],
      ));

      // Clean.
      $variables['wind'] = theme('yr_verdata_wind', array(
        'dir' => $data->forecast->tabular->time[0]->windDirection,
        'speed' => $data->forecast->tabular->time[0]->windSpeed,
      ));

      // Clean.
      $variables['temp'] = theme('yr_verdata_temp', array(
        'temp' => $data->forecast->tabular->time[0]->temperature,
      ));

      // Clean.
      $variables['time'] = date(_yr_verdata_date_format(), strtotime($data->forecast->tabular->time[0]['from']));

      // Clean.
      break;
    case 'information':

      // For the page, we first want a part with basic information and upcoming weather.
      if ($location->lang == 'en') {
        $trans_loctype = _yr_verdata_translatable();
        $loctype = in_array((string) $data->location->type, $trans_loctype) ? $trans_loctype[(string) $data->location->type] : t('Place');
      }
      else {
        $loctype = check_plain($data->location->type);
      }
      $variables['location'] = t('!type in @country, @alt meters above sealevel', array(
        '!type' => $loctype,
        '@country' => $data->location->country,
        '@alt' => $data->location->location['altitude'],
      ));

      // Clean. $loctype is clean because it is a string from a translation.
      $variables['lastupdate'] = t('Last updated @lastup', array(
        '@lastup' => date(_yr_verdata_date_format(), strtotime($data->meta->lastupdate)),
      ));

      // Clean.
      if (isset($data->sun['never_rise'])) {
        $variables['sun']['never_rise'] = t("Polar night, the sun doesn't rise.");
      }
      elseif (isset($data->sun['never_set'])) {
        $variables['sun']['never_set'] = t("Midnight sun, the sun doesn’t set.");
      }
      else {
        $variables['sun']['rise'] = t('Sunrise:') . ' ' . date(_yr_verdata_date_format(), strtotime($data->sun['rise']));

        // Clean.
        $variables['sun']['set'] = t('Sunset:') . ' ' . date(_yr_verdata_date_format(), strtotime($data->sun['set']));

        // Clean.
      }

      // Link to yr.no.
      $variables['links']['yr'] = l(t('Forecast for !location at yr.no', array(
        '!location' => $location->name,
      )), $location->url, array(
        'attributes' => array(
          'rel' => 'nofollow',
        ),
      ));

      // Cleaned by l().
      $variables['links']['pdf'] = l(t('Printable PDF for !location (from yr.no)', array(
        '!location' => $location->name,
      )), $location->url . 'varsel.pdf', array(
        'attributes' => array(
          'rel' => 'nofollow',
        ),
      ));

      // Cleaned by l().
      // Link to google maps. TODO Make other map services available as a choice?
      $gmaps_url = 'http://maps.google.com/maps?ie=UTF8&hl=en&z=12&ll=' . $data->location->location['latitude'] . ',' . $data->location->location['longitude'];
      $variables['links']['gmaps'] = l(t('View !location at Google Maps', array(
        '!location' => $location->name,
      )), $gmaps_url, array(
        'attributes' => array(
          'rel' => 'nofollow',
        ),
      ));

      // Cleaned by l().
      $variables['upcoming-forecast'] = yr_verdata_generate_forecastboxes($data);
      $variables['timezone'] = $data->location->timezone['id'];

      // Cleaned by t() in the theme function.
      break;
    case 'forecast':

      // And add the four next forecast periods that are available.
      $variables['longterm-forecast'] = yr_verdata_generate_forecastboxes($data, 0, 24);
      break;
    case 'radar':
      $variables = yr_verdata_radar($data);
      break;
    case 'block':
      $variables = yr_verdata_generate_forecastboxes($data, 0, 1, $name);

      // Just the first period for the block.
      $variables['yid'] = $location->yid;
      break;
  }
  return theme('yr_verdata_' . $context, $variables);
}