| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @file ajax.inc 5 * 6 * Handles the server side AJAX interactions of Views. 7 * 8 * @defgroup ajax Views ajax library 9 * @{ 10 */ 11 12 /** 13 * Menu callback to load a view via AJAX. 14 */ 15 function 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']; 19 $args = isset($_REQUEST['view_args']) && $_REQUEST['view_args'] !== '' ? explode('/', $_REQUEST['view_args']) : array(); 20 $path = isset($_REQUEST['view_path']) ? $_REQUEST['view_path'] : NULL; 21 $dom_id = isset($_REQUEST['view_dom_id']) ? intval($_REQUEST['view_dom_id']) : NULL; 22 $pager_element = isset($_REQUEST['pager_element']) ? intval($_REQUEST['pager_element']) : NULL; 23 views_include('ajax'); 24 $object = new stdClass(); 25 26 $object->status = FALSE; 27 $object->display = ''; 28 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 } 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 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 52 $errors = $view->validate(); 53 if ($errors === TRUE) { 54 $object->status = TRUE; 55 $object->display .= $view->preview($display_id, $args); 56 // Get the title after the preview call, to let it set 57 // up both the view's current display and arguments 58 $object->title = $view->get_title(); 59 } 60 else { 61 foreach ($errors as $error) { 62 drupal_set_message($error, 'error'); 63 } 64 } 65 // Register the standard JavaScript callback. 66 $object->__callbacks = array('Drupal.Views.Ajax.ajaxViewResponse'); 67 // Allow other modules to extend the data returned. 68 drupal_alter('ajax_data', $object, 'views', $view); 69 } 70 } 71 $messages = theme('status_messages'); 72 $object->messages = $messages ? '<div class="views-messages">' . $messages . '</div>' : ''; 73 74 views_ajax_render($object); 75 } 76 } 77 78 /** 79 * Simple render function to make sure output is what we want. 80 * 81 * This function renders an object into JSON, and that object contains 82 * commands to the ajax response parser on the other side. The actual 83 * commands that can be sent are completely dependent upon the client 84 * javascript parser, which can be anything, but this function assumes 85 * that 'display', at least, will be displayed in some kind of ajax 86 * spot or popup. 87 */ 88 function views_ajax_render($output = NULL, $title = NULL, $url = NULL, $js = NULL) { 89 if (empty($output)) { 90 $output->display = t('Server reports invalid input error.'); 91 $output->title = t('Error'); 92 } 93 elseif (!is_object($output)) { 94 $temp = new stdClass(); 95 $temp->display = $output; 96 $temp->title = $title; 97 $temp->url = $url; 98 $output = $temp; 99 } 100 if (!empty($js)) { 101 $output->js = $js; 102 } 103 104 drupal_json($output); 105 exit; 106 } 107 108 /** 109 * Wrapper around drupal_build_form to handle some AJAX stuff automatically. 110 * This makes some assumptions about the client. 111 */ 112 function views_ajax_form_wrapper($form_id, &$form_state) { 113 // This won't override settings already in. 114 $form_state += array( 115 'rerender' => FALSE, 116 'no_redirect' => !empty($form_state['ajax']), 117 ); 118 119 $output = drupal_build_form($form_id, $form_state); 120 if (!empty($form_state['ajax']) && empty($form_state['executed'])) { 121 // If the form didn't execute and we're using ajax, build up a 122 // json command object to render. 123 $object = new stdClass(); 124 $object->display = ''; 125 if ($messages = theme('status_messages')) { 126 $object->display = '<div class="views-messages">' . $messages . '</div>'; 127 } 128 $object->display .= $output; 129 130 $object->title = empty($form_state['title']) ? '' : $form_state['title']; 131 if (!empty($form_state['help_topic'])) { 132 $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views'; 133 $object->title = theme('advanced_help_topic', $module, $form_state['help_topic']) . $object->title; 134 } 135 $object->url = empty($form_state['url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form_state['url']; 136 $object->js = empty($form_state['js settings']) ? NULL : $form_state['js settings']; 137 if (!empty($form_state['#section'])) { 138 $object->hilite = '.' . views_ui_item_css($form_state['#section']); 139 } 140 141 $output = $object; 142 } 143 144 // These forms have the title built in, so set the title here: 145 if (empty($form_state['ajax']) && !empty($form_state['title'])) { 146 drupal_set_title($form_state['title']); 147 } 148 149 return $output; 150 } 151 152 153 /** 154 * Page callback for views user autocomplete 155 */ 156 function views_ajax_autocomplete_user($string = '') { 157 // The user enters a comma-separated list of tags. We only autocomplete the last tag. 158 $array = drupal_explode_tags($string); 159 160 // Fetch last tag 161 $last_string = trim(array_pop($array)); 162 $matches = array(); 163 if ($last_string != '') { 164 $prefix = count($array) ? implode(', ', $array) . ', ' : ''; 165 166 if (strpos('anonymous', strtolower($last_string)) !== FALSE) { 167 $matches[$prefix . 'Anonymous'] = 'Anonymous'; 168 } 169 $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $last_string, 0, 10); 170 171 while ($account = db_fetch_object($result)) { 172 $n = $account->name; 173 // Commas and quotes in terms are special cases, so encode 'em. 174 if (strpos($account->name, ',') !== FALSE || strpos($account->name, '"') !== FALSE) { 175 $n = '"' . str_replace('"', '""', $account->name) . '"'; 176 } 177 $matches[$prefix . $n] = check_plain($account->name); 178 } 179 } 180 181 drupal_json($matches); 182 } 183 184 /** 185 * @} 186 */
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |