00001 <?php
00002
00015 function views_ajax() {
00016 if (isset($_REQUEST['view_name']) && isset($_REQUEST['view_display_id'])) {
00017 $name = $_REQUEST['view_name'];
00018 $display_id = $_REQUEST['view_display_id'];
00019 $args = isset($_REQUEST['view_args']) && $_REQUEST['view_args'] !== '' ? explode('/', $_REQUEST['view_args']) : array();
00020 $path = isset($_REQUEST['view_path']) ? $_REQUEST['view_path'] : NULL;
00021 $dom_id = isset($_REQUEST['view_dom_id']) ? preg_replace('/[^a-zA-Z0-9_-]+/', '-', $_REQUEST['view_dom_id']) : NULL;
00022 $pager_element = isset($_REQUEST['pager_element']) ? intval($_REQUEST['pager_element']) : NULL;
00023
00024 $commands = array();
00025
00026 $arg = explode('/', $_REQUEST['view_path']);
00027
00028
00029 foreach (array('view_name', 'view_display_id', 'view_args', 'view_path', 'view_dom_id', 'pager_element', 'view_base_path', 'ajax_html_ids', 'ajax_page_state') as $key) {
00030 if (isset($_GET[$key])) {
00031 unset($_GET[$key]);
00032 }
00033 if (isset($_REQUEST[$key])) {
00034 unset($_REQUEST[$key]);
00035 }
00036 if (isset($_POST[$key])) {
00037 unset($_POST[$key]);
00038 }
00039 }
00040
00041
00042 $view = views_get_view($name);
00043 if ($view && $view->access($display_id)) {
00044
00045 if (!empty($path)) {
00046 $_GET['q'] = $path;
00047 }
00048
00049
00050
00051 $_GET += $_POST;
00052
00053
00054
00055 $origin_destination = $path;
00056 $query = drupal_http_build_query($_REQUEST);
00057 if ($query != '') {
00058 $origin_destination .= '?' . $query;
00059 }
00060 $destination = &drupal_static('drupal_get_destination');
00061 $destination = array('destination' => $origin_destination);
00062
00063
00064 if (isset($pager_element)) {
00065 $commands[] = views_ajax_command_scroll_top('.view-dom-id-' . $dom_id);
00066 $view->display[$display_id]->handler->set_option('pager_element', $pager_element);
00067 }
00068
00069 $view->dom_id = $dom_id;
00070
00071 $commands[] = ajax_command_replace('.view-dom-id-' . $dom_id, $view->preview($display_id, $args));
00072 }
00073 drupal_alter('views_ajax_data', $commands, $view);
00074 return array('#type' => 'ajax', '#commands' => $commands);
00075 }
00076 }
00077
00091 function views_ajax_command_set_form($output, $title, $url = NULL) {
00092 $command = array(
00093 'command' => 'viewsSetForm',
00094 'output' => $output,
00095 'title' => $title,
00096 );
00097 if (isset($url)) {
00098 $command['url'] = $url;
00099 }
00100 return $command;
00101 }
00102
00109 function views_ajax_command_dismiss_form() {
00110 $command = array(
00111 'command' => 'viewsDismissForm',
00112 );
00113 return $command;
00114 }
00115
00125 function views_ajax_command_hilite($selector) {
00126 return array(
00127 'command' => 'viewsHilite',
00128 'selector' => $selector,
00129 );
00130 }
00131
00145 function views_ajax_command_add_tab($id, $title, $body) {
00146 $command = array(
00147 'command' => 'viewsAddTab',
00148 'id' => $id,
00149 'title' => $title,
00150 'body' => $body,
00151 );
00152 return $command;
00153 }
00154
00161 function views_ajax_command_scroll_top($selector) {
00162 $command = array(
00163 'command' => 'viewsScrollTop',
00164 'selector' => $selector,
00165 );
00166 return $command;
00167 }
00168
00175 function views_ajax_command_show_buttons() {
00176 $command = array(
00177 'command' => 'viewsShowButtons',
00178 );
00179 return $command;
00180 }
00181
00188 function views_ajax_command_trigger_preview() {
00189 $command = array(
00190 'command' => 'viewsTriggerPreview',
00191 );
00192 return $command;
00193 }
00194
00201 function views_ajax_command_replace_title($title) {
00202 $command = array(
00203 'command' => 'viewsReplaceTitle',
00204 'title' => $title,
00205 'siteName' => variable_get('site_name', 'Drupal'),
00206 );
00207 return $command;
00208 }
00209
00213 function views_ajax_error($message) {
00214 $commands = array();
00215 $commands[] = views_ajax_command_set_form($message, t('Error'));
00216 return $commands;
00217 }
00218
00223 function views_ajax_form_wrapper($form_id, &$form_state) {
00224 ctools_include('dependent');
00225
00226
00227 $form_state += array(
00228 'rerender' => FALSE,
00229 'no_redirect' => !empty($form_state['ajax']),
00230 'no_cache' => TRUE,
00231 'build_info' => array(
00232 'args' => array(),
00233 ),
00234 );
00235
00236 $form = drupal_build_form($form_id, $form_state);
00237 $output = drupal_render($form);
00238
00239
00240 if (empty($form_state['ajax']) && !empty($form_state['title'])) {
00241 drupal_set_title($form_state['title']);
00242 drupal_add_css(drupal_get_path('module', 'views_ui') . '/css/views-admin.css');
00243 }
00244
00245 if (!empty($form_state['ajax']) && (empty($form_state['executed']) || !empty($form_state['rerender']))) {
00246
00247
00248 $commands = array();
00249
00250 $display = '';
00251 if ($messages = theme('status_messages')) {
00252 $display = '<div class="views-messages">' . $messages . '</div>';
00253 }
00254 $display .= $output;
00255
00256 $title = empty($form_state['title']) ? '' : $form_state['title'];
00257 if (!empty($form_state['help_topic'])) {
00258 $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
00259 if (module_exists('advanced_help')) {
00260 $title = theme('advanced_help_topic', array('module' => $module, 'topic' => $form_state['help_topic'])) . $title;
00261 }
00262 }
00263
00264 $url = empty($form_state['url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form_state['url'];
00265
00266 $commands[] = views_ajax_command_set_form($display, $title, $url);
00267
00268 if (!empty($form_state['#section'])) {
00269 $commands[] = views_ajax_command_hilite('.' . drupal_clean_css_identifier($form_state['#section']));
00270 }
00271
00272 return $commands;
00273 }
00274
00275
00276 if (empty($form_state['ajax']) && !empty($form_state['title'])) {
00277 drupal_set_title($form_state['title']);
00278 }
00279
00280 return $output;
00281 }
00282
00283
00287 function views_ajax_autocomplete_user($string = '') {
00288
00289 $array = drupal_explode_tags($string);
00290
00291
00292 $last_string = trim(array_pop($array));
00293 $matches = array();
00294 if ($last_string != '') {
00295 $prefix = count($array) ? implode(', ', $array) . ', ' : '';
00296
00297 if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
00298 $matches[$prefix . 'Anonymous'] = 'Anonymous';
00299 }
00300
00301 $result = db_select('users', 'u')
00302 ->fields('u', array('uid', 'name'))
00303 ->condition('u.name', db_like($last_string) . '%', 'LIKE')
00304 ->range(0, 10)
00305 ->execute()
00306 ->fetchAllKeyed();
00307
00308 foreach ($result as $account) {
00309 $n = $account;
00310
00311 if (strpos($account, ',') !== FALSE || strpos($account, '"') !== FALSE) {
00312 $n = '"' . str_replace('"', '""', $account) . '"';
00313 }
00314 $matches[$prefix . $n] = check_plain($account);
00315 }
00316 }
00317
00318 drupal_json_output($matches);
00319 }
00320
00332 function views_ajax_autocomplete_taxonomy($vid, $tags_typed = '') {
00333
00334 $tags_typed = drupal_explode_tags($tags_typed);
00335 $tag_last = drupal_strtolower(array_pop($tags_typed));
00336
00337 $matches = array();
00338 if ($tag_last != '') {
00339
00340 $query = db_select('taxonomy_term_data', 't');
00341 $query->addTag('translatable');
00342 $query->addTag('term_access');
00343
00344
00345 if (!empty($tags_typed)) {
00346 $query->condition('t.name', $tags_typed, 'NOT IN');
00347 }
00348
00349 $tags_return = $query
00350 ->fields('t', array('tid', 'name'))
00351 ->condition('t.vid', $vid)
00352 ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
00353 ->range(0, 10)
00354 ->execute()
00355 ->fetchAllKeyed();
00356
00357 $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
00358
00359 $term_matches = array();
00360 foreach ($tags_return as $tid => $name) {
00361 $n = $name;
00362
00363 if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
00364 $n = '"' . str_replace('"', '""', $name) . '"';
00365 }
00366
00367 $term_matches[$prefix . $n] = check_plain($name);
00368 }
00369 }
00370
00371 drupal_json_output($term_matches);
00372 }
00373