ajax.inc

  1. 3.x includes/ajax.inc
  2. 2.x includes/ajax.inc

ajax.inc

Handles the server side AJAX interactions of Views.

ajax Views ajax library

File

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