You are here

function weather_block_view in Weather 7.3

Same name and namespace in other branches
  1. 7 weather.module \weather_block_view()
  2. 7.2 weather.module \weather_block_view()

Implements hook_block_view().

File

./weather.module, line 311
Display current weather data from many places in the world.

Code

function weather_block_view($delta = '') {
  global $user;
  module_load_include('inc', 'weather', 'weather.common');
  $block = array();

  // Handle the 'system_NUMBER' type of blocks.
  if (strpos($delta, '_') === FALSE) {
    $display_type = $delta;
  }
  else {
    list($display_type, $display_number) = explode('_', $delta);
  }
  $forecast_days = (int) variable_get('weather_forecast_days', '2');
  switch ($display_type) {
    case 'user':
      if (weather_access_userblock()) {

        // Show the user's custom weather block, if there is already
        // a place configured. Otherwise, do not show the block.
        $places = weather_get_places_in_use('user', $user->uid);
        if (!$places) {
          return;
        }
        $block['subject'] = t('Weather');
        $weather = array();
        $display = weather_get_display_config('user', $user->uid);
        $display->detailed = FALSE;
        foreach ($places as $idx => $place) {
          $forecasts = weather_get_weather($place->place_geoid, $forecast_days, $display->detailed);
          $weather[$idx]['forecasts'] = $forecasts['forecasts'];
          $weather[$idx]['utc_offset'] = $forecasts['utc_offset'];
          $weather[$idx]['name'] = $place->displayed_name;
          $weather[$idx]['geoid'] = $place->place_geoid;
          $link = _weather_get_link_for_geoid($place->place_geoid, 'user', $user->uid);
          $weather[$idx]['link'] = l($place->displayed_name, $link);
          $link = _weather_get_link_for_geoid($place->place_geoid, 'yr.no');
          $weather[$idx]['yr.no'] = $link;
        }
        $block['content'] = theme('weather_forecast_preprocess', array(
          'weather' => $weather,
          'display' => $display,
        ));
      }
      break;
    case 'location':
      if (user_access('access content')) {

        // Set up the node location weather block.
        if (arg(0) == 'node' and is_numeric(arg(1))) {
          $node = node_load(arg(1));
          $block['subject'] = t('Weather nearby');
          $block['content'] = '';
          $display = weather_get_display_config('default');
          $display->detailed = FALSE;

          // This checks the location module.
          if (isset($node->locations)) {

            // Iterate through all available locations and check
            // for lat/long information. If there is no information,
            // the location module returns 0.0/0.0 instead of NULL values.
            foreach ($node->locations as $location) {
              if ($location['latitude'] != 0 or $location['longitude'] != 0) {
                $place = weather_get_nearest_station($location['latitude'], $location['longitude']);
                $forecasts = weather_get_weather($place->geoid, $forecast_days, $display->detailed);
                $weather[0]['forecasts'] = $forecasts['forecasts'];
                $weather[0]['utc_offset'] = $forecasts['utc_offset'];
                $weather[0]['name'] = $place->displayed_name;
                $weather[0]['geoid'] = $place->geoid;
                $link = _weather_get_link_for_geoid($place->geoid, 'default');
                $weather[0]['link'] = l($place->displayed_name, $link);
                $link = _weather_get_link_for_geoid($place->geoid, 'yr.no');
                $weather[0]['yr.no'] = $link;
                $weather[0]['station'] = array(
                  'distance' => $place->distance,
                  'bearing' => $place->bearing,
                );
                $block['content'] = theme('weather_forecast_preprocess', array(
                  'weather' => $weather,
                  'display' => $display,
                ));
              }
            }
          }
          else {

            // Get a list of all field names which are location fields.
            $location_field_names = db_select('field_config', 'fc')
              ->fields('fc', array(
              'field_name',
            ))
              ->condition('type', 'location', '=')
              ->execute()
              ->fetchCol();
            foreach ($location_field_names as $location_field_name) {
              if (isset($node->{$location_field_name})) {

                // The node has location fields, determine if there's usable data.
                // First cycle through the language codes (will mostly be 'und').
                foreach ($node->{$location_field_name} as $language) {

                  // Now cycle through the different locations.
                  foreach ($language as $location) {
                    if ($location['latitude'] != 0 or $location['longitude'] != 0) {
                      $place = weather_get_nearest_station($location['latitude'], $location['longitude']);
                      $forecasts = weather_get_weather($place->geoid, $forecast_days, $display->detailed);
                      $weather[0]['forecasts'] = $forecasts['forecasts'];
                      $weather[0]['utc_offset'] = $forecasts['utc_offset'];
                      $weather[0]['name'] = $place->displayed_name;
                      $weather[0]['geoid'] = $place->geoid;
                      $link = _weather_get_link_for_geoid($place->geoid, 'default');
                      $weather[0]['link'] = l($place->displayed_name, $link);
                      $link = _weather_get_link_for_geoid($place->geoid, 'yr.no');
                      $weather[0]['yr.no'] = $link;
                      $weather[0]['station'] = array(
                        'distance' => $place->distance,
                        'bearing' => $place->bearing,
                      );
                      $block['content'] = theme('weather_forecast_preprocess', array(
                        'weather' => $weather,
                        'display' => $display,
                      ));
                    }
                  }
                }
              }
            }
          }

          // Do not show block if no lat/long information has been found.
          if (empty($block['content'])) {
            return;
          }
        }
      }
      break;
    case 'geofield':
      if (user_access('access content')) {

        // Set up the node geofield weather block.
        if (arg(0) == 'node' and is_numeric(arg(1))) {
          $node = node_load(arg(1));
          $block['subject'] = t('Weather nearby');
          $block['content'] = '';
          $display = weather_get_display_config('default');
          $display->detailed = FALSE;

          // Get a list of all field names which are geofield fields.
          $geofield_field_names = db_select('field_config', 'fc')
            ->fields('fc', array(
            'field_name',
          ))
            ->condition('type', 'geofield', '=')
            ->execute()
            ->fetchCol();
          foreach ($geofield_field_names as $geofield_field_name) {
            if (isset($node->{$geofield_field_name})) {

              // The node has geofield fields, determine if there's usable data.
              // First cycle through the language codes (will mostly be 'und').
              foreach ($node->{$geofield_field_name} as $language) {

                // Now cycle through the different locations.
                foreach ($language as $location) {
                  if ($location['lat'] != 0 or $location['lon'] != 0) {
                    $place = weather_get_nearest_station($location['lat'], $location['lon']);
                    $forecasts = weather_get_weather($place->geoid, $forecast_days, $display->detailed);
                    $weather[0]['forecasts'] = $forecasts['forecasts'];
                    $weather[0]['utc_offset'] = $forecasts['utc_offset'];
                    $weather[0]['name'] = $place->displayed_name;
                    $weather[0]['geoid'] = $place->geoid;
                    $link = _weather_get_link_for_geoid($place->geoid, 'default');
                    $weather[0]['link'] = l($place->displayed_name, $link);
                    $link = _weather_get_link_for_geoid($place->geoid, 'yr.no');
                    $weather[0]['yr.no'] = $link;
                    $weather[0]['station'] = array(
                      'distance' => $place->distance,
                      'bearing' => $place->bearing,
                    );
                    $block['content'] = theme('weather_forecast_preprocess', array(
                      'weather' => $weather,
                      'display' => $display,
                    ));
                  }
                }
              }
            }
          }

          // Do not show block if no lat/long information has been found.
          if (empty($block['content'])) {
            return;
          }
        }
      }
      break;
    case 'geofieldtaxonomyterms':
      if (user_access('access content')) {

        // Set up the taxonomy terms geofield weather block.
        if (arg(0) == 'taxonomy' and arg(1) == 'term') {
          $tid = (int) arg(2);
          $term = taxonomy_term_load($tid);
          $block['subject'] = t('Weather nearby');
          $block['content'] = '';
          $display = weather_get_display_config('default');
          $display->detailed = FALSE;

          // Get a list of all field names which are geofield fields.
          $geofield_field_names = db_select('field_config', 'fc')
            ->fields('fc', array(
            'field_name',
          ))
            ->condition('type', 'geofield', '=')
            ->execute()
            ->fetchCol();
          foreach ($geofield_field_names as $geofield_field_name) {
            if (isset($term->{$geofield_field_name})) {

              // The term has geofield fields, determine if there's usable data.
              // First cycle through the language codes (will mostly be 'und').
              foreach ($term->{$geofield_field_name} as $language) {

                // Now cycle through the different locations.
                foreach ($language as $location) {
                  if ($location['lat'] != 0 or $location['lon'] != 0) {
                    $place = weather_get_nearest_station($location['lat'], $location['lon']);
                    $forecasts = weather_get_weather($place->geoid, $forecast_days, $display->detailed);
                    $weather[0]['forecasts'] = $forecasts['forecasts'];
                    $weather[0]['utc_offset'] = $forecasts['utc_offset'];
                    $weather[0]['name'] = $place->displayed_name;
                    $weather[0]['geoid'] = $place->geoid;
                    $link = _weather_get_link_for_geoid($place->geoid, 'default');
                    $weather[0]['link'] = l($place->displayed_name, $link);
                    $link = _weather_get_link_for_geoid($place->geoid, 'yr.no');
                    $weather[0]['yr.no'] = $link;
                    $weather[0]['station'] = array(
                      'distance' => $place->distance,
                      'bearing' => $place->bearing,
                    );
                    $block['content'] = theme('weather_forecast_preprocess', array(
                      'weather' => $weather,
                      'display' => $display,
                    ));
                  }
                }
              }
            }
          }

          // Do not show block if no lat/long information has been found.
          if (empty($block['content'])) {
            return;
          }
        }
      }
      break;
    case 'ip':
      if (user_access('access content') and module_exists('smart_ip')) {
        $smart_ip_session = smart_ip_session_get('smart_ip');

        // Check that the lookup did find a location for the IP.
        if (isset($smart_ip_session['location'])) {
          $latitude = $smart_ip_session['location']['latitude'];
          $longitude = $smart_ip_session['location']['longitude'];
          if (!empty($latitude) and !empty($longitude)) {
            $block['subject'] = t('Weather near you');
            $display = weather_get_display_config('default');
            $display->detailed = FALSE;
            $place = weather_get_nearest_station($latitude, $longitude);
            $forecasts = weather_get_weather($place->geoid, $forecast_days, $display->detailed);
            $weather[0]['forecasts'] = $forecasts['forecasts'];
            $weather[0]['utc_offset'] = $forecasts['utc_offset'];
            $weather[0]['name'] = $place->displayed_name;
            $weather[0]['geoid'] = $place->geoid;
            $link = _weather_get_link_for_geoid($place->geoid, 'default');
            $weather[0]['link'] = l($place->displayed_name, $link);
            $link = _weather_get_link_for_geoid($place->geoid, 'yr.no');
            $weather[0]['yr.no'] = $link;
            $weather[0]['station'] = array(
              'distance' => $place->distance,
              'bearing' => $place->bearing,
            );
            $block['content'] = theme('weather_forecast_preprocess', array(
              'weather' => $weather,
              'display' => $display,
            ));
          }
        }
      }
      break;
    case 'system':
      if (user_access('access content')) {

        // Show a system-wide weather display.
        $block['subject'] = t('Weather');
        $weather = array();
        $display = weather_get_display_config('system-wide', $display_number);
        $display->detailed = FALSE;
        $places = weather_get_places_in_use('system-wide', $display_number);
        if (!$places) {
          return;
        }
        foreach ($places as $idx => $place) {
          $forecasts = weather_get_weather($place->place_geoid, $forecast_days, $display->detailed);
          $weather[$idx]['forecasts'] = $forecasts['forecasts'];
          $weather[$idx]['utc_offset'] = $forecasts['utc_offset'];
          $weather[$idx]['name'] = $place->displayed_name;
          $weather[$idx]['geoid'] = $place->place_geoid;
          $link = _weather_get_link_for_geoid($place->place_geoid, 'system-wide', $display_number);
          $weather[$idx]['link'] = l($place->displayed_name, $link);
          $link = _weather_get_link_for_geoid($place->place_geoid, 'yr.no');
          $weather[$idx]['yr.no'] = $link;
        }
        $block['content'] = theme('weather_forecast_preprocess', array(
          'weather' => $weather,
          'display' => $display,
        ));
      }
  }
  return $block;
}