ajax.inc 11 KB

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