search_api_ajax.module 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * @file AJAXifies the Search API search pages, links, ranges, sorts and forms.
  4. */
  5. /**
  6. * Implements hook_search_api_ajax_settings().
  7. */
  8. function search_api_ajax_search_api_ajax_settings() {
  9. global $theme_key;
  10. $settings = array();
  11. // get all drupal blocks
  12. $settings['blocks'] = &drupal_static(__FUNCTION__);
  13. if (!isset($settings['blocks'])) {
  14. $blocks = array();
  15. // Drupal blocks
  16. // @todo cache this?
  17. $regions = system_region_list($theme_key);
  18. foreach ($regions as $region_key => $region_name) {
  19. $settings['regions'][] = $region_key;
  20. $block_list = block_list($region_key);
  21. // remove inactive blocks (status=0)
  22. foreach ($block_list as $key => $block) {
  23. if ($block->status == 0) {
  24. unset($block_list[$key]);
  25. }
  26. }
  27. $blocks = array_merge(block_list($region_key), $blocks);
  28. if (module_exists('context')) {
  29. $blocks = array_merge(context_get_plugin('reaction', 'block')->block_list($region_key), $blocks);
  30. }
  31. }
  32. $modules = search_api_ajax_modules();
  33. foreach ($blocks as $block) {
  34. if (in_array($block->module, $modules)) {
  35. // facetapi block names are strtolowered in html, so do same here
  36. if (in_array($block->module, array(
  37. 'facetapi',
  38. 'facetapi_tagcloud'
  39. ))) {
  40. $block->delta = strtolower($block->delta);
  41. }
  42. $settings['blocks'][$block->module . '_' . $block->delta] = str_replace('_', '-', '#block-' . $block->module . '-' . $block->delta);
  43. }
  44. }
  45. }
  46. // need to make the current search api page path available as jQuery setting
  47. // @see search_api_ajax.js
  48. $current_search = search_api_current_search();
  49. foreach ($current_search as $key => $search) {
  50. // break on facet blocks - $path is only useful for pages, avoid blocks
  51. if (strpos($key, 'facets_block') !== FALSE || arg(0) == 'admin') {
  52. break;
  53. }
  54. $path_parts = explode(':', $key);
  55. // is it a view path or a search_api_page?
  56. // views paths have 3 parts, search pages only 2
  57. if ($path_parts[0] == 'search_api_views') {
  58. $trail = menu_set_active_trail();
  59. // process last entry
  60. end($trail);
  61. $key = key($trail);
  62. $views_path = isset($trail[$key]['link_path']) ? $trail[$key]['link_path'] : $trail[$key]['path'];
  63. $path = search_api_ajax_get_views_path($views_path);
  64. $settings['view'] = 1;
  65. }
  66. else {
  67. $path = $path_parts[1];
  68. $settings['view'] = 0;
  69. }
  70. if (!empty($path)) {
  71. // needed to add current page path to ajax
  72. drupal_add_js(array('search_api_ajax_path' => $path), 'setting');
  73. }
  74. $settings['path'] = $path;
  75. }
  76. return $settings;
  77. }
  78. /**
  79. * Implementation of hook_menu().
  80. */
  81. function search_api_ajax_menu() {
  82. $items = array();
  83. // during uninstallation, this would lead to a fatal error otherwise.
  84. if (module_exists('search_api_page')) {
  85. foreach (search_api_page_load_multiple(FALSE, array('enabled' => TRUE)) as $page) {
  86. $items['search_api_ajax/' . $page->path] = array(
  87. 'title' => $page->name,
  88. 'description' => $page->description ? $page->description : '',
  89. 'page callback' => 'search_api_ajax_proxy',
  90. 'page arguments' => array((string)$page->id),
  91. 'access arguments' => array('access search_api_page'),
  92. 'type' => MENU_CALLBACK,
  93. 'file' => 'search_api_ajax.pages.inc',
  94. );
  95. }
  96. }
  97. // similarly, walk through search_api_views with a path (pages)
  98. if (module_exists('search_api_views') && function_exists('views_get_enabled_views')) {
  99. foreach (views_get_enabled_views() as $view) {
  100. if (strpos($view->base_table, 'search_api_index') !== FALSE) {
  101. foreach ($view->display as $display_name => $display) {
  102. if (isset($display->display_options['path']) && !(empty($display->display_options['path']))) {
  103. $items['search_api_ajax/' . $display->display_options['path']] = array(
  104. 'title' => isset($display->display_title) ? $display->display_title : '',
  105. 'description' => (string)$view->human_name,
  106. 'page callback' => 'search_api_ajax_proxy_views',
  107. 'page arguments' => array(
  108. (string)$view->name,
  109. (string)$display_name
  110. ),
  111. 'access arguments' => array('access content'),
  112. 'type' => MENU_CALLBACK,
  113. 'file' => 'search_api_ajax.pages.inc',
  114. );
  115. }
  116. }
  117. }
  118. }
  119. }
  120. return $items;
  121. }
  122. /**
  123. * Returns the list of modules whose blocks are search api-related.
  124. *
  125. * @see search_api_ajax_proxy()
  126. */
  127. function search_api_ajax_modules() {
  128. static $modules = NULL;
  129. if (is_null($modules)) {
  130. $modules = array(
  131. 'search_api_facets',
  132. 'search_api_ranges',
  133. 'search_api_sorts',
  134. 'search_api_widgets',
  135. 'facetapi',
  136. 'facetapi_tagcloud',
  137. );
  138. drupal_alter('search_api_ajax_modules', $modules);
  139. }
  140. return $modules;
  141. }
  142. /**
  143. * Implementation of hook_preprocess_page().
  144. *
  145. * Adds JavaScript files and settings. To successfully configure this module,
  146. * another module must implement hook_search_api_ajax_settings(), which must
  147. * return an array with the required keys "content", "blocks", "regions" and
  148. * the optional key "spinner".
  149. *
  150. * "content" must be the CSS selector for the HTML node in which the page
  151. * content is displayed. In the Zen theme, this is '#content-area'.
  152. *
  153. * "blocks" must be a map between the block keys and the CSS selectors for
  154. * the blocks. Block keys follow the pattern: MODULE_DELTA. In the Zen theme,
  155. * this may be: array('node_0' => '#block-node-0', ...). You need only do this
  156. * for search-related blocks. If you don't know your blocks, run:
  157. *
  158. * <pre>
  159. * $modules = search_api_ajax_modules();
  160. * foreach ($modules as $module) {
  161. * if (module_exists($module)) {
  162. * if ($list = module_invoke($module, 'block', 'list')) {
  163. * foreach (array_keys($list) as $delta) {
  164. * print $module .'_'. $delta;
  165. * }
  166. * }
  167. * }
  168. * }
  169. * </pre>
  170. *
  171. * "regions" must be a map between the theme regions and the CSS selectors
  172. * for the regions. In the Zen theme, this may be: array('content_top' =>
  173. * '.region-content-top'). If you don't know your theme's regions, run:
  174. * <code>system_region_list('mytheme');</code>
  175. *
  176. * (Optional) "spinner" is the path to an animated image to be displayed
  177. * while content is loading via AJAX, e.g.: <code>base_path() .
  178. * drupal_get_path('module', 'mymodule') .'/images/spinner.gif'</code>
  179. *
  180. * @see http://developer.yahoo.com/yui/history/
  181. */
  182. function search_api_ajax_preprocess_page(&$vars, $hook) {
  183. if (!search_api_current_search()) {
  184. return;
  185. }
  186. drupal_add_css(drupal_get_path('module', 'search_api_ajax') . '/search_api_ajax.css');
  187. drupal_add_js(array('search_api_ajax' => module_invoke_all('search_api_ajax_settings')), 'setting');
  188. drupal_add_js(drupal_get_path('module', 'search_api_ajax') . '/jquery.livequery.js', array('scope' => 'footer'));
  189. if (module_exists('libraries') && $library = libraries_get_path('yui')) {
  190. drupal_add_js($library . '/build/yui/yui-min.js', array('scope' => 'footer'));
  191. }
  192. else {
  193. drupal_add_js('http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js', 'external');
  194. }
  195. drupal_add_js(drupal_get_path('module', 'search_api_ajax') . '/search_api_ajax.js', array('scope' => 'footer'));
  196. }
  197. /**
  198. * Collects debug data to send to the browser with the AJAX response.
  199. *
  200. * @param mixed $data Debug data you want to send to the browser.
  201. */
  202. function search_api_ajax_debug($data = NULL) {
  203. static $debug = array();
  204. if (isset($data)) {
  205. $debug[] = $data;
  206. }
  207. return $debug;
  208. }
  209. /**
  210. * Get the actual views path
  211. * Filling in '%' arguments in the path with the correct values from arg()
  212. *
  213. * @param string $views_path
  214. * The views page path, e.g. 'mynodes', 'mynodes/%'
  215. *
  216. * @param int $skip
  217. * The number of arguments to skip (usually only needed on
  218. * search_api_ajax/* pages).
  219. */
  220. function search_api_ajax_get_views_path($views_path, $skip = 0) {
  221. $path = array();
  222. $current_path = arg();
  223. // fill in % paths: we know the views_path, which may contain % arguments
  224. // so we need to fill in those % with the current page from arg()
  225. // @todo is there a 'Drupal way' to do this?
  226. $path = explode('/', $views_path);
  227. foreach (arg(NULL, $views_path) as $key => $piece) {
  228. if ($piece == '%') {
  229. $path[$key] = $current_path[$key + $skip];
  230. }
  231. }
  232. $path = implode('/', $path);
  233. return $path;
  234. }