You are here

function wunderground_weather_get_current in Wunderground weather 7

Get current weather conditions and return them.

Return value

string Html for the current conditions block.

1 call to wunderground_weather_get_current()
wunderground_weather_block_view in ./wunderground_weather.module
Implements hook_block_view().

File

./wunderground_weather.module, line 532
Wunderground weather module to display weather forecasts and current weather conditions in blocks.

Code

function wunderground_weather_get_current($block_number) {

  // Get data from cache or Wunderground directly.
  $weather = _wunderground_weather_cache('current', $block_number);
  $degrees = variable_get('wunderground_weather_degrees', 'celsius');
  $degrees_key = $degrees == 'celsius' ? 'c' : 'f';
  $suffix = strtoupper($degrees_key);

  // Check if data is received.
  if ($weather['current_observation']) {

    // Calculate windspeed.
    $windspeed_unit = variable_get('wunderground_weather_windspeed', 'kph');
    switch ($windspeed_unit) {
      case 'mph':
        $windspeed = $weather['current_observation']['wind_mph'] . ' ' . t('mph');
        break;
      case 'bft':
        $windspeed = _wunderground_weather_speed_to_beaufort($weather['current_observation']['wind_kph'], 'kph') . ' ' . t('bft');
        break;
      default:
        $windspeed = $weather['current_observation']['wind_kph'] . ' ' . t('km/h');
        break;
    }
    $unit = variable_get('wunderground_weather_distance', 'km');
    $distance = $weather['current_observation']['visibility_' . $unit];
    $visibility = $distance == 'N/A' ? $distance : $distance . ' ' . $unit;

    // Build list items.
    $items = array();
    $serialized_fields = variable_get('wunderground_weather_current_' . $block_number . '_fields');
    $block_fields = unserialize($serialized_fields);
    foreach (element_children($block_fields, TRUE) as $field) {
      if ($block_fields[$field]['#enabled'] == 1) {
        switch ($field) {
          case 'weather':
            $items[$field] = $weather['current_observation']['weather'];
            break;
          case 'temperature':
            $items[$field] = t('Temperature: !temp°', array(
              '!temp' => $weather['current_observation']['temp_' . $degrees_key],
            )) . $suffix;
            break;
          case 'feels_like':
            $items[$field] = t('Feels like: !temp°', array(
              '!temp' => $weather['current_observation']['feelslike_' . $degrees_key],
            )) . $suffix;
            break;
          case 'wind':
            $items[$field] = t('Wind') . ': ' . $windspeed;
            break;
          case 'visibility':
            $items[$field] = t('Visibility') . ': ' . $visibility;
            break;
          case 'uv':
            $items[$field] = t('UV index') . ': ' . $weather['current_observation']['UV'];
            break;
        }
      }
    }

    // Get an unorderd list.
    $list = array(
      'title' => '',
      'type' => 'ul',
      'items' => $items,
      'attributes' => array(
        'class' => array(
          'current-weather-summary',
        ),
      ),
    );
    $summary = theme('item_list', $list);

    // Get the weather icon.
    $icon = $weather['current_observation']['icon'];
    $icon_set = variable_get('wunderground_weather_current_icons_' . $block_number, 'a');
    $icon_url = _wunderground_weather_get_icon_url($icon, $icon_set);
    $variables = array(
      'image' => theme('image', array(
        'path' => $icon_url,
        'alt' => t('Weather in !city', array(
          '!city' => variable_get('wunderground_weather_screen', ''),
        )),
        'title' => t('Weather in !city', array(
          '!city' => variable_get('wunderground_weather_screen', ''),
        )),
        'attributes' => array(),
      )),
      'summary' => $summary,
    );
    $output = theme('wunderground_weather_current', $variables);
  }
  else {

    // Return message if no data is retrieved.
    $output = t('No current weather data available.');
    if (user_access('administer wunderground weather')) {
      $configure_path = 'admin/structure/block/manage/wunderground_weather/wunderground_weather_current_' . $block_number . '/configure';
      $output .= ' ' . l(t('Configure this block'), $configure_path);
    }
  }
  return $output;
}