search_api_ajax.pages.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Checks incoming AJAX requests for Search API pages paths
  4. */
  5. function search_api_ajax_proxy($id, $keys = NULL) {
  6. $page = search_api_page_load((int)$id);
  7. if (!$page) {
  8. return MENU_NOT_FOUND;
  9. }
  10. $return = menu_execute_active_handler($page->path . '/' . $keys, FALSE);
  11. search_api_ajax_return_json($return);
  12. }
  13. /**
  14. * Checks incoming AJAX requests for Search API views paths
  15. */
  16. function search_api_ajax_proxy_views($name, $display, $keys = NULL) {
  17. $view = views_get_view($name);
  18. if (!$view) {
  19. return MENU_NOT_FOUND;
  20. }
  21. $path = search_api_ajax_get_views_path($view->display[$display]->display_options['path'], 1);
  22. if ($keys) {
  23. $path = $path . '/' . $keys;
  24. }
  25. $return = menu_execute_active_handler($path, FALSE);
  26. search_api_ajax_return_json($return);
  27. }
  28. /**
  29. * Performs a search request and returns page & block content html as JSON
  30. *
  31. * If the incoming request is not an AJAX request, user/bot is redirected
  32. * to the non-Ajax URL.
  33. *
  34. * @see search_api_ajax_modules()
  35. */
  36. function search_api_ajax_return_json($return) {
  37. // check request source type: non-ajax is redirect back to non-ajax URL
  38. if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
  39. $params = drupal_get_query_parameters();
  40. $path = str_replace('search_api_ajax/', '', $_GET['q']);
  41. drupal_goto($path, array('query' => $params), 301);
  42. exit ;
  43. }
  44. if (is_int($return)) {
  45. switch ($return) {
  46. case MENU_NOT_FOUND :
  47. drupal_add_http_header('Status', '404 Not Found');
  48. break;
  49. case MENU_ACCESS_DENIED :
  50. drupal_add_http_header('Status', '403 Forbidden');
  51. break;
  52. case MENU_SITE_OFFLINE :
  53. drupal_add_http_header('Status', '503 Service unavailable');
  54. break;
  55. }
  56. }
  57. elseif (isset($return)) {
  58. global $theme;
  59. if (!isset($theme)) {
  60. init_theme();
  61. }
  62. // render JSON (views are already rendered)
  63. if (is_array($return)) {
  64. $return = drupal_render($return);
  65. }
  66. $json = array('regions' => array('search_api_ajax' => $return));
  67. // add new blocks
  68. $blocks = array();
  69. // Drupal blocks
  70. // @todo cache this?
  71. $regions = system_region_list($theme);
  72. foreach ($regions as $region_key => $region_name) {
  73. $settings['regions'][] = $region_key;
  74. $block_list = block_list($region_key);
  75. // remove inactive blocks (status=0)
  76. foreach ($block_list as $key => $block) {
  77. if ($block->status == 0) {
  78. unset($block_list[$key]);
  79. }
  80. }
  81. $blocks = array_merge(block_list($region_key), $blocks);
  82. if (module_exists('context')) {
  83. $blocks = array_merge(context_get_plugin('reaction', 'block')->block_list($region_key), $blocks);
  84. }
  85. }
  86. $modules = search_api_ajax_modules();
  87. foreach ($blocks as $block) {
  88. if (in_array($block->module, $modules)) {
  89. $rendered_block = _block_render_blocks(array($block));
  90. $rendered_block = _block_get_renderable_array($rendered_block);
  91. $json['regions'][$block->region][$block->module . '_' . $block->delta] = drupal_render($rendered_block);
  92. // new blocks may have come into existence, we notify about them
  93. // otherwise they would never be displayed to the user
  94. // @see search_api_ajax.js
  95. $block_delta = strtolower($block->delta);
  96. $json['blocks'][$block->module . '_' . $block->delta] = str_replace('_', '-', '#block-' . $block->module . '-' . $block->delta);
  97. }
  98. }
  99. // merge all scopes and settings
  100. $javascript = drupal_add_js(NULL, NULL, NULL);
  101. $scopes = call_user_func_array('array_merge_recursive', $javascript);
  102. $json['settings'] = call_user_func_array('array_merge_recursive', array($scopes['data']));
  103. $json['debug'] = search_api_ajax_debug();
  104. drupal_add_http_header('Content-Type', 'application/json; charset=utf-8');
  105. print drupal_json_encode($json);
  106. }
  107. }