diff --git a/autologout.js b/autologout.js index 1d06c40..613ef9d 100644 --- a/autologout.js +++ b/autologout.js @@ -1,4 +1,5 @@ (function ($) { + Drupal.behaviors.autologout = { attach: function(context, settings) { @@ -12,6 +13,18 @@ if (context == document) { + Drupal.ajax.prototype.refreshAutologout = function() { + $.ajax(this.options); + t = setTimeout(init, Drupal.settings.autologout.timeout); + activity = false; + return false; + }; + + Drupal.ajax['refresh.autologout'] = new Drupal.ajax(null, $(document.body), { + url: '/autologout_ahah_set_last/ajax/', + event: null + }); + if (Drupal.settings.autologout.refresh_only) { // On pages that cannot be logged out of don't start the logout countdown. t = setTimeout(keepAlive, Drupal.settings.autologout.timeout); @@ -164,21 +177,8 @@ } function refresh() { - $.ajax({ - url: Drupal.settings.basePath + "autologout_ahah_set_last", - type: "POST", - success: function() { - t = setTimeout(init, Drupal.settings.autologout.timeout); - activity = false; - }, - error: function(XMLHttpRequest, textStatus) { - if (XMLHttpRequest.status == 403) { - window.location = Drupal.settings.autologout.redirect_url; - } - } - }); + Drupal.ajax['refresh.autologout'].refreshAutologout(); } - } }; })(jQuery); diff --git a/autologout.module b/autologout.module index ff90b5c..d4fa500 100644 --- a/autologout.module +++ b/autologout.module @@ -48,6 +48,7 @@ function autologout_menu() { 'page callback' => 'autologout_ahah_set_last', 'access callback' => 'user_is_logged_in', 'type' => MENU_CALLBACK, + 'theme callback' => 'ajax_base_page_theme', ); $items['autologout_ajax_get_time_left'] = array( @@ -61,6 +62,113 @@ function autologout_menu() { } /** + * Implements hook_block_info(). + */ +function autologout_block_info() { + $blocks = array(); + + $blocks['info'] = array( + 'info' => t('Automated Logout info'), + ); + + return $blocks; +} + +/** + * Implements hook_block_view(). + */ +function autologout_block_view($delta = '') { + global $user; + + $block = array(); + + if (_autologout_prevent()) { + // Don't display the block if the user is not going + // to be logged out on this page. + return; + } + + $block['subject'] = filter_xss_admin(variable_get('autologout_block_title', t('Autologout warning block'))); + + if (module_exists('jstimer') && module_exists('jst_timer')) { + $block['content'] = array(drupal_get_form('autologout_create_block_form')); + } + else { + $timeout = (int) variable_get('autologout_timeout', 1800); + $block['content'] = t('You will be logged out in !time if this page is not refreshed before then.', array('!time' => format_interval($timeout))); + } + + return $block; +} + +/** + * Drupal reset timer form on timer block. + */ +function autologout_create_block_form() { + + $markup = autologout_create_timer(); + + $form['test'] = array( + '#type' => 'button', + '#value' => t('Reset Timeout'), + '#weight' => 1, + '#limit_validation_errors' => FALSE, + '#executes_submit_callback' => FALSE, + '#ajax' => array( + 'callback' => 'autologout_ahah_set_last', + ), + ); + + $form['timer'] = array( + '#type' => 'markup', + '#value' => $markup, + '#markup' => $markup, + ); + + return $form; +} + +/** + * Get the timer HTML markup. + * + * @return string + * HTML to insert a countdown timer. + */ +function autologout_create_timer() { + // @TODO - should be a theme function? + $refresh = (int) variable_get('autologout_refresh_delta', 0); + $redirect_url = filter_xss_admin(variable_get('autologout_redirect_url', 'user/login')); + $timeout = (int) variable_get('autologout_timeout', 1800); + $timeformat = filter_xss_admin(variable_get('autologout_jstimer_format', '%hours%:%mins%:%secs%')); + + $markup = "