ajax.inc

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

Handles the server side AJAX interactions of Views.

File

includes/ajax.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Handles the server side AJAX interactions of Views.
  5. */
  6. /**
  7. * @defgroup ajax Views AJAX library
  8. * @{
  9. * Handles the server side AJAX interactions of Views.
  10. */
  11. /**
  12. * Menu callback to load a view via AJAX.
  13. */
  14. function views_ajax() {
  15. if (isset($_REQUEST['view_name']) && isset($_REQUEST['view_display_id'])) {
  16. $name = $_REQUEST['view_name'];
  17. $display_id = $_REQUEST['view_display_id'];
  18. $args = isset($_REQUEST['view_args']) && $_REQUEST['view_args'] !== '' ? explode('/', $_REQUEST['view_args']) : array();
  19. $path = isset($_REQUEST['view_path']) ? rawurldecode($_REQUEST['view_path']) : NULL;
  20. $dom_id = isset($_REQUEST['view_dom_id']) ? preg_replace('/[^a-zA-Z0-9_-]+/', '-', $_REQUEST['view_dom_id']) : NULL;
  21. $pager_element = isset($_REQUEST['pager_element']) ? intval($_REQUEST['pager_element']) : NULL;
  22. $commands = array();
  23. // Remove all of this stuff from $_GET so it doesn't end up in pagers and tablesort URLs.
  24. 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) {
  25. if (isset($_GET[$key])) {
  26. unset($_GET[$key]);
  27. }
  28. if (isset($_REQUEST[$key])) {
  29. unset($_REQUEST[$key]);
  30. }
  31. if (isset($_POST[$key])) {
  32. unset($_POST[$key]);
  33. }
  34. }
  35. // Load the view.
  36. $view = views_get_view($name);
  37. if ($view && $view->access($display_id)) {
  38. // Fix 'q' for paging.
  39. if (!empty($path)) {
  40. $_GET['q'] = $path;
  41. }
  42. // Add all $_POST data, because AJAX is always a post and many things,
  43. // such as tablesorts, exposed filters and paging assume $_GET.
  44. $_GET = $_POST + $_GET;
  45. // Overwrite the destination.
  46. // @see drupal_get_destination()
  47. $origin_destination = $path;
  48. $query = drupal_http_build_query($_REQUEST);
  49. if ($query != '') {
  50. $origin_destination .= '?' . $query;
  51. }
  52. $destination = &drupal_static('drupal_get_destination');
  53. $destination = array('destination' => $origin_destination);
  54. // Override the display's pager_element with the one actually used.
  55. if (isset($pager_element)) {
  56. $commands[] = views_ajax_command_scroll_top('.view-dom-id-' . $dom_id);
  57. $view->display[$display_id]->handler->set_option('pager_element', $pager_element);
  58. }
  59. // Reuse the same DOM id so it matches that in Drupal.settings.
  60. $view->dom_id = $dom_id;
  61. $commands[] = ajax_command_replace('.view-dom-id-' . $dom_id, $view->preview($display_id, $args));
  62. }
  63. drupal_alter('views_ajax_data', $commands, $view);
  64. return array('#type' => 'ajax', '#commands' => $commands);
  65. }
  66. }
  67. /**
  68. * Creates a Drupal AJAX 'viewsSetForm' command.
  69. *
  70. * @param $output
  71. * The form to display in the modal.
  72. * @param $title
  73. * The title.
  74. * @param $url
  75. * An optional URL.
  76. *
  77. * @return
  78. * An array suitable for use with the ajax_render() function.
  79. */
  80. function views_ajax_command_set_form($output, $title, $url = NULL) {
  81. $command = array(
  82. 'command' => 'viewsSetForm',
  83. 'output' => $output,
  84. 'title' => $title,
  85. );
  86. if (isset($url)) {
  87. $command['url'] = $url;
  88. }
  89. return $command;
  90. }
  91. /**
  92. * Creates a Drupal AJAX 'viewsDismissForm' command.
  93. *
  94. * @return
  95. * An array suitable for use with the ajax_render() function.
  96. */
  97. function views_ajax_command_dismiss_form() {
  98. $command = array(
  99. 'command' => 'viewsDismissForm',
  100. );
  101. return $command;
  102. }
  103. /**
  104. * Creates a Drupal AJAX 'viewsHilite' command.
  105. *
  106. * @param $selector
  107. * The selector to highlight
  108. *
  109. * @return
  110. * An array suitable for use with the ajax_render() function.
  111. */
  112. function views_ajax_command_hilite($selector) {
  113. return array(
  114. 'command' => 'viewsHilite',
  115. 'selector' => $selector,
  116. );
  117. }
  118. /**
  119. * Creates a Drupal AJAX 'addTab' command.
  120. *
  121. * @param $id
  122. * The DOM ID.
  123. * @param $title
  124. * The title.
  125. * @param $body
  126. * The body.
  127. *
  128. * @return
  129. * An array suitable for use with the ajax_render() function.
  130. */
  131. function views_ajax_command_add_tab($id, $title, $body) {
  132. $command = array(
  133. 'command' => 'viewsAddTab',
  134. 'id' => $id,
  135. 'title' => $title,
  136. 'body' => $body,
  137. );
  138. return $command;
  139. }
  140. /**
  141. * Scroll to top of the current view.
  142. *
  143. * @return
  144. * An array suitable for use with the ajax_render() function.
  145. */
  146. function views_ajax_command_scroll_top($selector) {
  147. $command = array(
  148. 'command' => 'viewsScrollTop',
  149. 'selector' => $selector,
  150. );
  151. return $command;
  152. }
  153. /**
  154. * Shows Save and Cancel buttons.
  155. *
  156. * @return
  157. * An array suitable for use with the ajax_render() function.
  158. */
  159. function views_ajax_command_show_buttons() {
  160. $command = array(
  161. 'command' => 'viewsShowButtons',
  162. );
  163. return $command;
  164. }
  165. /**
  166. * Trigger the Views live preview.
  167. *
  168. * @return
  169. * An array suitable for use with the ajax_render() function.
  170. */
  171. function views_ajax_command_trigger_preview() {
  172. $command = array(
  173. 'command' => 'viewsTriggerPreview',
  174. );
  175. return $command;
  176. }
  177. /**
  178. * Replace the page title.
  179. *
  180. * @return
  181. * An array suitable for use with the ajax_render() function.
  182. */
  183. function views_ajax_command_replace_title($title) {
  184. $command = array(
  185. 'command' => 'viewsReplaceTitle',
  186. 'title' => $title,
  187. 'siteName' => variable_get('site_name', 'Drupal'),
  188. );
  189. return $command;
  190. }
  191. /**
  192. * Return an AJAX error.
  193. */
  194. function views_ajax_error($message) {
  195. $commands = array();
  196. $commands[] = views_ajax_command_set_form($message, t('Error'));
  197. return $commands;
  198. }
  199. /**
  200. * Wrapper around drupal_build_form to handle some AJAX stuff automatically.
  201. * This makes some assumptions about the client.
  202. */
  203. function views_ajax_form_wrapper($form_id, &$form_state) {
  204. ctools_include('dependent');
  205. // This won't override settings already in.
  206. $form_state += array(
  207. 'rerender' => FALSE,
  208. 'no_redirect' => !empty($form_state['ajax']),
  209. 'no_cache' => TRUE,
  210. 'build_info' => array(
  211. 'args' => array(),
  212. ),
  213. );
  214. $form = drupal_build_form($form_id, $form_state);
  215. $output = drupal_render($form);
  216. // These forms have the title built in, so set the title here:
  217. if (empty($form_state['ajax']) && !empty($form_state['title'])) {
  218. drupal_set_title($form_state['title']);
  219. drupal_add_css(drupal_get_path('module', 'views_ui') . '/css/views-admin.css');
  220. }
  221. if (!empty($form_state['ajax']) && (empty($form_state['executed']) || !empty($form_state['rerender']))) {
  222. // If the form didn't execute and we're using ajax, build up a
  223. // Ajax command list to execute.
  224. $commands = array();
  225. $display = '';
  226. if ($messages = theme('status_messages')) {
  227. $display = '<div class="views-messages">' . $messages . '</div>';
  228. }
  229. $display .= $output;
  230. $title = empty($form_state['title']) ? '' : $form_state['title'];
  231. if (!empty($form_state['help_topic'])) {
  232. $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
  233. if (module_exists('advanced_help')) {
  234. $title = theme('advanced_help_topic', array('module' => $module, 'topic' => $form_state['help_topic'])) . $title;
  235. }
  236. }
  237. $url = empty($form_state['url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form_state['url'];
  238. $commands[] = views_ajax_command_set_form($display, $title, $url);
  239. if (!empty($form_state['#section'])) {
  240. $commands[] = views_ajax_command_hilite('.' . drupal_clean_css_identifier($form_state['#section']));
  241. }
  242. return $commands;
  243. }
  244. // These forms have the title built in, so set the title here:
  245. if (empty($form_state['ajax']) && !empty($form_state['title'])) {
  246. drupal_set_title($form_state['title']);
  247. }
  248. return $output;
  249. }
  250. /**
  251. * Page callback for views user autocomplete
  252. */
  253. function views_ajax_autocomplete_user($string = '') {
  254. // The user enters a comma-separated list of user name. We only autocomplete the last name.
  255. $array = drupal_explode_tags($string);
  256. // Fetch last name
  257. $last_string = trim(array_pop($array));
  258. $matches = array();
  259. if ($last_string != '') {
  260. $prefix = count($array) ? implode(', ', $array) . ', ' : '';
  261. if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
  262. $matches[$prefix . 'Anonymous'] = 'Anonymous';
  263. }
  264. $result = db_select('users', 'u')
  265. ->fields('u', array('uid', 'name'))
  266. ->condition('u.name', db_like($last_string) . '%', 'LIKE')
  267. ->range(0, 10)
  268. ->execute()
  269. ->fetchAllKeyed();
  270. foreach ($result as $account) {
  271. $n = $account;
  272. // Commas and quotes in terms are special cases, so encode 'em.
  273. if (strpos($account, ',') !== FALSE || strpos($account, '"') !== FALSE) {
  274. $n = '"' . str_replace('"', '""', $account) . '"';
  275. }
  276. $matches[$prefix . $n] = check_plain($account);
  277. }
  278. }
  279. drupal_json_output($matches);
  280. }
  281. /**
  282. * Page callback for views taxonomy autocomplete.
  283. *
  284. * @param $vid
  285. * The vocabulary id of the tags which should be returned.
  286. *
  287. * @param $tags_typed
  288. * The typed string of the user.
  289. *
  290. * @see taxonomy_autocomplete()
  291. */
  292. function views_ajax_autocomplete_taxonomy($vid, $tags_typed = '') {
  293. // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  294. $tags_typed = drupal_explode_tags($tags_typed);
  295. $tag_last = drupal_strtolower(array_pop($tags_typed));
  296. $matches = array();
  297. if ($tag_last != '') {
  298. $query = db_select('taxonomy_term_data', 't');
  299. $query->addTag('translatable');
  300. $query->addTag('term_access');
  301. // Do not select already entered terms.
  302. if (!empty($tags_typed)) {
  303. $query->condition('t.name', $tags_typed, 'NOT IN');
  304. }
  305. // Select rows that match by term name.
  306. $tags_return = $query
  307. ->fields('t', array('tid', 'name'))
  308. ->condition('t.vid', $vid)
  309. ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
  310. ->range(0, 10)
  311. ->execute()
  312. ->fetchAllKeyed();
  313. $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
  314. $term_matches = array();
  315. foreach ($tags_return as $tid => $name) {
  316. $n = $name;
  317. // Term names containing commas or quotes must be wrapped in quotes.
  318. if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
  319. $n = '"' . str_replace('"', '""', $name) . '"';
  320. }
  321. // Add term name to list of matches.
  322. $term_matches[$prefix . $n] = check_plain($name);
  323. }
  324. }
  325. drupal_json_output($term_matches);
  326. }
  327. /**
  328. * @}
  329. */