123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- /**
- * @file
- * Handles the server side AJAX interactions of Views.
- */
- /**
- * @defgroup ajax Views AJAX library
- * @{
- * Handles the server side AJAX interactions of Views.
- */
- /**
- * Menu callback to load a view via AJAX.
- */
- function views_ajax() {
- if (isset($_REQUEST['view_name']) && isset($_REQUEST['view_display_id'])) {
- $name = $_REQUEST['view_name'];
- $display_id = $_REQUEST['view_display_id'];
- $args = isset($_REQUEST['view_args']) && $_REQUEST['view_args'] !== '' ? explode('/', htmlspecialchars_decode($_REQUEST['view_args'], ENT_QUOTES)) : array();
- $path = isset($_REQUEST['view_path']) ? htmlspecialchars_decode($_REQUEST['view_path'], ENT_QUOTES) : NULL;
- $dom_id = isset($_REQUEST['view_dom_id']) ? preg_replace('/[^a-zA-Z0-9_-]+/', '-', $_REQUEST['view_dom_id']) : NULL;
- $pager_element = isset($_REQUEST['pager_element']) ? intval($_REQUEST['pager_element']) : NULL;
- $commands = array();
- // Remove all of this stuff from $_GET so it doesn't end up in pagers and
- // tablesort URLs.
- 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) {
- if (isset($_GET[$key])) {
- unset($_GET[$key]);
- }
- if (isset($_REQUEST[$key])) {
- unset($_REQUEST[$key]);
- }
- if (isset($_POST[$key])) {
- unset($_POST[$key]);
- }
- }
- // Load the view.
- $view = views_get_view($name);
- if ($view && $view->access($display_id) && $view->set_display($display_id) && $view->display_handler->use_ajax()) {
- // Fix 'q' for paging.
- if (!empty($path)) {
- $_GET['q'] = $path;
- }
- // If page parameter is in the $_POST exclude it from $_GET, otherwise
- // support views_ajax requests using $_GET.
- $exclude = isset($_POST['page']) ? array('page') : array();
- // Add all $_POST data to $_GET as many things, such as tablesorts,
- // exposed filters and paging assume $_GET.
- $_GET = $_POST + drupal_get_query_parameters($_GET, $exclude);
- // Overwrite the destination.
- // @see drupal_get_destination()
- $origin_destination = $path;
- $query = drupal_http_build_query(drupal_get_query_parameters());
- if ($query != '') {
- $origin_destination .= '?' . $query;
- }
- $destination = &drupal_static('drupal_get_destination');
- $destination = array('destination' => $origin_destination);
- // Override the display's pager_element with the one actually used.
- if (isset($pager_element)) {
- $commands[] = views_ajax_command_scroll_top('.view-dom-id-' . $dom_id);
- $view->display[$display_id]->handler->set_option('pager_element', $pager_element);
- }
- // Reuse the same DOM id so it matches that in Drupal.settings.
- $view->dom_id = $dom_id;
- // Always return HTML with the same DOM ID that was sent by the browser.
- $commands[] = ajax_command_replace('.view-dom-id-' . $dom_id, preg_replace('/view-dom-id-[a-zA-Z0-9_-]+/', 'view-dom-id-' . $view->dom_id, $view->preview($display_id, $args), 1));
- }
- drupal_alter('views_ajax_data', $commands, $view);
- return array('#type' => 'ajax', '#commands' => $commands);
- }
- }
- /**
- * Creates a Drupal AJAX 'viewsSetForm' command.
- *
- * @param string $output
- * The form to display in the modal.
- * @param string $title
- * The title.
- * @param string $url
- * An optional URL.
- *
- * @return array
- * An array suitable for use with the ajax_render() function.
- */
- function views_ajax_command_set_form($output, $title, $url = NULL) {
- $command = array(
- 'command' => 'viewsSetForm',
- 'output' => $output,
- 'title' => $title,
- );
- if (isset($url)) {
- $command['url'] = $url;
- }
- return $command;
- }
- /**
- * Creates a Drupal AJAX 'viewsDismissForm' command.
- *
- * @return array
- * An array suitable for use with the ajax_render() function.
- */
- function views_ajax_command_dismiss_form() {
- $command = array(
- 'command' => 'viewsDismissForm',
- );
- return $command;
- }
- /**
- * Creates a Drupal AJAX 'viewsHilite' command.
- *
- * @param string $selector
- * The selector to highlight.
- *
- * @return
- * An array suitable for use with the ajax_render() function.
- */
- function views_ajax_command_hilite($selector) {
- return array(
- 'command' => 'viewsHilite',
- 'selector' => $selector,
- );
- }
- /**
- * Creates a Drupal AJAX 'addTab' command.
- *
- * @param string $id
- * The DOM ID.
- * @param string $title
- * The title.
- * @param string $body
- * The body.
- *
- * @return array
- * An array suitable for use with the ajax_render() function.
- */
- function views_ajax_command_add_tab($id, $title, $body) {
- $command = array(
- 'command' => 'viewsAddTab',
- 'id' => $id,
- 'title' => $title,
- 'body' => $body,
- );
- return $command;
- }
- /**
- * Scroll to top of the current view.
- *
- * @return array
- * An array suitable for use with the ajax_render() function.
- */
- function views_ajax_command_scroll_top($selector) {
- $command = array(
- 'command' => 'viewsScrollTop',
- 'selector' => $selector,
- );
- return $command;
- }
- /**
- * Shows Save and Cancel buttons.
- *
- * @param bool $changed
- * Whether of not the view has changed.
- *
- * @return array
- * An array suitable for use with the ajax_render() function.
- */
- function views_ajax_command_show_buttons($changed) {
- $command = array(
- 'command' => 'viewsShowButtons',
- 'changed' => (bool) $changed,
- );
- return $command;
- }
- /**
- * Trigger the Views live preview.
- *
- * @return array
- * An array suitable for use with the ajax_render() function.
- */
- function views_ajax_command_trigger_preview() {
- $command = array(
- 'command' => 'viewsTriggerPreview',
- );
- return $command;
- }
- /**
- * Replace the page title.
- *
- * @return array
- * An array suitable for use with the ajax_render() function.
- */
- function views_ajax_command_replace_title($title) {
- $command = array(
- 'command' => 'viewsReplaceTitle',
- 'title' => $title,
- 'siteName' => variable_get('site_name', 'Drupal'),
- );
- return $command;
- }
- /**
- * Return an AJAX error.
- *
- * @param string $message
- * The message to display.
- *
- * @return array
- * An array suitable for use with the ajax_render() function.
- */
- function views_ajax_error($message) {
- $commands = array();
- $commands[] = views_ajax_command_set_form($message, t('Error'));
- return $commands;
- }
- /**
- * Wrapper around drupal_build_form to handle some AJAX stuff automatically.
- *
- * This makes some assumptions about the client.
- */
- function views_ajax_form_wrapper($form_id, &$form_state) {
- ctools_include('dependent');
- // This won't override settings already in.
- $form_state += array(
- 'rerender' => FALSE,
- 'no_redirect' => !empty($form_state['ajax']),
- 'no_cache' => TRUE,
- 'build_info' => array(
- 'args' => array(),
- ),
- );
- $form = drupal_build_form($form_id, $form_state);
- $output = drupal_render($form);
- // These forms have the title built in, so set the title here.
- if (empty($form_state['ajax']) && !empty($form_state['title'])) {
- drupal_set_title($form_state['title']);
- drupal_add_css(drupal_get_path('module', 'views_ui') . '/css/views-admin.css');
- }
- if (!empty($form_state['ajax']) && (empty($form_state['executed']) || !empty($form_state['rerender']))) {
- // If the form didn't execute and we're using ajax, build up a AJAX command
- // list to execute.
- $commands = array();
- $display = '';
- if ($messages = theme('status_messages')) {
- $display = '<div class="views-messages">' . $messages . '</div>';
- }
- $display .= $output;
- $title = empty($form_state['title']) ? '' : $form_state['title'];
- if (!empty($form_state['help_topic'])) {
- $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
- if (module_exists('advanced_help')) {
- $title = theme('advanced_help_topic', array('module' => $module, 'topic' => $form_state['help_topic'])) . $title;
- }
- }
- $url = empty($form_state['url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form_state['url'];
- $commands[] = views_ajax_command_set_form($display, $title, $url);
- if (!empty($form_state['#section'])) {
- $commands[] = views_ajax_command_hilite('.' . drupal_clean_css_identifier($form_state['#section']));
- }
- return $commands;
- }
- // These forms have the title built in, so set the title here.
- if (empty($form_state['ajax']) && !empty($form_state['title'])) {
- drupal_set_title($form_state['title']);
- }
- return $output;
- }
- /**
- * Page callback for views user autocomplete.
- */
- function views_ajax_autocomplete_user($string = '') {
- // The user enters a comma-separated list of user name. We only autocomplete
- // the last name.
- $array = drupal_explode_tags($string);
- // Fetch last name.
- $last_string = trim(array_pop($array));
- $matches = array();
- if ($last_string != '') {
- $prefix = count($array) ? implode(', ', $array) . ', ' : '';
- if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
- $matches[$prefix . 'Anonymous'] = 'Anonymous';
- }
- $result = db_select('users', 'u')
- ->fields('u', array('uid', 'name'))
- ->condition('u.name', db_like($last_string) . '%', 'LIKE')
- ->range(0, 10)
- ->execute()
- ->fetchAllKeyed();
- foreach ($result as $account) {
- $n = $account;
- // Commas and quotes in terms are special cases, so encode 'em.
- if (strpos($account, ',') !== FALSE || strpos($account, '"') !== FALSE) {
- $n = '"' . str_replace('"', '""', $account) . '"';
- }
- $matches[$prefix . $n] = check_plain($account);
- }
- }
- drupal_json_output($matches);
- }
- /**
- * Page callback for views taxonomy autocomplete.
- *
- * @param int $vid
- * The vocabulary id of the tags which should be returned.
- * @param string $tags_typed
- * The typed string of the user.
- *
- * @see taxonomy_autocomplete()
- */
- function views_ajax_autocomplete_taxonomy($vid, $tags_typed = '') {
- // The user enters a comma-separated list of tags. We only autocomplete the
- // last tag.
- $tags_typed = drupal_explode_tags($tags_typed);
- $tag_last = drupal_strtolower(array_pop($tags_typed));
- $matches = array();
- if ($tag_last != '') {
- $query = db_select('taxonomy_term_data', 't');
- $query->addTag('translatable');
- $query->addTag('taxonomy_term_access');
- // Do not select already entered terms.
- if (!empty($tags_typed)) {
- $query->condition('t.name', $tags_typed, 'NOT IN');
- }
- // Select rows that match by term name.
- $tags_return = $query
- ->fields('t', array('tid', 'name'))
- ->condition('t.vid', $vid)
- ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
- ->range(0, 10)
- ->execute()
- ->fetchAllKeyed();
- $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
- $term_matches = array();
- foreach ($tags_return as $tid => $name) {
- $n = $name;
- // Term names containing commas or quotes must be wrapped in quotes.
- if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
- $n = '"' . str_replace('"', '""', $name) . '"';
- }
- // Add term name to list of matches.
- $term_matches[$prefix . $n] = check_plain($name);
- }
- }
- drupal_json_output($term_matches);
- }
- /**
- * @}
- */
|