You are here

function boxes_footer in Boxes 6

Same name and namespace in other branches
  1. 7 boxes.module \boxes_footer()

Implementation of hook_footer().

All ajax requests are targeted back to the current page so that the proper environment can be re-setup. Here in hook_footer we then detect that this is an ajax submission and handle it.

File

./boxes.module, line 151

Code

function boxes_footer() {

  // Besure the page isn't a 404 or 403.
  $headers = drupal_set_header();

  // Support for Pressflow patched drupal_set_header
  if (!is_array($headers)) {
    $headers = explode("\n", $headers);
  }
  foreach ($headers as $header) {
    if ($header == "HTTP/1.1 404 Not Found" || $header == "HTTP/1.1 403 Forbidden") {
      return;
    }
  }
  if (!empty($_GET['boxes_delta']) && boxes_access_admin()) {
    ctools_include('ajax');
    $output = array();
    if (isset($_GET['plugin_key'])) {
      ctools_include('form');
      $plugin_key = $_GET['plugin_key'];
      $delta = $_GET['boxes_delta'];
      $box = boxes_load($delta);
      if (!$box && $plugin_key) {
        $box = boxes_factory($plugin_key, array(
          'delta' => $delta,
        ));
        $form_state = array(
          'box' => $box,
          'plugin_key' => $plugin_key,
          'custom_action' => TRUE,
          'no_redirect' => TRUE,
        );
      }
      elseif (!$box) {
        return;
      }
      else {
        $form_state = array(
          'box' => $box,
          'no_redirect' => TRUE,
        );
      }
      $form = ctools_build_form('boxes_box_form', $form_state);
      if (empty($form_state['executed'])) {
        $output[] = ctools_ajax_command_html('#boxes-box-' . $delta . ' .box-editor', $form);
      }
      else {
        $output[] = array(
          'command' => 'getBlock',
          'delta' => $delta,
          'url' => url($_GET['q'], array(
            'absolute' => true,
          )),
        );
      }
    }
    else {
      $block = boxes_block('view', $_GET['boxes_delta']);
      $block['module'] = 'boxes';
      $output[] = ctools_ajax_command_replace('#block-boxes-' . $_GET['boxes_delta'], theme('block', (object) $block));
      if (module_exists('context')) {
        array_unshift($output, array(
          'command' => 'preReplaceContextBlock',
          'id' => 'block-boxes-' . $_GET['boxes_delta'],
        ));
        $output[] = array(
          'command' => 'postReplaceContextBlock',
          'id' => 'block-boxes-' . $_GET['boxes_delta'],
        );
      }
    }
    ctools_ajax_render($output);
  }
}