ajax.inc 11 KB

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