123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- /**
- * @file AJAXifies the Search API search pages, links, ranges, sorts and forms.
- */
- /**
- * Implements hook_search_api_ajax_settings().
- */
- function search_api_ajax_search_api_ajax_settings() {
- global $theme_key;
- $settings = array();
- // get all drupal blocks
- $settings['blocks'] = &drupal_static(__FUNCTION__);
- if (!isset($settings['blocks'])) {
- $blocks = array();
- // Drupal blocks
- // @todo cache this?
- $regions = system_region_list($theme_key);
- foreach ($regions as $region_key => $region_name) {
- $settings['regions'][] = $region_key;
- $block_list = block_list($region_key);
- // remove inactive blocks (status=0)
- foreach ($block_list as $key => $block) {
- if ($block->status == 0) {
- unset($block_list[$key]);
- }
- }
- $blocks = array_merge(block_list($region_key), $blocks);
- if (module_exists('context')) {
- $blocks = array_merge(context_get_plugin('reaction', 'block')->block_list($region_key), $blocks);
- }
- }
- $modules = search_api_ajax_modules();
- foreach ($blocks as $block) {
- if (in_array($block->module, $modules)) {
- // facetapi block names are strtolowered in html, so do same here
- if (in_array($block->module, array(
- 'facetapi',
- 'facetapi_tagcloud'
- ))) {
- $block->delta = strtolower($block->delta);
- }
- $settings['blocks'][$block->module . '_' . $block->delta] = str_replace('_', '-', '#block-' . $block->module . '-' . $block->delta);
- }
- }
- }
- // need to make the current search api page path available as jQuery setting
- // @see search_api_ajax.js
- $current_search = search_api_current_search();
- foreach ($current_search as $key => $search) {
- // break on facet blocks - $path is only useful for pages, avoid blocks
- if (strpos($key, 'facets_block') !== FALSE || arg(0) == 'admin') {
- break;
- }
- $path_parts = explode(':', $key);
- // is it a view path or a search_api_page?
- // views paths have 3 parts, search pages only 2
- if ($path_parts[0] == 'search_api_views') {
- $trail = menu_set_active_trail();
- // process last entry
- end($trail);
- $key = key($trail);
- $views_path = isset($trail[$key]['link_path']) ? $trail[$key]['link_path'] : $trail[$key]['path'];
- $path = search_api_ajax_get_views_path($views_path);
- $settings['view'] = 1;
- }
- else {
- $path = $path_parts[1];
- $settings['view'] = 0;
- }
- if (!empty($path)) {
- // needed to add current page path to ajax
- drupal_add_js(array('search_api_ajax_path' => $path), 'setting');
- }
- $settings['path'] = $path;
- }
- return $settings;
- }
- /**
- * Implementation of hook_menu().
- */
- function search_api_ajax_menu() {
- $items = array();
- // during uninstallation, this would lead to a fatal error otherwise.
- if (module_exists('search_api_page')) {
- foreach (search_api_page_load_multiple(FALSE, array('enabled' => TRUE)) as $page) {
- $items['search_api_ajax/' . $page->path] = array(
- 'title' => $page->name,
- 'description' => $page->description ? $page->description : '',
- 'page callback' => 'search_api_ajax_proxy',
- 'page arguments' => array((string)$page->id),
- 'access arguments' => array('access search_api_page'),
- 'type' => MENU_CALLBACK,
- 'file' => 'search_api_ajax.pages.inc',
- );
- }
- }
- // similarly, walk through search_api_views with a path (pages)
- if (module_exists('search_api_views') && function_exists('views_get_enabled_views')) {
- foreach (views_get_enabled_views() as $view) {
- if (strpos($view->base_table, 'search_api_index') !== FALSE) {
- foreach ($view->display as $display_name => $display) {
- if (isset($display->display_options['path']) && !(empty($display->display_options['path']))) {
- $items['search_api_ajax/' . $display->display_options['path']] = array(
- 'title' => isset($display->display_title) ? $display->display_title : '',
- 'description' => (string)$view->human_name,
- 'page callback' => 'search_api_ajax_proxy_views',
- 'page arguments' => array(
- (string)$view->name,
- (string)$display_name
- ),
- 'access arguments' => array('access content'),
- 'type' => MENU_CALLBACK,
- 'file' => 'search_api_ajax.pages.inc',
- );
- }
- }
- }
- }
- }
- return $items;
- }
- /**
- * Returns the list of modules whose blocks are search api-related.
- *
- * @see search_api_ajax_proxy()
- */
- function search_api_ajax_modules() {
- static $modules = NULL;
- if (is_null($modules)) {
- $modules = array(
- 'search_api_facets',
- 'search_api_ranges',
- 'search_api_sorts',
- 'search_api_widgets',
- 'facetapi',
- 'facetapi_tagcloud',
- );
- drupal_alter('search_api_ajax_modules', $modules);
- }
- return $modules;
- }
- /**
- * Implementation of hook_preprocess_page().
- *
- * Adds JavaScript files and settings. To successfully configure this module,
- * another module must implement hook_search_api_ajax_settings(), which must
- * return an array with the required keys "content", "blocks", "regions" and
- * the optional key "spinner".
- *
- * "content" must be the CSS selector for the HTML node in which the page
- * content is displayed. In the Zen theme, this is '#content-area'.
- *
- * "blocks" must be a map between the block keys and the CSS selectors for
- * the blocks. Block keys follow the pattern: MODULE_DELTA. In the Zen theme,
- * this may be: array('node_0' => '#block-node-0', ...). You need only do this
- * for search-related blocks. If you don't know your blocks, run:
- *
- * <pre>
- * $modules = search_api_ajax_modules();
- * foreach ($modules as $module) {
- * if (module_exists($module)) {
- * if ($list = module_invoke($module, 'block', 'list')) {
- * foreach (array_keys($list) as $delta) {
- * print $module .'_'. $delta;
- * }
- * }
- * }
- * }
- * </pre>
- *
- * "regions" must be a map between the theme regions and the CSS selectors
- * for the regions. In the Zen theme, this may be: array('content_top' =>
- * '.region-content-top'). If you don't know your theme's regions, run:
- * <code>system_region_list('mytheme');</code>
- *
- * (Optional) "spinner" is the path to an animated image to be displayed
- * while content is loading via AJAX, e.g.: <code>base_path() .
- * drupal_get_path('module', 'mymodule') .'/images/spinner.gif'</code>
- *
- * @see http://developer.yahoo.com/yui/history/
- */
- function search_api_ajax_preprocess_page(&$vars, $hook) {
- if (!search_api_current_search()) {
- return;
- }
- drupal_add_css(drupal_get_path('module', 'search_api_ajax') . '/search_api_ajax.css');
- drupal_add_js(array('search_api_ajax' => module_invoke_all('search_api_ajax_settings')), 'setting');
- drupal_add_js(drupal_get_path('module', 'search_api_ajax') . '/jquery.livequery.js', array('scope' => 'footer'));
- if (module_exists('libraries') && $library = libraries_get_path('yui')) {
- drupal_add_js($library . '/build/yui/yui-min.js', array('scope' => 'footer'));
- }
- else {
- drupal_add_js('http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js', 'external');
- }
- drupal_add_js(drupal_get_path('module', 'search_api_ajax') . '/search_api_ajax.js', array('scope' => 'footer'));
- }
- /**
- * Collects debug data to send to the browser with the AJAX response.
- *
- * @param mixed $data Debug data you want to send to the browser.
- */
- function search_api_ajax_debug($data = NULL) {
- static $debug = array();
- if (isset($data)) {
- $debug[] = $data;
- }
- return $debug;
- }
- /**
- * Get the actual views path
- * Filling in '%' arguments in the path with the correct values from arg()
- *
- * @param string $views_path
- * The views page path, e.g. 'mynodes', 'mynodes/%'
- *
- * @param int $skip
- * The number of arguments to skip (usually only needed on
- * search_api_ajax/* pages).
- */
- function search_api_ajax_get_views_path($views_path, $skip = 0) {
- $path = array();
- $current_path = arg();
- // fill in % paths: we know the views_path, which may contain % arguments
- // so we need to fill in those % with the current page from arg()
- // @todo is there a 'Drupal way' to do this?
- $path = explode('/', $views_path);
- foreach (arg(NULL, $views_path) as $key => $piece) {
- if ($piece == '%') {
- $path[$key] = $current_path[$key + $skip];
- }
- }
- $path = implode('/', $path);
- return $path;
- }
|