ajax.inc 11 KB

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