You are here

function ajaxblocks_ajax_handler in Ajax Blocks 7

Same name and namespace in other branches
  1. 6 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 216
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.
  drupal_page_is_cacheable(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.
      // TODO Check permissions fully.
      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();
      if (array_key_exists('settings', $js)) {
        $settings_old = $js['settings']['data'];
      }
      $parts = explode("-", $block_id, 2);
      $block = new stdClass();
      $block->module = $parts[0];
      $block->delta = $parts[1];
      $block_content = module_invoke($block->module, 'block_view', $block->delta);

      // Allow modules to modify the block before it is viewed, via either
      // hook_block_view_alter() or hook_block_view_MODULE_DELTA_alter().
      drupal_alter(array(
        'block_view',
        "block_view_{$block->module}_" . str_replace('-', '_', $block->delta),
      ), $block_content, $block);
      $content[$block_id] = array(
        'content' => isset($block_content['content']) ? render($block_content['content']) : '',
      );
      $settings_new = array();
      $js = drupal_add_js();
      if (array_key_exists('settings', $js)) {
        $settings_new = $js['settings']['data'];
      }
      $array_mapping_func = function_exists('drupal_array_diff_assoc_recursive') ? 'drupal_array_diff_assoc_recursive' : 'array_diff_assoc';
      $settings_diff = call_user_func_array($array_mapping_func, array(
        $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;
    }
  }
  drupal_json_output($content);
}