2 // $Id: ajax.inc,v 1.19 2008/09/22 20:50:58 merlinofchaos Exp $
7 * Handles the server side AJAX interactions of Views.
9 * @defgroup ajax Views ajax library
14 * Menu callback to load a view via AJAX.
16 function views_ajax() {
17 if (isset($_REQUEST['view_name']) && isset($_REQUEST['view_display_id'])) {
18 $name = $_REQUEST['view_name'];
19 $display_id = $_REQUEST['view_display_id'];
20 $args = isset($_REQUEST['view_args']) && $_REQUEST['view_args'] !== '' ? explode('/', $_REQUEST['view_args']) : array();
21 $path = isset($_REQUEST['view_path']) ? $_REQUEST['view_path'] : NULL;
22 $dom_id = isset($_REQUEST['view_dom_id']) ? $_REQUEST['view_dom_id'] : NULL;
23 $pager_element = isset($_REQUEST['pager_element']) ? $_REQUEST['pager_element'] : NULL;
24 views_include('ajax');
25 $object = new stdClass();
27 $object->status = FALSE;
28 $object->display = '';
31 if ($view = views_get_view($name)) {
32 if ($view->access($display_id)) {
34 // Fix 'q' for paging.
39 // Override the display's pager_element with the one actually used.
40 if (isset($pager_element)) {
41 $view->display[$display_id]->handler->set_option('pager_element', $pager_element);
43 // Reuse the same DOM id so it matches that in Drupal.settings.
44 $view->dom_id = $dom_id;
46 $errors = $view->validate();
47 if ($errors === TRUE) {
48 $object->status = TRUE;
49 $object->title = $view->get_title();
50 $object->display .= $view->preview($display_id, $args);
53 foreach ($errors as $error) {
54 drupal_set_message($error, 'error');
57 // Register the standard JavaScript callback.
58 $object->__callbacks = array('Drupal.Views.Ajax.ajaxViewResponse');
59 // Allow other modules to extend the data returned.
60 drupal_alter('ajax_data', $object, 'views', $view);
63 $messages = theme('status_messages');
64 $object->messages = $messages ? '<div class="views-messages">' . $messages . '</div>' : '';
66 views_ajax_render($object);
71 * Simple render function to make sure output is what we want.
73 * This function renders an object into JSON, and that object contains
74 * commands to the ajax response parser on the other side. The actual
75 * commands that can be sent are completely dependent upon the client
76 * javascript parser, which can be anything, but this function assumes
77 * that 'display', at least, will be displayed in some kind of ajax
80 function views_ajax_render($output = NULL, $title = NULL, $url = NULL, $js = NULL) {
82 $output->display = t('Server reports invalid input error.');
83 $output->title = t('Error');
85 elseif (!is_object($output)) {
86 $temp = new stdClass();
87 $temp->display = $output;
88 $temp->title = $title;
101 * Wrapper around drupal_build_form to handle some AJAX stuff automatically.
102 * This makes some assumptions about the client.
104 function views_ajax_form_wrapper($form_id, &$form_state) {
105 // This won't override settings already in.
106 $form_state += array(
107 're_render' => FALSE,
108 'no_redirect' => !empty($form_state['ajax']),
111 $output = drupal_build_form($form_id, $form_state);
112 if (!empty($form_state['ajax']) && empty($form_state['executed'])) {
113 // If the form didn't execute and we're using ajax, build up a
114 // json command object to render.
115 $object = new stdClass();
116 $object->display = '';
117 if ($messages = theme('status_messages')) {
118 $object->display = '<div class="views-messages">' . $messages . '</div>';
120 $object->display .= $output;
122 $object->title = empty($form_state['title']) ? '' : $form_state['title'];
123 if (!empty($form_state['help_topic'])) {
124 $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
125 $object->title = theme('advanced_help_topic', $module, $form_state['help_topic']) . $object->title;
127 $object->url = empty($form_state['url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form_state['url'];
128 $object->js = empty($form_state['js settings']) ? NULL : $form_state['js settings'];
129 if (!empty($form_state['#section'])) {
130 $object->hilite = '.' . views_ui_item_css($form_state['#section']);
136 // These forms have the title built in, so set the title here:
137 if (empty($form_state['ajax']) && !empty($form_state['title'])) {
138 drupal_set_title($form_state['title']);
146 * Page callback for views user autocomplete
148 function views_ajax_autocomplete_user($string = '') {
149 // The user enters a comma-separated list of tags. We only autocomplete the last tag.
150 $array = drupal_explode_tags($string);
153 $last_string = trim(array_pop($array));
155 if ($last_string != '') {
156 $prefix = count($array) ? implode(', ', $array) . ', ' : '';
158 if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
159 $matches[$prefix . 'Anonymous'] = 'Anonymous';
161 $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $last_string, 0, 10);
163 while ($account = db_fetch_object($result)) {
165 // Commas and quotes in terms are special cases, so encode 'em.
166 if (strpos($account->name, ',') !== FALSE || strpos($account->name, '"') !== FALSE) {
167 $n = '"' . str_replace('"', '""', $account->name) . '"';
169 $matches[$prefix . $n] = check_plain($account->name);
173 drupal_json($matches);