You are here

function yr_verdata_generate_forecastboxes in Yr Weatherdata 6.2

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

Function for generating one or more forecast boxes for given periods.

Parameters

$data: The whole loaded xml.

$start: Which number in the array to start from. Defaults to 0, which is the first.

$num: The number of boxes we want presented, starting at the first time period available in the xml. Defaults to 4, as this normally represents a total of 24 hours.

$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 an array of themed forecast boxes.

1 call to yr_verdata_generate_forecastboxes()
yr_verdata_generate in ./yr_verdata.module
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…

File

./yr_verdata.module, line 511
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_forecastboxes($data, $start = 0, $num = 4, $name = '') {
  $key = 0;
  $boxes = array();
  $periods = $data->forecast->tabular->time;
  $last = 0;
  foreach ($periods as $forecast) {
    if ($num - $key == 1) {
      $boxes[$key]['last'] = TRUE;
    }
    if ($key == $num) {
      break;
    }

    // Stop the loop once we hit the desired number of boxes.
    if ($start > $key) {

      // This enables the ability to skip the first $start periods.
      $key++;
      continue;
    }
    $boxes[$key]['time'] = date(_yr_verdata_date_format(), strtotime($forecast['from']));

    // Clean.
    $boxes[$key]['symbol'] = theme('yr_verdata_symbol', array(
      'symbol' => $forecast->symbol,
      'period' => $forecast['period'],
    ));

    // Clean.
    $boxes[$key]['wind'] = theme('yr_verdata_wind', array(
      'dir' => $forecast->windDirection,
      'speed' => $forecast->windSpeed,
    ));

    // Clean.
    $boxes[$key]['temp'] = theme('yr_verdata_temp', $forecast->temperature);

    // Clean.
    $boxes[$key]['precip'] = theme('yr_verdata_precip', $forecast->precipitation);

    // Clean.
    $boxes[$key]['pressure'] = theme('yr_verdata_pressure', array(
      'pressure' => $forecast->pressure,
    ));

    // Clean.
    $boxes[$key]['period'] = check_plain($forecast['period']);

    // This is used for adding markup in the long-term forecast.
    if ($boxes[$key]['period'] == 0) {
      $boxes[$key]['day'] = format_date(strtotime($forecast['to']), 'custom', 'l', NULL, NULL);
    }

    // Add the dayname. We use $forecast['to'] to avoid any wrong daynames because of potential timezone issues.
    if ($last == 3 && $boxes[$key]['period'] == 2) {

      // For the last days, which are just one box per day.
      $boxes[$key]['day'] = t('Following days');
      $boxes[$key]['following'] = TRUE;
    }
    $last = $boxes[$key]['period'];
    $key++;
  }
  if (!empty($name)) {
    $boxes['name'] = check_plain($name);
  }
  return $boxes;
}