You are here

function ajaxblocks_ajax_handler in Ajax Blocks 6

Same name and namespace in other branches
  1. 7 ajaxblocks.module \ajaxblocks_ajax_handler()

Handles AJAX request and returns the content of the appropriate blocks.

1 string reference to 'ajaxblocks_ajax_handler'
ajaxblocks_menu in ./ajaxblocks.module
Implements hook_menu().

File

./ajaxblocks.module, line 221
Loads dynamic blocks on cached page for anonymous users by performing AJAX request.

Code

function ajaxblocks_ajax_handler() {

  // Disable client-side caching.
  header('Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0');
  header('Pragma: no-cache');

  // Disable server-side caching.
  global $conf;
  $conf['cache'] = FALSE;
  _ajaxblocks_in_ajax_handler_impl(TRUE);
  $content = array();
  $delays = array();
  $min_delay = 1000000;
  $current_user_rids = array_keys($GLOBALS['user']->roles);
  if (isset($_GET['blocks']) && isset($_GET['path'])) {

    // Set 'q' parameter in order to make arg() work correctly.
    $_GET['q'] = $_GET['path'];
    $_REQUEST['q'] = $_GET['path'];

    // Set $_SERVER variables in order to make request_uri() work correctly.
    _ajaxblocks_fix_request_uri($_GET['path']);

    // Build the block content and return as json.
    $ajax_blocks = explode('/', $_GET['blocks']);
    unset($_GET['blocks']);
    unset($_GET['path']);
    unset($_GET['nocache']);
    $block_settings = array();
    foreach ($ajax_blocks as $block_id) {
      if (!ajaxblocks_is_ajax($block_id, $block_settings)) {
        continue;
      }

      // Don't return blocks which are not enabled for the current user.
      if (count($block_settings['role_permission']) > 0 && count(array_intersect($current_user_rids, $block_settings['role_permission'])) == 0) {
        continue;
      }
      $delay = intval($block_settings['delay']);
      $delays[$block_id] = $delay;
      if ($min_delay > $delay) {
        $min_delay = $delay;
      }

      // Drupal.settings can be changed by the block construction code. The handler returns the settings difference to the client.
      $settings_old = array();
      $js = drupal_add_js(NULL, NULL, NULL);
      if (array_key_exists('header', $js)) {
        if (array_key_exists('setting', $js['header'])) {
          $settings_old = $js['header']['setting'];
        }
      }
      $parts = explode("-", $block_id, 2);
      $block_content = module_invoke($parts[0], 'block', 'view', $parts[1]);
      $content[$block_id] = array(
        'content' => isset($block_content['content']) ? $block_content['content'] : '',
      );
      $settings_new = array();
      $js = drupal_add_js(NULL, NULL, NULL);
      if (array_key_exists('header', $js)) {
        if (array_key_exists('setting', $js['header'])) {
          $settings_new = $js['header']['setting'];
        }
      }
      $settings_diff = _ajaxblocks_drupal_array_diff_assoc_recursive($settings_new, $settings_old);
      $content[$block_id]['ajaxblocks_settings'] = '';
      if (count($settings_diff) > 0) {
        $content[$block_id]['ajaxblocks_settings'] = call_user_func_array('array_merge_recursive', $settings_diff);
      }
    }
  }
  foreach ($delays as $block_id => $delay) {
    $delay -= $min_delay;
    if ($delay > 0) {
      $content[$block_id]['delay'] = $delay;
    }
  }
  ajaxblocks_json($content);
  exit;
}