You are here

function boxes_footer in Boxes 7

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

Implements 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.

1 call to boxes_footer()
boxes_page_alter in ./boxes.module
Implements hook_page_alter().

File

./boxes.module, line 240
Core functionality for boxes module.

Code

function boxes_footer() {

  // If ckeditor is used, the base path needs to be set on the original
  // page load, or ckeditor will set it incorrectly to the root.
  // while this is loaded on every page it is just a single set of a variable.
  if (module_exists("wysiwyg") && ($editor = wysiwyg_get_editor('ckeditor'))) {
    $base = base_path();
    drupal_add_js("window.CKEDITOR_BASEPATH = '{$base}{$editor['library path']}/'", 'inline');
  }

  // Besure the page isn't a 404 or 403.
  $headers = drupal_get_http_header();
  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_edit()) {
    include_once 'includes/ajax.inc';
    $output = array();
    if (isset($_GET['plugin_key'])) {
      $plugin_key = $_GET['plugin_key'];
      $delta = $_GET['boxes_delta'];
      $box = boxes_box_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 = drupal_build_form('boxes_box_form', $form_state);
      $errors = form_set_error();
      if (!empty($errors)) {

        /* validation error */
        $msg_and_form = theme('status_messages') . drupal_render($form);
        $output[] = array(
          'command' => 'showBoxForm',
          'method' => 'html',
          'selector' => '#block-boxes-' . $delta,
          'data' => $msg_and_form,
          'settings' => NULL,
        );
      }
      elseif (empty($form_state['submitted']) || isset($_GET['init_form_continue'])) {
        $output[] = array(
          'command' => 'showBoxForm',
          'method' => 'html',
          'selector' => '#boxes-box-' . $delta,
          'data' => drupal_render($form),
          'settings' => NULL,
        );
      }
      else {
        $output[] = array(
          'selector' => '#block-boxes-' . $delta,
          'command' => 'getBlock',
          'method' => 'replaceWith',
          'delta' => $delta,
          'url' => url(drupal_is_front_page() ? '<front>' : $_GET['q'], array(
            'absolute' => TRUE,
          )),
        );
      }
    }
    else {
      $block = boxes_block_view($_GET['boxes_delta']);
      $block['module'] = 'boxes';
      $block['region'] = 'page_bottom';
      $block['content'] = array(
        '#markup' => $block['content'],
      );
      $block = (object) $block;
      $renderable = _block_get_renderable_array(_block_render_blocks(array(
        'boxes_' . $_GET['boxes_delta'] => $block,
      )));
      $markup = trim(drupal_render($renderable));
      $id = 'block-boxes-' . $_GET['boxes_delta'];

      // We cannot use drupal_html_id() here because it will increment the id on
      // pages where a box is already present. So we do its other transforms
      // manually.
      $id = strtr(drupal_strtolower($id), array(
        ' ' => '-',
        '_' => '-',
        '[' => '-',
        ']' => '',
      ));
      $id = preg_replace('/[^A-Za-z0-9\\-_]/', '', $id);
      $id = preg_replace('/\\-+/', '-', $id);
      $output[] = ajax_command_replace('#' . $id, $markup);
      if (module_exists('context')) {
        array_unshift($output, array(
          'selector' => '#' . $id,
          'command' => 'preReplaceContextBlock',
          'id' => $id,
        ));
        $output[] = array(
          'selector' => '#' . $id,
          'command' => 'postReplaceContextBlock',
          'id' => $id,
        );
      }
    }
    ajax_deliver(array(
      '#type' => 'ajax',
      '#commands' => $output,
    ));
    exit;
  }
}