/[drupal]/contributions/modules/autotimezone/autotimezone.module
ViewVC logotype

Diff of /contributions/modules/autotimezone/autotimezone.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.15, Thu Aug 23 22:15:50 2007 UTC revision 1.16, Wed Feb 18 17:11:22 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: autotimezone.module,v 1.11.2.4 2007/08/23 22:13:42 lukelast Exp $  // $Id: autotimezone.module,v 1.15 2007/08/23 22:15:50 lukelast Exp $
3    
4  /**  /**
5   * @file   * @file
6   * Automatically sets the users timezone based on the value from the users browser using Javascript.   * Automatically sets user time zones based on the value from the users browser
7   * The javascript compares the local browser timezone value and the timezone value drupal has stored for that user.   * using Javascript. The javascript compares the local browser time zone value
8   * If the two values are different it will send a page request index.php?q=autotimezone/*value* where *value* is the new timezone value.   * and the time zone value drupal has stored for that user. If the two values
9   * This module relies on jQuery.   * 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().   * Implementation of hook_menu().
15   */   */
16  function autotimezone_menu($may_cache) {  function autotimezone_menu() {
17    $items = array();    $items['admin/settings/autotimezone'] = array(
18    if ($may_cache) {      'title' => 'Auto Time Zone',
19      $items[] = array(      'description' => 'Automatically updates user time zones using JavaScript.',
20        'path' => 'admin/settings/autotimezone',      'page callback' => 'drupal_get_form',
21        'title' => t('Autotimezone'),      'page arguments' => array('autotimezone_admin_settings'),
22        'description' => t('Automatically updates users timezone using javascript.'),      'access arguments' => array('administer site configuration'),
23        'callback' => 'drupal_get_form',      'type' => MENU_NORMAL_ITEM,
24        'callback arguments' => 'autotimezone_admin_settings',    );
25        'type' => MENU_NORMAL_ITEM,    $items['autotimezone'] = array(
26      );      'page callback' => 'autotimezone_page',
27      $items[] = array('path' => 'autotimezone', 'title' => t('Auto Timezone Update'),      'access callback' => TRUE,
28        'callback' => 'autotimezone_page',      'type' => MENU_CALLBACK,
29        'access' => TRUE,    );
30        'type' => MENU_CALLBACK);    return $items;
31    }  }
32    else {  
33      if (!function_exists('throttle_status') || !throttle_status()) { //Don't do anything if the throttle is active.  /**
34        global $user;   * Implementation of hook_init().
35        if ($user->uid != 0 or variable_get('autotimezone_update_guest', 0)) { //If the user is a guest, only continue if guest checking is on.   */
36          //We need to check if the user is a guest and then use the guest session variable.  function autotimezone_init() {
37          if ($user->uid == 0) {    if (!function_exists('throttle_status') || !throttle_status()) { // Don't do anything if the throttle is active.
38            if (!$_SESSION['timezone']) {      global $user;
39              $_SESSION['timezone'] = variable_get('date_default_timezone', 0);      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            $timezone = $_SESSION['timezone'] / -60;        if ($user->uid == 0) {
42          }          if (!$_SESSION['timezone']) {
43          else { //If user is not guest.            $_SESSION['timezone'] = variable_get('date_default_timezone', 0);
           $timezone = $user->timezone / -60; //Convert offset to minutes.  
44          }          }
45          //This is the Javascript that will send the timezone back to the server if it needs to be updated.          $timezone = $_SESSION['timezone'] / -60;
         $javascript = "\nvar now = new Date();";  
         $javascript .= "\nvar offset = now.getTimezoneOffset();";  
         $javascript .= "\n" . 'if (!(offset == ' . drupal_to_js($timezone) . ')) {$(document).ready(function(){$.get(' . drupal_to_js(url('autotimezone/', NULL, NULL, TRUE)) . ' + offset);})}';  
         drupal_add_js($javascript, 'inline');  
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    }    }
   return $items;  
57  }  }
58    
59  /**  /**
# Line 60  function autotimezone_menu($may_cache) { Line 63  function autotimezone_menu($may_cache) {
63   *   array of form content.   *   array of form content.
64   */   */
65  function autotimezone_admin_settings() {  function autotimezone_admin_settings() {
66    variable_set('configurable_timezones', 1); //This variable must be true for the format_date function to use the individual users time zones.    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('#type' => 'fieldset', '#title' => t('Guest user options'), '#description' => t('If you do not know why you might need this feature keep it turned off.'));    $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(    $form['guest']['autotimezone_update_guest'] = array(
73      '#type' => 'radios',      '#type' => 'radios',
74      '#title' => t('Update guest users session variables'),      '#title' => t('Update guest user session variables'),
75      '#default_value' => variable_get('autotimezone_update_guest', 0),      '#default_value' => variable_get('autotimezone_update_guest', FALSE),
76      '#options' => array('1' => t('On'), '0' => t('Off')),      '#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.'));      '#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);    return system_settings_form($form);
80  }  }
81    
# Line 77  function autotimezone_admin_settings() { Line 85  function autotimezone_admin_settings() {
85   */   */
86  function autotimezone_page($offset) {  function autotimezone_page($offset) {
87    global $user;    global $user;
88    $offset = intval($offset) * -60; //Convert offset to units of seconds from GMT.    $offset = intval($offset) * -60; // Convert offset to units of seconds from GMT.
89    $_SESSION['timezone'] = $offset; //Update session variable.    $_SESSION['timezone'] = $offset; // Update session variable.
90    //If needed update user object with new timezone value.    // If needed update user object with new timezone value.
91    if ($user->uid != 0 and $offset != $user->timezone) {    if ($user->uid != 0 and $offset != $user->timezone) {
92      watchdog('user', t('Timezone updated from %old to %new.', array('%old' => $user->timezone / 3600, '%new' => $offset / 3600)));      watchdog('user', 'Time zone updated from %old to %new.', array('%old' => $user->timezone / 3600, '%new' => $offset / 3600));
93      user_save($user, array('timezone' => $offset));      user_save($user, array('timezone' => $offset));
94    }    }
95  }  }

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.16

  ViewVC Help
Powered by ViewVC 1.1.2