| 1 |
<?php
|
| 2 |
// $Id: autotimezone.module,v 1.15 2007/08/23 22:15:50 lukelast Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Automatically sets user time zones based on the value from the users browser
|
| 7 |
* using Javascript. The javascript compares the local browser time zone value
|
| 8 |
* and the time zone value drupal has stored for that user. If the two values
|
| 9 |
* are different it will send a page request index.php?q=autotimezone/*value*
|
| 10 |
* where *value* is the new timezone value.
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_menu().
|
| 15 |
*/
|
| 16 |
function autotimezone_menu() {
|
| 17 |
$items['admin/settings/autotimezone'] = array(
|
| 18 |
'title' => 'Auto Time Zone',
|
| 19 |
'description' => 'Automatically updates user time zones using JavaScript.',
|
| 20 |
'page callback' => 'drupal_get_form',
|
| 21 |
'page arguments' => array('autotimezone_admin_settings'),
|
| 22 |
'access arguments' => array('administer site configuration'),
|
| 23 |
'type' => MENU_NORMAL_ITEM,
|
| 24 |
);
|
| 25 |
$items['autotimezone'] = array(
|
| 26 |
'page callback' => 'autotimezone_page',
|
| 27 |
'access callback' => TRUE,
|
| 28 |
'type' => MENU_CALLBACK,
|
| 29 |
);
|
| 30 |
return $items;
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of hook_init().
|
| 35 |
*/
|
| 36 |
function autotimezone_init() {
|
| 37 |
if (!function_exists('throttle_status') || !throttle_status()) { // Don't do anything if the throttle is active.
|
| 38 |
global $user;
|
| 39 |
if ($user->uid != 0 || variable_get('autotimezone_update_guest', FALSE)) { // If the user is a guest, only continue if guest checking is on.
|
| 40 |
// We need to check if the user is a guest and then use the guest session variable.
|
| 41 |
if ($user->uid == 0) {
|
| 42 |
if (!$_SESSION['timezone']) {
|
| 43 |
$_SESSION['timezone'] = variable_get('date_default_timezone', 0);
|
| 44 |
}
|
| 45 |
$timezone = $_SESSION['timezone'] / -60;
|
| 46 |
}
|
| 47 |
else { // If user is not guest.
|
| 48 |
$timezone = $user->timezone / -60; // Convert offset to minutes.
|
| 49 |
}
|
| 50 |
// This is the Javascript that will send the timezone back to the server if it needs to be updated.
|
| 51 |
$javascript = "\nvar now = new Date();";
|
| 52 |
$javascript .= "\nvar offset = now.getTimezoneOffset();";
|
| 53 |
$javascript .= "\n".'if (!(offset == '. drupal_to_js($timezone) .')) {$(document).ready(function(){$.get('. drupal_to_js(url('autotimezone/', array('absolute' => TRUE))) .' + offset);})}';
|
| 54 |
drupal_add_js($javascript, 'inline');
|
| 55 |
}
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
* Menu callback
|
| 61 |
*
|
| 62 |
* @return
|
| 63 |
* array of form content.
|
| 64 |
*/
|
| 65 |
function autotimezone_admin_settings() {
|
| 66 |
variable_set('configurable_timezones', TRUE); // This variable must be true for the format_date function to use the individual users time zones.
|
| 67 |
$form['guest'] = array(
|
| 68 |
'#type' => 'fieldset',
|
| 69 |
'#title' => t('Guest user options'),
|
| 70 |
'#description' => t('If you do not know why you might need this feature, keep it turned off.'),
|
| 71 |
);
|
| 72 |
$form['guest']['autotimezone_update_guest'] = array(
|
| 73 |
'#type' => 'radios',
|
| 74 |
'#title' => t('Update guest user session variables'),
|
| 75 |
'#default_value' => variable_get('autotimezone_update_guest', FALSE),
|
| 76 |
'#options' => array('1' => t('On'), '0' => t('Off')),
|
| 77 |
'#description' => t('This module can set a session variable named $_SESSION[\'timezone\'] for anonymous users, which is in the form of seconds from GMT. This feature is of no use unless extra code has been added to your site to take advantage of it.'),
|
| 78 |
);
|
| 79 |
return system_settings_form($form);
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Menu callback for /autotimezone/xxx
|
| 84 |
* Takes the data sent from the Javascript and updates the timezone.
|
| 85 |
*/
|
| 86 |
function autotimezone_page($offset) {
|
| 87 |
global $user;
|
| 88 |
$offset = intval($offset) * -60; // Convert offset to units of seconds from GMT.
|
| 89 |
$_SESSION['timezone'] = $offset; // Update session variable.
|
| 90 |
// If needed update user object with new timezone value.
|
| 91 |
if ($user->uid != 0 and $offset != $user->timezone) {
|
| 92 |
watchdog('user', 'Time zone updated from %old to %new.', array('%old' => $user->timezone / 3600, '%new' => $offset / 3600));
|
| 93 |
user_save($user, array('timezone' => $offset));
|
| 94 |
}
|
| 95 |
}
|