apachesolr_ajax.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/apachesolr_ajax/apachesolr_ajax.module

AJAXifies the Apache Solr module.

Functions & methods

NameDescription
apachesolr_ajax_apachesolr_ajax_enableImplementation of hook_apachesolr_ajax_enable().
apachesolr_ajax_debugCollects debug data to send to the browser with the AJAX response.
apachesolr_ajax_enabledUsing hook_apachesolr_ajax_enable() and hook_apachesolr_hook_disable(), it is possible to enable or disable this module on a per-request basis.
apachesolr_ajax_menuImplementation of hook_menu().
apachesolr_ajax_modulesReturns the list of modules whose blocks are search-related.
apachesolr_ajax_preprocess_pageImplementation of hook_preprocess_page().
apachesolr_ajax_proxyPerforms the search request, and returns the page content and the block content of search-related blocks as JSON.
apachesolr_ajax_themeImplementation of hook_theme().
theme_apachesolr_ajax_jsOverride this function in your theme if you want to swap this module's JavaScript and CSS files for your own.
theme_apachesolr_ajax_yuiYou need only include this markup for Internet Explorer.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * AJAXifies the Apache Solr module.
  6. */
  7. /**
  8. * Implementation of hook_menu().
  9. */
  10. function apachesolr_ajax_menu() {
  11. $items = array();
  12. $items['search/apachesolr_ajax'] = array(
  13. 'page callback' => 'apachesolr_ajax_proxy',
  14. 'access arguments' => array('search content'),
  15. 'type' => MENU_CALLBACK,
  16. );
  17. $items['search/apachesolr_ajax/%menu_tail'] = array(
  18. 'page callback' => 'apachesolr_ajax_proxy',
  19. 'access arguments' => array('search content'),
  20. 'type' => MENU_CALLBACK,
  21. );
  22. return $items;
  23. }
  24. /**
  25. * Performs the search request, and returns the page content and the block
  26. * content of search-related blocks as JSON.
  27. *
  28. * <p>If the incoming request is not an AJAX request, behaves as though the
  29. * request were to "search/apachesolr_search".</p>
  30. *
  31. * @see apachesolr_ajax_modules()
  32. */
  33. function apachesolr_ajax_proxy() {
  34. $return = menu_execute_active_handler('search/apachesolr_search/'. search_get_keys());
  35. if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
  36. return $return;
  37. }
  38. if (is_int($return)) {
  39. switch ($return) {
  40. case MENU_NOT_FOUND:
  41. drupal_set_header('HTTP/1.1 404 Not Found');
  42. break;
  43. case MENU_ACCESS_DENIED:
  44. drupal_set_header('HTTP/1.1 403 Forbidden');
  45. break;
  46. case MENU_SITE_OFFLINE:
  47. drupal_set_header('HTTP/1.1 503 Service unavailable');
  48. break;
  49. }
  50. }
  51. elseif (isset($return)) {
  52. global $theme;
  53. if (!isset($theme)) {
  54. init_theme();
  55. }
  56. $json = array('regions' => array('apachesolr_ajax' => $return));
  57. $regions = system_region_list($theme);
  58. $modules = apachesolr_ajax_modules();
  59. foreach (array_keys($regions) as $region) {
  60. if ($list = block_list($region)) {
  61. foreach ($list as $block) {
  62. if (in_array($block->module, $modules)) {
  63. $json['regions'][$region][$block->module .'_'. $block->delta] = theme('block', $block);
  64. }
  65. }
  66. }
  67. }
  68. $javascript = drupal_add_js(NULL, NULL, NULL);
  69. $all_scopes = call_user_func_array('array_merge_recursive', $javascript); //merge all scopes
  70. $json['settings'] = call_user_func_array('array_merge_recursive', $all_scopes['setting']); //merge all settings
  71. $json['debug'] = apachesolr_ajax_debug();
  72. header('Content-Type: application/json');
  73. print json_encode($json);
  74. }
  75. }
  76. /**
  77. * Returns the list of modules whose blocks are search-related.
  78. *
  79. * <p>By default, the list includes the apachesolr, apachesolr_search, and
  80. * apachesolr_og modules. To modify this list, define an implementation of
  81. * hook_apachesolr_ajax_modules_alter(), e.g.:</p>
  82. *
  83. * <pre>
  84. * function mymodule_apachesolr_ajax_modules_alter(&$modules) {
  85. * $modules[] = 'mymodule';
  86. * }
  87. * </pre>
  88. *
  89. * @see apachesolr_ajax_proxy()
  90. */
  91. function apachesolr_ajax_modules() {
  92. static $modules = NULL;
  93. if (is_null($modules)) {
  94. $modules = array('apachesolr', 'apachesolr_search', 'apachesolr_og');
  95. drupal_alter('apachesolr_ajax_modules', $modules);
  96. }
  97. return $modules;
  98. }
  99. /**
  100. * Implementation of hook_preprocess_page().
  101. *
  102. * <p>Adds JavaScript files and settings. To successfully configure this module,
  103. * another module must implement hook_apachesolr_ajax_settings(), which must
  104. * return an array with the required keys "content", "blocks", "regions" and
  105. * the optional key "spinner".</p>
  106. *
  107. * <p>"content" must be the CSS selector for the HTML node in which the page
  108. * content is displayed. In the Zen theme, this is '#content-area'.</p>
  109. *
  110. * <p>"blocks" must be a map between the block keys and the CSS selectors for
  111. * the blocks. Block keys follow the pattern: MODULE_DELTA. In the Zen theme,
  112. * this may be: array('node_0' => '#block-node-0', ...). You need only do this
  113. * for search-related blocks. If you don't know your blocks, run:</p>
  114. *
  115. * <pre>
  116. * $modules = apachesolr_ajax_modules();
  117. * foreach ($modules as $module) {
  118. * if (module_exists($module)) {
  119. * if ($list = module_invoke($module, 'block', 'list')) {
  120. * foreach (array_keys($list) as $delta) {
  121. * print $module .'_'. $delta;
  122. * }
  123. * }
  124. * }
  125. * }
  126. * </pre>
  127. *
  128. * <p>"regions" must be a map between the theme regions and the CSS selectors
  129. * for the regions. In the Zen theme, this may be: array('content_top' =>
  130. * '.region-content-top'). If you don't know your theme's regions, run:
  131. * <code>system_region_list('mytheme');</code></p>
  132. *
  133. * <p>(Optional) "spinner" is the path to an animated image to be displayed
  134. * while content is loading via AJAX, e.g.: <code>base_path() .
  135. * drupal_get_path('module', 'mymodule') .'/images/spinner.gif'</code></p>
  136. *
  137. * <p>Note that, for Internet Explorer, you must add markup right after the
  138. * opening <tt>body</tt> tag. See theme_apachesolr_ajax_yui().</p>
  139. *
  140. * @see http://developer.yahoo.com/yui/history/
  141. */
  142. function apachesolr_ajax_preprocess_page(&$vars, $hook) {
  143. if (apachesolr_ajax_enabled()) {
  144. $vars['apachesolr_ajax'] = theme('apachesolr_ajax_yui');
  145. drupal_add_js(array('apachesolr_ajax' => module_invoke_all('apachesolr_ajax_settings')), 'setting');
  146. theme('apachesolr_ajax_js');
  147. $vars['scripts'] = drupal_get_js();
  148. $vars['closure'] = theme('closure');
  149. $vars['head'] = drupal_get_html_head();
  150. }
  151. }
  152. /**
  153. * Using hook_apachesolr_ajax_enable() and hook_apachesolr_hook_disable(), it is
  154. * possible to enable or disable this module on a per-request basis.
  155. *
  156. * @return bool TRUE if this module is enabled for this request.
  157. */
  158. function apachesolr_ajax_enabled() {
  159. return array_filter(module_invoke_all('apachesolr_ajax_enable')) && !array_filter(module_invoke_all('apachesolr_ajax_disable'));
  160. }
  161. /**
  162. * Implementation of hook_apachesolr_ajax_enable().
  163. */
  164. function apachesolr_ajax_apachesolr_ajax_enable() {
  165. return arg(0) == 'search' && strpos(arg(1), 'apachesolr_') !== FALSE;
  166. }
  167. /**
  168. * Collects debug data to send to the browser with the AJAX response.
  169. *
  170. * @param mixed $data Debug data you want to send to the browser.
  171. */
  172. function apachesolr_ajax_debug($data = NULL) {
  173. static $debug = array();
  174. if (isset($data)) {
  175. $debug[] = $data;
  176. }
  177. return $debug;
  178. }
  179. /**
  180. * Implementation of hook_theme().
  181. */
  182. function apachesolr_ajax_theme($existing, $type, $theme, $path) {
  183. return array(
  184. 'apachesolr_ajax_js' => array(
  185. 'arguments' => array(),
  186. ),
  187. 'apachesolr_ajax_yui' => array(
  188. 'arguments' => array('src' => '/robots.txt')
  189. ),
  190. );
  191. }
  192. /**
  193. * Override this function in your theme if you want to swap this module's
  194. * JavaScript and CSS files for your own.
  195. */
  196. function theme_apachesolr_ajax_js() {
  197. $head = drupal_get_html_head();
  198. foreach (array(
  199. 'http://yui.yahooapis.com/2.8.0r4/build/yahoo/yahoo-min.js',
  200. 'http://yui.yahooapis.com/2.8.0r4/build/event/event-min.js',
  201. 'http://yui.yahooapis.com/2.8.0r4/build/history/history-min.js') as $script) {
  202. if (strpos('<script src="'. $script .'"></script>', $head) === FALSE) {
  203. drupal_set_html_head('<script src="'. $script .'"></script>');
  204. }
  205. }
  206. drupal_add_js(drupal_get_path('module', 'apachesolr_ajax') .'/apachesolr_ajax.js', 'module', 'footer');
  207. drupal_add_css(drupal_get_path('module', 'apachesolr_ajax') .'/apachesolr_ajax.css');
  208. }
  209. /**
  210. * You need only include this markup for Internet Explorer.
  211. * @see http://developer.yahoo.com/yui/history/
  212. */
  213. function theme_apachesolr_ajax_yui($src = '/robots.txt') {
  214. return '<iframe id="yui-history-iframe" src="'. $src .'"></iframe><input id="yui-history-field" type="hidden">';
  215. }