You are here

function timezone_detect_add_js in Timezone Detect 7

Same name and namespace in other branches
  1. 6 timezone_detect.module \timezone_detect_add_js()

Add the javascript that will update the user's timezone via ajax callback.

1 call to timezone_detect_add_js()
timezone_detect_page_build in ./timezone_detect.module
Implements hook_page_build().

File

./timezone_detect.module, line 134
Module provides automatic timezone detection via javascript.

Code

function timezone_detect_add_js() {
  $added =& drupal_static(__FUNCTION__, FALSE);
  if (!$added) {

    // Add library js.
    drupal_add_library('timezone_detect', 'jstz');

    // Add helper js.
    $helper_js = drupal_get_path('module', 'timezone_detect') . '/timezone_detect.js';
    drupal_add_js($helper_js);

    // Store the current timezone for comparison.
    $timezone = '';
    if (!empty($_SESSION['timezone_detect']['current_timezone'])) {
      $timezone = $_SESSION['timezone_detect']['current_timezone'];
    }
    elseif (!empty($GLOBALS['user']->timezone)) {
      $timezone = $GLOBALS['user']->timezone;
    }

    // Create and pass token to prevent cross-site request forgery.
    $token = drupal_get_token();

    // Add these items as js settings.
    $setting = array(
      'timezone_detect' => array(
        'current_timezone' => $timezone,
        'token' => $token,
      ),
    );
    drupal_add_js($setting, 'setting');

    // Mark as added so we don't do it again.
    $added = TRUE;
  }
}