/[drupal]/contributions/modules/views/includes/ajax.inc
ViewVC logotype

Contents of /contributions/modules/views/includes/ajax.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


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

  ViewVC Help
Powered by ViewVC 1.1.2