You are here

function weather_views_pre_execute in Weather 7.3

Same name and namespace in other branches
  1. 7.2 weather.views.inc \weather_views_pre_execute()

Implements hook_views_pre_execute().

File

./weather.views.inc, line 205
Views integration for weather module.

Code

function weather_views_pre_execute(&$view) {

  // Check if the view uses the weather places table.
  if ($view->base_table == 'weather_places') {

    // Clone the current query for modification.
    $current_query =& $view->build_info['query'];
    $new_query = clone $current_query;

    // Ensure that the geoid is included in the query. If it was
    // included already, this alias will be changed to weather_places_geoid_2.
    $new_query
      ->addField('weather_places', 'geoid', 'weather_places_geoid');
    $result = $new_query
      ->execute();
    $geoids = array();
    foreach ($result as $row) {
      $geoids[] = $row->weather_places_geoid;
    }

    // Reduce the geoids to unique values.
    $geoids = array_unique($geoids);
    sort($geoids);

    // Get all geoids with current forecasts (no download needed).
    $time = variable_get('weather_time_for_testing', REQUEST_TIME);
    $current_utc_time = gmdate('Y-m-d H:i:s', $time);
    $current_forecasts = db_query('SELECT * FROM {weather_forecast_information} WHERE geoid IN (:geoids) AND next_download_attempt > :current_utc_time', array(
      ':geoids' => $geoids,
      ':current_utc_time' => $current_utc_time,
    ));

    // Construct an array with geoids with current forecasts.
    $no_download_needed = array();
    foreach ($current_forecasts as $current_forecast) {
      $no_download_needed[] = $current_forecast->geoid;
    }

    // Get all geoids which need a download.
    $need_downloads = array_diff($geoids, $no_download_needed);
    module_load_include('inc', 'weather', 'weather.common');

    // Use at most three download attempts.
    $download_counter = 0;
    foreach ($need_downloads as $geoid) {
      weather_get_weather($geoid);
      $download_counter++;
      if ($download_counter >= 3) {
        break;
      }
    }
  }
}