You are here

function ajaxblocks_preprocess_html in Ajax Blocks 7

Implements hook_preprocess_html().

File

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

Code

function ajaxblocks_preprocess_html(&$variables) {
  $ajax_blocks = ajaxblocks_page_ajax_list();
  if (count($ajax_blocks) == 0) {
    return;
  }
  drupal_add_js(drupal_get_path('module', 'ajaxblocks') . '/ajaxblocks.js');
  $path = url('ajaxblocks');
  if ($path !== base_path() . 'ajaxblocks') {

    // Provide path for AJAX handler directly in non-trivial cases (for instance, language prefix).
    drupal_add_js(array(
      'ajaxblocks_path' => $path,
    ), 'setting');
  }
  $block_ids = array();
  $block_ids_late = array();
  $min_delay = 1000000;
  $min_delay_late = 1000000;
  $use_loader_picture = FALSE;
  foreach ($ajax_blocks as $block_id => $settings) {
    if ($settings['is_late']) {
      $block_ids_late[] = $block_id;
      if ($min_delay_late > $settings['delay']) {
        $min_delay_late = intval($settings['delay']);
      }
    }
    else {
      $block_ids[] = $block_id;
      if ($min_delay > $settings['delay']) {
        $min_delay = intval($settings['delay']);
      }
    }
    if ($settings['loader_picture'] > 0) {
      $use_loader_picture = TRUE;
    }
  }
  if ($use_loader_picture) {
    drupal_add_css(drupal_get_path('module', 'ajaxblocks') . '/ajaxblocks.css');
    $variables['styles'] = drupal_get_css();
  }
  $get_copy = $_GET;
  $params_to_exclude = array(
    'q',
    'blocks',
    'path',
  );
  foreach ($params_to_exclude as $param) {
    unset($get_copy[$param]);
  }
  $get_params = drupal_http_build_query($get_copy);
  if (strlen($get_params) > 0) {
    $get_params = '&' . $get_params;
  }
  if (count($block_ids) > 0) {
    drupal_add_js(array(
      'ajaxblocks' => 'blocks=' . implode('/', $block_ids) . '&path=' . $_GET['q'] . $get_params,
    ), 'setting');
    if ($min_delay > 0) {
      drupal_add_js(array(
        'ajaxblocks_delay' => $min_delay,
      ), 'setting');
    }
  }
  if (count($block_ids_late) > 0) {
    drupal_add_js(array(
      'ajaxblocks_late' => 'blocks=' . implode('/', $block_ids_late) . '&path=' . $_GET['q'] . $get_params,
    ), 'setting');
    if ($min_delay_late > 0) {
      drupal_add_js(array(
        'ajaxblocks_delay_late' => $min_delay_late,
      ), 'setting');
    }
  }

  // Rewrite page-level js.
  $variables['scripts'] = drupal_get_js();
}