[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/date/date_timezone/ -> date_timezone.module (source)

   1  <?php
   2  // $Id: date_timezone.module,v 1.18.2.3.2.13 2010/08/12 21:59:07 karens Exp $
   3  /**
   4   * @file
   5   * This module will make the alter the user and site timezone forms to
   6   * select a timezone name instead of a timezone offset.
   7   *
   8   * This module won't be needed once core starts tracking timezone names
   9   * instead of offsets.
  10   */
  11  
  12  /**
  13   * Make sure a timezone has been selected.
  14   */
  15  function date_timezone_init() {
  16    $tz_name = variable_get('date_default_timezone_name', NULL);
  17    if (!empty($user->uid) && $_GET['q'] != 'admin/settings/date-time' && empty($tz_name)) {
  18      drupal_set_message(t('The Date Timezone module requires you to <a href="@link">set the site timezone name</a>.', array('@link' => url('admin/settings/date-time'))), 'error');
  19    }
  20  }
  21  
  22  /**
  23   * Implementation of hook_menu().
  24   */
  25  function date_timezone_menu() {
  26    $items = array();
  27    $items['user/timezone'] = array(
  28      'title' => 'User timezone',
  29      'page callback' => 'user_timezone',
  30      'access callback' => TRUE,
  31      'type' => MENU_CALLBACK,
  32    );
  33    return $items;
  34  }
  35  
  36  /**
  37   * Implementation of hook_form_alter().
  38   *
  39   * Override system handling of user and site timezone selection.
  40   */
  41  function date_timezone_form_alter(&$form, &$form_state, $form_id) {
  42    if ($form_id == 'system_date_time_settings') {
  43      date_timezone_site_form($form);
  44      if (!isset($form['#after_build'])) {
  45        $form['#after_build'] = array();
  46      }
  47      $form['#after_build'][] = 'date_timezone_site_form_after_build';
  48    }
  49    elseif ($form_id == 'user_profile_form' && variable_get('configurable_timezones', 1) && isset($form['timezone'])) {
  50      date_timezone_user_form($form);
  51      if (!isset($form['#after_build'])) {
  52        $form['#after_build'] = array();
  53      }
  54      $form['#after_build'][] = 'date_timezone_user_form_after_build';  
  55    }
  56  }
  57  
  58  /**
  59   * Create a form for the site timezone names.
  60   * Display a list of timezone names instead of offsets.
  61   */
  62  function date_timezone_site_form(&$form) {
  63    drupal_add_js(drupal_get_path('module', 'date_timezone') .'/date_timezone.js');
  64    $form['locale']['#element_validate'] = array('date_timezone_update_site');
  65  
  66    $timezone = variable_get('date_default_timezone_name', NULL);
  67    $form['locale']['date_default_timezone_name'] = array(
  68      '#type' => 'select',
  69      '#title' => t('Default time zone'),
  70      '#default_value' => $timezone,
  71      '#options' => date_timezone_names(FALSE, TRUE), // Force an update before setting a site default.
  72      '#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.'),
  73      '#weight' => -10,
  74      '#offset' => variable_get('date_default_timezone', 0),
  75    );
  76    // Add the JavaScript callback to automatically set the timezone.
  77    if (empty($timezone)) {
  78      drupal_add_js('
  79  // Global Killswitch
  80  if (Drupal.jsEnabled) {
  81    $(document).ready(function() {
  82      Drupal.setDefaultTimezone();
  83    });
  84  }', 'inline');
  85    } 
  86    return $form;
  87  }
  88  
  89  /**
  90   * Hide the original form.
  91   * 
  92   * We have to do this in after_build in case the Event module
  93   * is enabled since the Event module will do its form_alter()
  94   * after the Date module.
  95   */
  96  function date_timezone_site_form_after_build(&$form) {
  97    
  98    // Set the value, and make sure it's a legal value.
  99    $value = $form['locale']['date_default_timezone']['#default_value'];
 100    $options = $form['locale']['date_default_timezone']['#options'];
 101    if (!array_key_exists($value, $options)) {
 102      //$value = array_pop($options);
 103      $value = NULL;
 104    }
 105    $form['locale']['date_default_timezone']['#type'] = 'hidden';
 106    $form['locale']['date_default_timezone']['#value'] = $value;
 107    return $form;  
 108  }
 109  
 110  /**
 111   * Create a form for the site timezone names.
 112   * Display a list of timezone names instead of offsets.
 113   */
 114  function date_timezone_user_form(&$form) {
 115    drupal_add_js(drupal_get_path('module', 'date_timezone') .'/date_timezone.js');
 116    
 117    $account = $form['_account']['#value'];
 118    $form['timezone']['#uid'] = $account->uid;
 119    $form['timezone']['#element_validate'] = array('date_timezone_update_user');
 120    
 121    $timezone = $account->timezone_name ? $account->timezone_name : variable_get('date_default_timezone_name', NULL);
 122    $form['timezone']['timezone_name'] = array(
 123      '#type' => 'select',
 124      '#title' => t('Default time zone'),
 125      '#default_value' => $timezone,
 126      '#options' => date_timezone_names(),
 127      '#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.'),
 128    );
 129    // Add the JavaScript callback to automatically set the timezone.
 130    if (empty($timezone)) {
 131      drupal_add_js('
 132  // Global Killswitch
 133  if (Drupal.jsEnabled) {
 134    $(document).ready(function() {
 135      Drupal.setDefaultTimezone();
 136    });
 137  }', 'inline');
 138    }  
 139    return $form;
 140  }
 141  
 142  /**
 143   * Hide the original form.
 144   * 
 145   * We have to do this in after_build in case the Event module
 146   * is enabled since the Event module will do its form_alter()
 147   * after the Date module.
 148   */
 149  function date_timezone_user_form_after_build($form) {
 150    // Set the value, and make sure it's a legal value.
 151    $value = $form['timezone']['timezone']['#default_value'];
 152    $options = $form['timezone']['timezone']['#options'];
 153    if (!array_key_exists($value, $options)) {
 154      //$value = array_pop($options);
 155      $value = NULL;
 156    }
 157    $form['timezone']['timezone']['#type'] = 'hidden';
 158    $form['timezone']['timezone']['#value'] = $value;
 159    return $form;
 160  }
 161  
 162  /**
 163   * Callback from site timezone settings form to update site timezone info.
 164   * When the timezone name is updated, update the offset as well.
 165   */
 166  function date_timezone_update_site($element, &$form_state) {
 167    $timezone = $element['date_default_timezone_name']['#value'];
 168    if (empty($timezone)) {
 169      $offset = $element['date_default_timezone_name']['#offset'];
 170    }
 171    else {
 172      variable_set('date_default_timezone_name', $timezone);
 173      $date = date_make_date('now', $timezone);
 174      $offset = date_offset_get($date);
 175    }
 176    
 177    // Reset the original form to the expected value.
 178    if (module_exists('event') && db_table_exists('event_timezones')) {
 179      $event_zone = date_event_zonelist_by_name(str_replace('_', ' ', $timezone));
 180      // The event module will update the timezone and zone id, using this value.
 181      if (!empty($event_zone['timezone'])) {
 182          form_set_value($element['date_default_timezone'], $event_zone['timezone'] .'|'. $offset, $form_state);
 183      } else {
 184          form_set_value($element['date_default_timezone'], $offset, $form_state); 
 185      }
 186    }
 187    else {
 188      form_set_value($element['date_default_timezone'], $offset, $form_state);
 189    }
 190  }
 191  
 192  /**
 193   * Callback from user timezone settings form to update user timezone info.
 194   * When the timezone name is updated, update the offset as well.
 195   */
 196  function date_timezone_update_user($element, &$form_state) {
 197    $timezone = $element['timezone_name']['#value'];
 198    if (!empty($timezone)) {
 199      $date = date_make_date('now', $timezone);
 200      $offset = date_offset_get($date);
 201    }
 202    
 203    // Reset the original form to the expected value.
 204    if (module_exists('event') && db_table_exists('event_timezones')) {
 205      $event_zone = date_event_zonelist_by_name(str_replace('_', ' ', $timezone));
 206      // The event module will update the timezone and zone id using this value.
 207      if (!empty($event_zone['timezone'])) {
 208          form_set_value($element['timezone'], $event_zone['timezone'] .'|'. $offset, $form_state);
 209      } else {
 210          form_set_value($element['timezone'], $offset, $form_state); 
 211      }
 212    }
 213    else {
 214      form_set_value($element['timezone'], $offset, $form_state);
 215    }
 216  }
 217  
 218  /**
 219   * Update the site timezone offset when cron runs.
 220   * 
 221   * This is to make sure that modules that rely on the timezone offset
 222   * have current information to process.
 223   */
 224  function date_timezone_cron() {
 225    $date = date_now(variable_get('date_default_timezone_name', NULL));
 226    $offset = date_offset_get($date);
 227    if ($offset != variable_get('date_default_timezone', 0)) {
 228      variable_set('date_default_timezone', $offset);
 229    }
 230  }
 231  
 232  /**
 233   * Update user timezone information at login.
 234   * 
 235   * This is to make sure that modules that rely on the timezone offset
 236   * have current information to process.
 237   */
 238  function date_timezone_user($op, &$edit, &$account, $category = NULL) {
 239    if (isset($account->uid) && $op == 'login' && variable_get('configurable_timezones', 1)) {
 240      if (strlen($account->timezone_name)) {
 241        $date = date_now($account->timezone_name);
 242        $offset = date_offset_get($date);
 243        if ($offset != $account->timezone) {
 244          $account->timezone = $offset;
 245          db_query("UPDATE {users} SET timezone='%s' WHERE uid = %d", $offset, $account->uid);
 246        }
 247      }
 248      else {
 249        // If the user doesn't already have a timezone name selected, 
 250        // default it to the site timezone name and offset.
 251        $timezone = variable_get('date_default_timezone_name', NULL);
 252        if (!empty($timezone)) {
 253          $date = date_now($timezone);
 254          $offset = date_offset_get($date);
 255          db_query("UPDATE {users} SET timezone_name = '%s', timezone='%s' WHERE uid = %d", $timezone, $offset, $account->uid);
 256        }
 257      }
 258    }
 259  }
 260  
 261  /**
 262   * Menu callback; Retrieve a JSON object containing a suggested time 
 263   * zone name.
 264   */
 265  function user_timezone($abbreviation = '', $offset = -1, $is_daylight_saving_time = NULL) {
 266    // An abbreviation of "0" passed in the callback arguments should be 
 267    // interpreted as the empty string.
 268    $abbreviation = $abbreviation ? $abbreviation : '';
 269    $timezone = timezone_name_from_abbr($abbreviation, intval($offset), $is_daylight_saving_time);
 270    // The client date is passed in for debugging purposes.
 271    $date = isset($_GET['date']) ? $_GET['date'] : '';
 272    // Log a debug message.
 273    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));
 274    drupal_json($timezone);
 275  }
 276  
 277  /**
 278   * Create replacement values for deprecated timezone names.
 279   */
 280  function date_timezone_replacement($old) {
 281    ('./'. drupal_get_path('module', 'date_timezone') .'/date_timezone.install');
 282    return _date_timezone_replacement($old);
 283  }
 284  
 285  /**
 286   * Helper function to update Event module timezone information.
 287   */
 288  function date_event_zonelist_by_name($name) {
 289    if (!module_exists('event') || !db_table_exists('event_timezones')) {
 290      return array();
 291    }
 292    static $zone_names = array();
 293  
 294    if (!isset($zone_names[$name])) {
 295      $zone = db_fetch_array(db_query("SELECT * FROM {event_timezones} WHERE name = '%s'", $name));
 296      $zone_names[$name] = $zone;
 297    }
 298  
 299    return $zone_names[$name];
 300  }
 301  
 302  


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7