ajax.inc 3.06 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4 5 6 7 8
<?php
// $Id$

/**
 * @file ajax.inc
 *
 * Handles the server side AJAX interactions of Views.
 *
merlinofchaos's avatar
merlinofchaos committed
9
 * @defgroup ajax Views ajax library
merlinofchaos's avatar
merlinofchaos committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
 * @{
 */

/**
 * Simple render function to make sure output is what we want.
 */
function views_ajax_render($output = NULL, $title = NULL, $url = NULL) {
  if (empty($output)) {
    $output->display = t('Server reports invalid input error.');
    $output->title = t('Error');
  }
  elseif (!is_object($output)) {
    $temp = new stdClass();
    $temp->display = $output;
    $temp->title = $title;
    $temp->url = $url;
    $output = $temp;
  }
  drupal_set_header('Content-Type: text/javascript; charset=utf-8');
  print drupal_to_js($output);
  exit;
}


/**
 * Handle a form for AJAX in a manner that happens to be basically the
 * opposite of the normal flow; if the form hasn't been processed,
 * just render it and exit; if it has been submitted successfuly, however,
 * then we return whatever the submit function returned and do our
 * next step accordingly.
 *
 * Much of this code is from drupal_get_form and/or drupal_process_form.
 *
 * @param $form_id
 *   The id of the form
 * @param ...
 *   Any arguments that go to the form.
 */
function views_ajax_form($form_id) {
  $args = func_get_args();

  // Insert a placeholder for the form state so we can then assign a reference to it.
  array_splice($args, 1, 0, array(NULL));

  // Create the empty form state object.
  $form_state = array('storage' => NULL, 'submitted' => FALSE, 'post' => $_POST, 'ajax' => TRUE);

  // Replace the URL with the empty form state object as a reference:
  $args[1] = &$form_state;

  $form = call_user_func_array('drupal_retrieve_form', $args);

  // Prepare the form. My Drupal 5 code didn't do this, so I'm not sure
  // if it is necessary in D6 or not.
  $form_build_id = 'form-'. md5(mt_rand());
  $form['#build_id'] = $form_build_id;
  drupal_prepare_form($form_id, $form, $form_state);

  // Process the form our own selves so we don't get some extra batch handling
  // that we don't want.
  $form['#post'] = $_POST;
  $form_state['values'] = array();
  $form = form_builder($form_id, $form, $form_state);

  // from the POST data is set and matches the current form_id.
  if ((!empty($form['#post']) && (isset($form['#post']['form_id']) && ($form['#post']['form_id'] == $form_id)))) {
    drupal_validate_form($form_id, $form, $form_state);

    // In case of successful submit, execute the form handlers and return the
    // form state so the calling function can do what it needs to do next.
    if ((!empty($form_state['submitted'])) && !form_get_errors() && empty($form_state['rebuild'])) {
      $form_state['redirect'] = NULL;
      form_execute_handlers('submit', $form, $form_state);
      return $form_state;
    }
  }

  // If the form wasn't submitted successfully, render the form.
  $output = theme('status_messages');
  $output .= drupal_render_form($form_id, $form);
  $title = empty($form['#title']) ? '' : $form['#title'];
91
  $url = empty($form['#url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form['#url'];
merlinofchaos's avatar
merlinofchaos committed
92 93 94 95 96 97
  views_ajax_render($output, $title, $url);
}

/**
 * @}
 */