ajax.inc 2.75 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
 * @{
 */

/**
 * Simple render function to make sure output is what we want.
merlinofchaos's avatar
merlinofchaos committed
15 16 17 18 19 20 21
 *
 * This function renders an object into JSON, and that object contains
 * commands to the ajax response parser on the other side. The actual
 * commands that can be sent are completely dependent upon the client
 * javascript parser, which can be anything, but this function assumes
 * that 'display', at least, will be displayed in some kind of ajax
 * spot or popup.
merlinofchaos's avatar
merlinofchaos committed
22
 */
23
function views_ajax_render($output = NULL, $title = NULL, $url = NULL, $js = NULL) {
merlinofchaos's avatar
merlinofchaos committed
24 25 26 27 28 29 30 31 32 33 34
  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;
  }
35 36 37 38
  if (!empty($js)) {
    $output->js = $js;
  }

merlinofchaos's avatar
merlinofchaos committed
39 40 41 42 43 44
  drupal_set_header('Content-Type: text/javascript; charset=utf-8');
  print drupal_to_js($output);
  exit;
}

/**
45 46
 * Wrapper around drupal_build_form to handle some AJAX stuff automatically.
 * This makes some assumptions about the client.
merlinofchaos's avatar
merlinofchaos committed
47
 */
48 49 50 51 52 53 54 55 56
function views_ajax_form_wrapper($form_id, &$form_state) {
  // This won't override settings already in.
  $form_state += array(
    're_render' => FALSE,
    'no_redirect' => !empty($form_state['ajax']),
  );

  $output = drupal_build_form($form_id, $form_state);
  if (!empty($form_state['ajax']) && empty($form_state['executed'])) {
57
    // If the form didn't execute and we're using ajax, build up a
58 59 60 61 62 63 64
    // json command object to render.
    $object = new stdClass();
    $object->display = '';
    if ($messages = theme('status_messages')) {
      $object->display = '<div class="views-messages">' . $messages . '</div>';
    }
    $object->display .= $output;
merlinofchaos's avatar
merlinofchaos committed
65

66
    $object->title = empty($form_state['title']) ? '' : $form_state['title'];
67 68 69 70
    if (!empty($form_state['help_topic'])) {
      $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
      $object->title = theme('advanced_help_topic', $module, $form_state['help_topic']) . $object->title;
    }
71 72 73 74
    $object->url = empty($form_state['url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form_state['url'];
    $object->js = empty($form_state['js settings']) ? NULL : $form_state['js settings'];
    if (!empty($form_state['#section'])) {
      $object->hilite = '.' . views_ui_item_css($form_state['#section']);
merlinofchaos's avatar
merlinofchaos committed
75 76
    }

77
    $output = $object;
78
  }
79

80 81 82
  // These forms have the title built in, so set the title here:
  if (empty($form_state['ajax']) && !empty($form_state['title'])) {
    drupal_set_title($form_state['title']);
83 84
  }

85 86 87
  return $output;
}

merlinofchaos's avatar
merlinofchaos committed
88 89 90
/**
 * @}
 */