You are here

function yr_verdata_page_all in Yr Weatherdata 6.2

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

Function for generating the main forecast page.

Return value

Returns a themed list of locations with upcoming forecasts.

1 string reference to 'yr_verdata_page_all'
yr_verdata_menu in ./yr_verdata.module
Implementation of hook_menu()

File

./yr_verdata.module, line 117
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_page_all() {
  if (!user_access('access content')) {
    drupal_access_denied();
  }

  // Users without content access shouldn't access yr_verdata either. Can't use hook_access, because this is not a node module.
  // First, we see if we have a cache. No need to ask the database,
  // filesystem, PHP SimpleXML and yr.no for lots of locations if
  // the cache is up to date.
  if ($cache = cache_get('yr_verdata_page_all', 'cache') && $cache->expire > time()) {
    $output = $cache->data;
    if (variable_get('yr_verdata_debug', 0) == 1) {
      watchdog('yr_verdata', 'The page for all locations was retrieved from cache.', array(), WATCHDOG_DEBUG);
    }
  }
  else {

    // No valid cache, generate new.
    if (variable_get('yr_verdata_debug', 0) == 1) {
      watchdog('yr_verdata', 'The page for all locations was outdated. Starting regeneration.', array(), WATCHDOG_DEBUG);
    }
    $header = array(
      t('Location'),
      t('Forecast'),
    );
    $rows = array();
    $empty = t('No locations are stored in the database.');

    // Add some more stuff for admins.
    if (user_access('administer yr_verdata')) {
      $header[] = t('Delete location');
      $empty .= ' ' . _yr_verdata_addmore_msg();
    }

    // Load locations.
    $grouping = variable_get('yr_verdata_group', 'off');

    // Are we grouping locations in some way?
    $records = _yr_verdata_get_all();
    $langs = _yr_verdata_langs();
    $last_group = '';
    if (count($records) > 0) {
      foreach ($records as $record) {

        // Go over the array of locations and generate a row for use in theme_table().
        // If we are grouping by something, add pretty "header" rows for each group.
        if ($grouping != 'off' && $record->{$grouping} != $last_group) {
          $colspan = user_access('administer yr_verdata') ? 3 : 2;
          $thegroup = $grouping == 'lang' ? $langs[$record->lang] : check_plain($record->{$grouping});
          $rows[$thegroup] = array(
            'data' => array(
              array(
                'data' => $thegroup,
                'header' => TRUE,
                'colspan' => $colspan,
              ),
            ),
          );
          $last_group = $record->{$grouping};
        }
        $name = yr_verdata_resolve_name($record);

        // Unclean, but cleaned by l().
        $rows[$record->yid] = array(
          l($name, "forecast/{$record->yid}"),
          yr_verdata_generate($record, 'table'),
        );

        // yid is a serial from the database, so it's safe.
        if (user_access('administer yr_verdata')) {
          $rows[$record->yid][] = l(t('Delete'), 'forecast/delete/' . $record->yid);
        }
      }
      $output = theme('table', $header, $rows);
      $npp = variable_get('yr_verdata_npp', 0);
      if ($npp > 0) {
        $output .= theme('pager', array(), $npp, 0, array(), 5);
      }
      $output .= yr_verdata_credit_link();
    }
    else {
      $output = '';
      drupal_set_message($empty, 'warning');
    }
    $maxage = variable_get('yr_verdata_maxage', 21600);
    cache_set('yr_verdata_page_all', $output, 'cache', time() + $maxage);
  }
  return $output;
}