You are here

date_timezone.module in Date 5.2

This module will make the alter the user and site timezone forms to select a timezone name instead of a timezone offset.

This module won't be needed once core starts tracking timezone names instead of offsets.

File

date_timezone/date_timezone.module
View source
<?php

/**
 * @file
 * This module will make the alter the user and site timezone forms to
 * select a timezone name instead of a timezone offset.
 *
 * This module won't be needed once core starts tracking timezone names
 * instead of offsets.
 */

/**
 * Make sure a timezone has been selected.
 */
function date_timezone_menu($may_cache) {
  global $user;
  $items = array();
  if (!$may_cache) {
    $tz_name = variable_get('date_default_timezone_name', NULL);
    if (!empty($user->uid) && $_GET['q'] != 'admin/settings/date-time' && empty($tz_name)) {
      drupal_set_message(t('The Date Timezone module requires you to !link.', array(
        '!link' => l(t('set the site timezone name'), 'admin/settings/date-time'),
      )), 'error');
    }
    $items[] = array(
      'path' => 'user/timezone',
      'title' => 'User timezone',
      'callback' => 'user_timezone',
      'access' => TRUE,
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}

/**
 * Implementation of hook_form_alter().
 *
 * Override system handling of user and site timezone selection.
 */
function date_timezone_form_alter($form_id, &$form) {
  if ($form_id == 'system_date_time_settings') {
    $form['#process'] = array(
      'date_timezone_site_form' => array(),
    );
  }
  elseif ($form_id == 'user_edit' && variable_get('configurable_timezones', 1) && isset($form['timezone'])) {
    $form['#process'] = array(
      'date_timezone_user_form' => array(),
    );
  }
}

/**
 * Override form for the site timezone settings form.
 * Display a list of timezone names instead of offsets
 * and hide the offset value.
 */
function date_timezone_site_form(&$form) {
  drupal_add_js(drupal_get_path('module', 'date_timezone') . '/date_timezone.js');
  $timezone = variable_get('date_default_timezone_name', NULL);
  $form['date_default_timezone'] = array(
    '#type' => 'select',
    '#title' => t('Default time zone'),
    '#default_value' => $timezone,
    '#options' => date_timezone_names(FALSE, TRUE),
    // Force an update before setting a site default.
    '#description' => t('Select the default site time zone. If in doubt, choose the timezone that is closest to your location which has the same rules for daylight saving time.'),
    '#weight' => -10,
    '#validate' => array(
      'date_timezone_update_site' => array(),
    ),
    '#offset' => variable_get('date_default_timezone', 0),
  );

  // Add the JavaScript callback to automatically set the timezone.
  if (empty($timezone)) {
    drupal_add_js('
// Global Killswitch
if (Drupal.jsEnabled) {
  $(document).ready(function() {
    Drupal.setDefaultTimezone();
  });
}', 'inline');
  }
  return $form;
}

/**
 * Override form for the user timezone settings form.
 * Display a list of timezone names instead of offsets
 * and hide the offset value.
 */
function date_timezone_user_form(&$form) {
  drupal_add_js(drupal_get_path('module', 'date_timezone') . '/date_timezone.js');
  $account = $form['_account']['#value'];
  $form['timezone']['#validate'] = array(
    'date_timezone_update_user' => array(),
  );
  $form['timezone']['#uid'] = $account->uid;
  $form['timezone']['timezone']['#type'] = 'hidden';
  $form['timezone']['timezone']['#value'] = $form['timezone']['timezone']['#default_value'];
  $timezone = $account->timezone_name ? $account->timezone_name : NULL;
  $form['timezone']['timezone_name'] = array(
    '#type' => 'select',
    '#title' => t('Default time zone'),
    '#default_value' => $timezone,
    '#options' => date_timezone_names(),
    '#description' => t('Select your current local time.  If in doubt, choose the timezone that is closest to your location which has the same rules for daylight saving time. Dates and times throughout this site will be displayed using this time zone.'),
  );

  // Add the JavaScript callback to automatically set the timezone.
  if (empty($timezone)) {
    drupal_add_js('
// Global Killswitch
if (Drupal.jsEnabled) {
  $(document).ready(function() {
    Drupal.setDefaultTimezone();
  });
}', 'inline');
  }
  return $form;
}

/**
 * Callback from site timezone settings form to update site timezone info.
 * When the timezone name is updated, update the offset as well.
 */
function date_timezone_update_site($element) {
  $timezone = $element['#value'];
  if (empty($timezone)) {
    $offset = $element['#offset'];
  }
  else {
    variable_set('date_default_timezone_name', $timezone);
    $date = date_now($timezone);
    $offset = date_offset_get($date);
  }

  // We overrode the event module's form handling, give it back
  // the values it expected to find in its submission handler.
  if (module_exists('event') && db_table_exists('event_timezones')) {
    $event_zone = date_event_zonelist_by_name(str_replace('_', ' ', $timezone));

    // The Event module will set its own variable.
    if (!empty($event_zone['timezone'])) {
      form_set_value($element, $event_zone['timezone'] . '|' . $offset);
    }
    else {
      form_set_value($element, $offset);
    }
  }
  else {
    form_set_value($element, $offset);
  }
}

/**
 * Callback from user timezone settings form to update user timezone info.
 * When the timezone name is updated, update the offset as well.
 */
function date_timezone_update_user($element) {
  $timezone = $element['timezone_name']['#value'];
  if (!empty($timezone)) {
    $date = date_now($timezone);
    $offset = date_offset_get($date);
  }
  else {
    $offset = NULL;
  }

  // We overrode the event module's form handling, give it back
  // the values it expected to find in its submission handler.
  if (module_exists('event') && db_table_exists('event_timezones')) {
    $event_zone = date_event_zonelist_by_name(str_replace('_', ' ', $timezone));

    // The Event module will update the user record with the timezone id.
    if (!empty($event_zone['timezone'])) {
      form_set_value($element['timezone'], $event_zone['timezone'] . '|' . $offset);
    }
    else {
      form_set_value($element['timezone'], $offset);
    }
  }
  else {
    form_set_value($element['timezone'], $offset);
  }
}

/**
 * Update the site timezone offset when cron runs.
 * 
 * This is to make sure that modules that rely on the timezone offset
 * have current information to process.
 */
function date_timezone_cron() {
  $date = date_now(variable_get('date_default_timezone_name', NULL));
  $offset = date_offset_get($date);
  if ($offset != variable_get('date_default_timezone', 0)) {
    variable_set('date_default_timezone', $offset);
  }
}

/**
 * Update user timezone information at login.
 * 
 * This is to make sure that modules that rely on the timezone offset
 * have current information to process.
 */
function date_timezone_user($op, &$edit, &$account, $category = NULL) {
  if ($account->uid && $op == 'login' && variable_get('configurable_timezones', 1)) {
    if (strlen($account->timezone_name)) {
      $date = date_now($account->timezone_name);
      $offset = date_offset_get($date);
      if ($offset != $account->timezone) {
        $account->timezone = $offset;
        db_query("UPDATE {users} SET timezone='%s' WHERE uid = %d", $offset, $account->uid);
      }
    }
    else {

      // If the user doesn't already have a timezone name selected,
      // default it to the site timezone name and offset.
      $timezone = variable_get('date_default_timezone_name', NULL);
      if (!empty($timezone)) {
        $date = date_now($timezone);
        $offset = date_offset_get($date);
        db_query("UPDATE {users} SET timezone_name = '%s', timezone='%s' WHERE uid = %d", $timezone, $offset, $account->uid);
      }
    }
  }
}

/**
 * Menu callback; Retrieve a JSON object containing a suggested time 
 * zone name.
 */
function user_timezone($abbreviation = '', $offset = -1, $is_daylight_saving_time = NULL) {

  // An abbreviation of "0" passed in the callback arguments should be
  // interpreted as the empty string.
  $abbreviation = $abbreviation ? $abbreviation : '';
  $timezone = timezone_name_from_abbr($abbreviation, intval($offset), $is_daylight_saving_time);

  // The client date is passed in for debugging purposes.
  $date = isset($_GET['date']) ? $_GET['date'] : '';

  // Log a debug message.
  watchdog('timezone', t('Detected time zone: %timezone; client date: %date; abbreviation: %abbreviation; offset: %offset; daylight saving time: %is_daylight_saving_time.', array(
    '%timezone' => $timezone,
    '%date' => $date,
    '%abbreviation' => $abbreviation,
    '%offset' => $offset,
    '%is_daylight_saving_time' => $is_daylight_saving_time,
  )));
  date_api_json($timezone);
}

/**
 * Create replacement values for deprecated timezone names.
 */
function date_timezone_replacement($old) {
  require_once './' . drupal_get_path('module', 'date_timezone') . '/date_timezone.install';
  return _date_timezone_replacement($old);
}

/**
 * Helper function to update Event module timezone information.
 */
function date_event_zonelist_by_name($name) {
  if (!module_exists('event') || !db_table_exists('event_timezones')) {
    return array();
  }
  static $zone_names = array();
  if (!isset($zone_names[$name])) {
    $zone = db_fetch_array(db_query("SELECT * FROM {event_timezones} WHERE name = '%s'", $name));
    $zone_names[$name] = $zone;
  }
  return $zone_names[$name];
}

Functions

Namesort descending Description
date_event_zonelist_by_name Helper function to update Event module timezone information.
date_timezone_cron Update the site timezone offset when cron runs.
date_timezone_form_alter Implementation of hook_form_alter().
date_timezone_menu Make sure a timezone has been selected.
date_timezone_replacement Create replacement values for deprecated timezone names.
date_timezone_site_form Override form for the site timezone settings form. Display a list of timezone names instead of offsets and hide the offset value.
date_timezone_update_site Callback from site timezone settings form to update site timezone info. When the timezone name is updated, update the offset as well.
date_timezone_update_user Callback from user timezone settings form to update user timezone info. When the timezone name is updated, update the offset as well.
date_timezone_user Update user timezone information at login.
date_timezone_user_form Override form for the user timezone settings form. Display a list of timezone names instead of offsets and hide the offset value.
user_timezone Menu callback; Retrieve a JSON object containing a suggested time zone name.