You are here

function weather_block_view in Weather 7

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

Implement hook_block_view().

File

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

Code

function weather_block_view($delta = '') {
  global $user;
  $block = array();

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

        // Show the user's custom weather block, if there is already
        // a location configured. Otherwise, do not show the block.
        $locations = weather_get_locations_in_use('user', $user->uid);
        if (empty($locations)) {
          return;
        }
        $block['subject'] = t('Current weather');
        $block['content'] = '';
        $display = weather_get_display_settings('user', $user->uid);
        foreach ($locations as $location_row) {
          $location = weather_get_location_settings($location_row->id);
          $metar = weather_get_metar($location_row->icao);
          $block['content'] .= theme('weather_theming', array(
            'display' => $display,
            'location' => $location,
            'metar' => $metar,
          ));
        }
      }
      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('Current weather nearby');
          $block['content'] = '';
          $display = weather_get_display_settings('default', 1);

          // 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) {
                $station = weather_get_nearest_station($location['latitude'], $location['longitude']);
                $metar = weather_get_metar($station->icao);
                $block['content'] .= theme('weather_theming', array(
                  'display' => $display,
                  'location' => $station,
                  'metar' => $metar,
                ));
              }
            }
          }
          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) {
                      $station = weather_get_nearest_station($location['latitude'], $location['longitude']);
                      $metar = weather_get_metar($station->icao);
                      $block['content'] .= theme('weather_theming', array(
                        'display' => $display,
                        'location' => $station,
                        'metar' => $metar,
                      ));
                    }
                  }
                }
              }
            }
          }

          // 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('Current weather nearby');
          $block['content'] = '';
          $display = weather_get_display_settings('default', 1);

          // 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) {
                    $station = weather_get_nearest_station($location['lat'], $location['lon']);
                    $metar = weather_get_metar($station->icao);
                    $block['content'] .= theme('weather_theming', array(
                      'display' => $display,
                      'location' => $station,
                      'metar' => $metar,
                    ));
                  }
                }
              }
            }
          }

          // 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')) {
        $result = smart_ip_get_location(ip_address());

        // Check that the lookup did find a location for the IP.
        if ($result != FALSE) {
          $latitude = $result['latitude'];
          $longitude = $result['longitude'];
          if (!empty($latitude) and !empty($longitude)) {
            $block['subject'] = t('Current weather near you');
            $display = weather_get_display_settings('default', 1);
            $station = weather_get_nearest_station($latitude, $longitude);
            $metar = weather_get_metar($station->icao);
            $block['content'] = theme('weather_theming', array(
              'display' => $display,
              'location' => $station,
              'metar' => $metar,
            ));
          }
        }
      }
      break;
    case 'system':
      if (user_access('access content')) {

        // Show a system-wide weather display.
        $block['subject'] = t('Current weather');
        $block['content'] = '';
        $display = weather_get_display_settings('system-wide', $display_number);
        $locations = weather_get_locations_in_use('system-wide', $display_number);
        foreach ($locations as $location_row) {
          $location = weather_get_location_settings($location_row->id);
          $metar = weather_get_metar($location->icao);
          $block['content'] .= theme('weather_theming', array(
            'display' => $display,
            'location' => $location,
            'metar' => $metar,
          ));
        }
        if (empty($block['content'])) {
          $block['content'] = t('There is no location configured yet.');
        }
      }
  }
  return $block;
}