You are here

date_timezone.module in Date 6

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_init() {
  $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');
  }
}

/**
 * Implementation of hook_menu().
 */
function date_timezone_menu() {
  $items = array();
  $items['user/timezone'] = array(
    'title' => 'User timezone',
    'page callback' => 'user_timezone',
    'access callback' => 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, &$form_state, $form_id) {
  if ($form_id == 'system_date_time_settings') {
    date_timezone_site_form($form);
  }
  elseif ($form_id == 'user_profile_form' && variable_get('configurable_timezones', 1) && isset($form['timezone'])) {
    date_timezone_user_form($form);
  }
}

/**
 * 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) {
  $timezone = variable_get('date_default_timezone_name', NULL);
  $form['locale']['date_default_timezone'] = array(
    '#type' => 'select',
    '#title' => t('Default time zone'),
    '#default_value' => $timezone,
    '#options' => date_timezone_names(),
    '#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,
    '#element_validate' => array(
      'date_timezone_update_site',
    ),
    '#offset' => variable_get('date_default_timezone', 0),
  );
}

/**
 * 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) {
  $account = $form['_account']['#value'];
  $form['timezone']['#element_validate'] = array(
    'date_timezone_update_user',
  );
  $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 : variable_get('date_default_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.'),
  );
  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, &$form_state) {
  $timezone = $element['#value'];
  if (empty($timezone)) {
    form_set_value($element, $element['#offset'], $form_state);
  }
  else {
    variable_set('date_default_timezone_name', $timezone);
    $date = date_make_date('now', $timezone);
    form_set_value($element, date_offset_get($date), $form_state);
  }
}

/**
 * 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, &$form_state) {
  $timezone = $element['timezone_name']['#value'];
  if (!empty($timezone)) {
    $date = date_make_date('now', $timezone);
    $offset = date_offset_get($date);
    form_set_value($element['timezone'], $offset, $form_state);
  }
}

/**
 * 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', '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,
  ));
  drupal_json($timezone);
}

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

Functions

Namesort descending Description
date_timezone_cron Update the site timezone offset when cron runs.
date_timezone_form_alter Implementation of hook_form_alter().
date_timezone_init Make sure a timezone has been selected.
date_timezone_menu Implementation of hook_menu().
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.