Stripping CVS keywords
[project/views.git] / includes / ajax.inc
CommitLineData
ddf08198 1<?php
ddf08198
EM
2
3/**
4 * @file ajax.inc
5 *
6 * Handles the server side AJAX interactions of Views.
7 *
dc3ec4ee 8 * @defgroup ajax Views ajax library
ddf08198
EM
9 * @{
10 */
11
12/**
80f01edd
EM
13 * Menu callback to load a view via AJAX.
14 */
15function views_ajax() {
16 if (isset($_REQUEST['view_name']) && isset($_REQUEST['view_display_id'])) {
17 $name = $_REQUEST['view_name'];
18 $display_id = $_REQUEST['view_display_id'];
86433f63 19 $args = isset($_REQUEST['view_args']) && $_REQUEST['view_args'] !== '' ? explode('/', $_REQUEST['view_args']) : array();
80f01edd 20 $path = isset($_REQUEST['view_path']) ? $_REQUEST['view_path'] : NULL;
72ec0beb
EM
21 $dom_id = isset($_REQUEST['view_dom_id']) ? $_REQUEST['view_dom_id'] : NULL;
22 $pager_element = isset($_REQUEST['pager_element']) ? $_REQUEST['pager_element'] : NULL;
80f01edd
EM
23 views_include('ajax');
24 $object = new stdClass();
25
26 $object->status = FALSE;
27 $object->display = '';
28
116233f4
EM
29 $arg = explode('/', $_REQUEST['view_path']);
30
31 if ($arg[0] == 'admin' || (variable_get('node_admin_theme', '0') && $arg[0] == 'node' && ($arg[1] == 'add' || $arg[2] == 'edit'))) {
32 global $custom_theme;
33 $custom_theme = variable_get('admin_theme', '0');
34 drupal_add_css(drupal_get_path('module', 'system') .'/admin.css', 'module');
35 }
80f01edd
EM
36 // Load the view.
37 if ($view = views_get_view($name)) {
38 if ($view->access($display_id)) {
39
40 // Fix 'q' for paging.
41 if (!empty($path)) {
42 $_GET['q'] = $path;
43 }
44
72ec0beb
EM
45 // Override the display's pager_element with the one actually used.
46 if (isset($pager_element)) {
47 $view->display[$display_id]->handler->set_option('pager_element', $pager_element);
48 }
49 // Reuse the same DOM id so it matches that in Drupal.settings.
50 $view->dom_id = $dom_id;
51
80f01edd
EM
52 $errors = $view->validate();
53 if ($errors === TRUE) {
54 $object->status = TRUE;
55 $object->title = $view->get_title();
56 $object->display .= $view->preview($display_id, $args);
57 }
58 else {
59 foreach ($errors as $error) {
60 drupal_set_message($error, 'error');
61 }
62 }
63 // Register the standard JavaScript callback.
64 $object->__callbacks = array('Drupal.Views.Ajax.ajaxViewResponse');
65 // Allow other modules to extend the data returned.
66 drupal_alter('ajax_data', $object, 'views', $view);
67 }
68 }
69 $messages = theme('status_messages');
9c4eabfc 70 $object->messages = $messages ? '<div class="views-messages">' . $messages . '</div>' : '';
80f01edd
EM
71
72 views_ajax_render($object);
73 }
74}
75
76/**
ddf08198 77 * Simple render function to make sure output is what we want.
288b7db8
EM
78 *
79 * This function renders an object into JSON, and that object contains
80 * commands to the ajax response parser on the other side. The actual
81 * commands that can be sent are completely dependent upon the client
82 * javascript parser, which can be anything, but this function assumes
83 * that 'display', at least, will be displayed in some kind of ajax
84 * spot or popup.
ddf08198 85 */
3a69a04c 86function views_ajax_render($output = NULL, $title = NULL, $url = NULL, $js = NULL) {
ddf08198
EM
87 if (empty($output)) {
88 $output->display = t('Server reports invalid input error.');
89 $output->title = t('Error');
90 }
91 elseif (!is_object($output)) {
92 $temp = new stdClass();
93 $temp->display = $output;
94 $temp->title = $title;
95 $temp->url = $url;
96 $output = $temp;
97 }
3a69a04c
EM
98 if (!empty($js)) {
99 $output->js = $js;
100 }
101
cb2cdc11 102 drupal_json($output);
ddf08198
EM
103 exit;
104}
105
ddf08198 106/**
06b9f62e
EM
107 * Wrapper around drupal_build_form to handle some AJAX stuff automatically.
108 * This makes some assumptions about the client.
ddf08198 109 */
06b9f62e
EM
110function views_ajax_form_wrapper($form_id, &$form_state) {
111 // This won't override settings already in.
112 $form_state += array(
113 're_render' => FALSE,
114 'no_redirect' => !empty($form_state['ajax']),
115 );
116
117 $output = drupal_build_form($form_id, $form_state);
118 if (!empty($form_state['ajax']) && empty($form_state['executed'])) {
bd1728c3 119 // If the form didn't execute and we're using ajax, build up a
06b9f62e
EM
120 // json command object to render.
121 $object = new stdClass();
122 $object->display = '';
123 if ($messages = theme('status_messages')) {
124 $object->display = '<div class="views-messages">' . $messages . '</div>';
125 }
126 $object->display .= $output;
ddf08198 127
06b9f62e 128 $object->title = empty($form_state['title']) ? '' : $form_state['title'];
c44188ce
EM
129 if (!empty($form_state['help_topic'])) {
130 $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
131 $object->title = theme('advanced_help_topic', $module, $form_state['help_topic']) . $object->title;
132 }
06b9f62e
EM
133 $object->url = empty($form_state['url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form_state['url'];
134 $object->js = empty($form_state['js settings']) ? NULL : $form_state['js settings'];
135 if (!empty($form_state['#section'])) {
136 $object->hilite = '.' . views_ui_item_css($form_state['#section']);
ddf08198 137 }
ddf08198 138
06b9f62e 139 $output = $object;
999b2e53 140 }
3a69a04c 141
06b9f62e
EM
142 // These forms have the title built in, so set the title here:
143 if (empty($form_state['ajax']) && !empty($form_state['title'])) {
144 drupal_set_title($form_state['title']);
999b2e53
EM
145 }
146
d3b06159
EM
147 return $output;
148}
149
47e6006d
EM
150
151/**
152 * Page callback for views user autocomplete
153 */
154function views_ajax_autocomplete_user($string = '') {
155 // The user enters a comma-separated list of tags. We only autocomplete the last tag.
156 $array = drupal_explode_tags($string);
157
158 // Fetch last tag
159 $last_string = trim(array_pop($array));
160 $matches = array();
161 if ($last_string != '') {
162 $prefix = count($array) ? implode(', ', $array) . ', ' : '';
163
164 if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
165 $matches[$prefix . 'Anonymous'] = 'Anonymous';
166 }
167 $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $last_string, 0, 10);
168
169 while ($account = db_fetch_object($result)) {
170 $n = $account->name;
171 // Commas and quotes in terms are special cases, so encode 'em.
172 if (strpos($account->name, ',') !== FALSE || strpos($account->name, '"') !== FALSE) {
173 $n = '"' . str_replace('"', '""', $account->name) . '"';
174 }
175 $matches[$prefix . $n] = check_plain($account->name);
176 }
177 }
178
179 drupal_json($matches);
180}
181
d3b06159 182/**
ddf08198 183 * @}
72ec0beb 184 */