ajax.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 + drupal_get_query_parameters($_GET, array('page'));
  45. // Overwrite the destination.
  46. // @see drupal_get_destination()
  47. $origin_destination = $path;
  48. $query = drupal_http_build_query(drupal_get_query_parameters());
  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. * @param bool $changed
  157. * Whether of not the view has changed.
  158. *
  159. * @return
  160. * An array suitable for use with the ajax_render() function.
  161. */
  162. function views_ajax_command_show_buttons($changed) {
  163. $command = array(
  164. 'command' => 'viewsShowButtons',
  165. 'changed' => (bool) $changed,
  166. );
  167. return $command;
  168. }
  169. /**
  170. * Trigger the Views live preview.
  171. *
  172. * @return
  173. * An array suitable for use with the ajax_render() function.
  174. */
  175. function views_ajax_command_trigger_preview() {
  176. $command = array(
  177. 'command' => 'viewsTriggerPreview',
  178. );
  179. return $command;
  180. }
  181. /**
  182. * Replace the page title.
  183. *
  184. * @return
  185. * An array suitable for use with the ajax_render() function.
  186. */
  187. function views_ajax_command_replace_title($title) {
  188. $command = array(
  189. 'command' => 'viewsReplaceTitle',
  190. 'title' => $title,
  191. 'siteName' => variable_get('site_name', 'Drupal'),
  192. );
  193. return $command;
  194. }
  195. /**
  196. * Return an AJAX error.
  197. */
  198. function views_ajax_error($message) {
  199. $commands = array();
  200. $commands[] = views_ajax_command_set_form($message, t('Error'));
  201. return $commands;
  202. }
  203. /**
  204. * Wrapper around drupal_build_form to handle some AJAX stuff automatically.
  205. * This makes some assumptions about the client.
  206. */
  207. function views_ajax_form_wrapper($form_id, &$form_state) {
  208. ctools_include('dependent');
  209. // This won't override settings already in.
  210. $form_state += array(
  211. 'rerender' => FALSE,
  212. 'no_redirect' => !empty($form_state['ajax']),
  213. 'no_cache' => TRUE,
  214. 'build_info' => array(
  215. 'args' => array(),
  216. ),
  217. );
  218. $form = drupal_build_form($form_id, $form_state);
  219. $output = drupal_render($form);
  220. // These forms have the title built in, so set the title here:
  221. if (empty($form_state['ajax']) && !empty($form_state['title'])) {
  222. drupal_set_title($form_state['title']);
  223. drupal_add_css(drupal_get_path('module', 'views_ui') . '/css/views-admin.css');
  224. }
  225. if (!empty($form_state['ajax']) && (empty($form_state['executed']) || !empty($form_state['rerender']))) {
  226. // If the form didn't execute and we're using ajax, build up a
  227. // Ajax command list to execute.
  228. $commands = array();
  229. $display = '';
  230. if ($messages = theme('status_messages')) {
  231. $display = '<div class="views-messages">' . $messages . '</div>';
  232. }
  233. $display .= $output;
  234. $title = empty($form_state['title']) ? '' : $form_state['title'];
  235. if (!empty($form_state['help_topic'])) {
  236. $module = !empty($form_state['help_module']) ? $form_state['help_module'] : 'views';
  237. if (module_exists('advanced_help')) {
  238. $title = theme('advanced_help_topic', array('module' => $module, 'topic' => $form_state['help_topic'])) . $title;
  239. }
  240. }
  241. $url = empty($form_state['url']) ? url($_GET['q'], array('absolute' => TRUE)) : $form_state['url'];
  242. $commands[] = views_ajax_command_set_form($display, $title, $url);
  243. if (!empty($form_state['#section'])) {
  244. $commands[] = views_ajax_command_hilite('.' . drupal_clean_css_identifier($form_state['#section']));
  245. }
  246. return $commands;
  247. }
  248. // These forms have the title built in, so set the title here:
  249. if (empty($form_state['ajax']) && !empty($form_state['title'])) {
  250. drupal_set_title($form_state['title']);
  251. }
  252. return $output;
  253. }
  254. /**
  255. * Page callback for views user autocomplete
  256. */
  257. function views_ajax_autocomplete_user($string = '') {
  258. // The user enters a comma-separated list of user name. We only autocomplete the last name.
  259. $array = drupal_explode_tags($string);
  260. // Fetch last name
  261. $last_string = trim(array_pop($array));
  262. $matches = array();
  263. if ($last_string != '') {
  264. $prefix = count($array) ? implode(', ', $array) . ', ' : '';
  265. if (strpos('anonymous', strtolower($last_string)) !== FALSE) {
  266. $matches[$prefix . 'Anonymous'] = 'Anonymous';
  267. }
  268. $result = db_select('users', 'u')
  269. ->fields('u', array('uid', 'name'))
  270. ->condition('u.name', db_like($last_string) . '%', 'LIKE')
  271. ->range(0, 10)
  272. ->execute()
  273. ->fetchAllKeyed();
  274. foreach ($result as $account) {
  275. $n = $account;
  276. // Commas and quotes in terms are special cases, so encode 'em.
  277. if (strpos($account, ',') !== FALSE || strpos($account, '"') !== FALSE) {
  278. $n = '"' . str_replace('"', '""', $account) . '"';
  279. }
  280. $matches[$prefix . $n] = check_plain($account);
  281. }
  282. }
  283. drupal_json_output($matches);
  284. }
  285. /**
  286. * Page callback for views taxonomy autocomplete.
  287. *
  288. * @param $vid
  289. * The vocabulary id of the tags which should be returned.
  290. *
  291. * @param $tags_typed
  292. * The typed string of the user.
  293. *
  294. * @see taxonomy_autocomplete()
  295. */
  296. function views_ajax_autocomplete_taxonomy($vid, $tags_typed = '') {
  297. // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  298. $tags_typed = drupal_explode_tags($tags_typed);
  299. $tag_last = drupal_strtolower(array_pop($tags_typed));
  300. $matches = array();
  301. if ($tag_last != '') {
  302. $query = db_select('taxonomy_term_data', 't');
  303. $query->addTag('translatable');
  304. $query->addTag('term_access');
  305. // Do not select already entered terms.
  306. if (!empty($tags_typed)) {
  307. $query->condition('t.name', $tags_typed, 'NOT IN');
  308. }
  309. // Select rows that match by term name.
  310. $tags_return = $query
  311. ->fields('t', array('tid', 'name'))
  312. ->condition('t.vid', $vid)
  313. ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE')
  314. ->range(0, 10)
  315. ->execute()
  316. ->fetchAllKeyed();
  317. $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : '';
  318. $term_matches = array();
  319. foreach ($tags_return as $tid => $name) {
  320. $n = $name;
  321. // Term names containing commas or quotes must be wrapped in quotes.
  322. if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
  323. $n = '"' . str_replace('"', '""', $name) . '"';
  324. }
  325. // Add term name to list of matches.
  326. $term_matches[$prefix . $n] = check_plain($name);
  327. }
  328. }
  329. drupal_json_output($term_matches);
  330. }
  331. /**
  332. * @}
  333. */