View source
<?php
require_once __DIR__ . '/includes/ajax_facets.block.inc';
function ajax_facets_menu() {
$items = [];
$items['ajax/ajax_facets/refresh'] = [
'title' => 'Callback to update facets content',
'page callback' => 'ajax_facets_refresh_facets_content',
'access arguments' => [
'access content',
],
'delivery callback' => 'ajax_deliver',
'type' => MENU_CALLBACK,
'file' => 'ajax_facets.pages.inc',
];
return $items;
}
function ajax_facets_theme() {
return [
'ajax_facets_ranges_input' => [
'variables' => [
'title' => NULL,
'attributes' => [],
'value' => NULL,
],
'file' => 'ajax_facets.theme.inc',
],
'ajax_facets_select' => [
'render element' => 'element',
],
];
}
function ajax_facets_facetapi_widgets() {
$query_type = [
'term',
'date',
'date_range',
];
$widgets = [
'facetapi_ajax_checkboxes' => [
'handler' => [
'label' => 'Ajax multiple checkboxes',
'class' => 'FacetapiAjaxWidgetCheckboxes',
'query types' => $query_type,
],
],
'facetapi_ajax_select' => [
'handler' => [
'label' => 'Ajax selectbox',
'class' => 'FacetapiAjaxWidgetSelect',
'query types' => $query_type,
],
],
'facetapi_ajax_links' => [
'handler' => [
'label' => 'Ajax links',
'class' => 'FacetapiAjaxWidgetLinks',
'query types' => $query_type,
],
],
'facetapi_ajax_ranges' => [
'handler' => [
'label' => 'Ajax ranges',
'class' => 'FacetapiAjaxWidgetRanges',
'query types' => [
'term',
],
],
],
];
return $widgets;
}
function ajax_facets_facetapi_empty_behaviors() {
return [
'ajax_facets' => [
'handler' => [
'label' => t('Display ajax_facets wrapper'),
'class' => 'FacetapiEmptyBehaviorAjaxFacets',
],
],
];
}
function ajax_facets_add_ajax_js($facet) {
static $included = FALSE;
if (!$included) {
$included = TRUE;
$history_js_exists = FALSE;
$module_path = drupal_get_path('module', 'ajax_facets');
drupal_add_js($module_path . '/misc/ajax_facets.js');
drupal_add_js([
'ajax_facets' => [
'auto_update_facets' => variable_get('ajax_facets_auto_update_facets', 1),
],
], [
'type' => 'setting',
]);
drupal_add_css($module_path . '/misc/ajax_facets.css');
$search_path = $facet
->getAdapter()
->getSearchPath();
$filter_key = $facet
->getAdapter()
->getUrlProcessor()
->getFilterKey();
$query = isset($_GET[$filter_key]) ? [
$filter_key => $_GET[$filter_key],
] : [];
$search_results = search_api_current_search();
$processable_views = [];
if (!empty($search_results)) {
foreach ($search_results as $key => $result) {
if (substr_count($key, 'search_api_views')) {
$facetapi_parts = explode(':', $key);
$views_parts = explode(':', $result[0]
->getOption('search id'));
$processable_views[] = [
'view_name' => $views_parts[1],
'view_display_id' => $views_parts[2],
'facetapi_view_display_id' => $facetapi_parts[2],
];
}
}
}
if (module_exists('libraries')) {
$history_js_path = libraries_get_path('history.js');
if ($history_js_path) {
$history_js_exists = TRUE;
drupal_add_js($history_js_path . '/scripts/bundled/html4+html5/jquery.history.js', [
'group' => JS_LIBRARY,
]);
}
}
$facet = $facet
->getFacet();
$setting['facetapi'] = [
'defaultQuery' => isset($_GET[$filter_key]) ? $_GET[$filter_key] : '',
'searchUrl' => url($search_path),
'index_id' => $facet['map options']['index id'],
'views' => $processable_views,
'facet_field' => $facet['map options']['field']['key'],
'applyPath' => url($search_path, [
'query' => $query,
]),
'isHistoryJsExists' => $history_js_exists,
];
drupal_add_js($setting, 'setting');
drupal_add_library('system', 'drupal.ajax');
}
}
function ajax_facets_facet_build_reset_path($facet, $adapter) {
$params = $adapter
->getUrlProcessor()
->fetchParams();
$filter_key = $adapter
->getUrlProcessor()
->getFilterKey();
$clean_params = [];
$url_params = [];
if (!empty($params[$filter_key])) {
foreach ($params[$filter_key] as $param) {
if (strpos($param, $facet['name']) !== 0) {
$clean_params[] = $param;
}
}
$url_params = [];
if (!empty($clean_params)) {
$url_params = [
'query' => [
$filter_key => $clean_params,
],
];
}
$unset_keys = [
'searchPath',
'q',
'page',
$filter_key,
];
foreach ($params as $key => $value) {
if (!in_array($key, $unset_keys)) {
$url_params['query'][$key] = $value;
}
}
}
return url(!empty($_GET['searchPath']) ? $_GET['searchPath'] : $adapter
->getSearchPath(), $url_params);
}
function ajax_facets_views_ajax_data_alter(&$commands, $view) {
if (strpos($view->base_table, 'search_api_index') !== FALSE) {
$index_id = str_replace('search_api_index_', '', $view->base_table);
$searcher = 'search_api@' . $index_id;
$blocks = ajax_facets_process_facet_blocks($searcher);
foreach ($blocks['facet_blocks'] as $id => $content) {
$commands[] = ajax_command_replace("#{$id}-wrapper", $content);
}
$commands[] = ajax_command_invoke('div.block--ajax_facets:not(:visible)', 'show');
foreach ($blocks['hide_blocks'] as $block_id) {
$commands[] = ajax_command_invoke("#{$block_id}", 'hide');
}
$facet_query = !empty($_GET['f']) ? $_GET['f'] : '';
if ($facet_query) {
$settings = [
'views' => [
'ajax_path' => url('views/ajax', [
'query' => [
'f' => $facet_query,
],
]),
],
];
array_unshift($commands, ajax_command_settings($settings, TRUE));
}
}
}
function ajax_facets_process_facet_blocks($searcher, $realm_name = 'block') {
$map = facetapi_get_delta_map();
$facets_to_proceed = [];
$enabled_facets = facetapi_get_enabled_facets($searcher, $realm_name);
foreach ($enabled_facets as $facet) {
$facets_to_proceed[] = $facet['name'];
}
$blocks = [
'facet_blocks' => [],
'hide_blocks' => [],
'reset_urls' => [],
'active_items' => [],
];
$group = $searcher . ':' . $realm_name;
$realm = facetapi_realm_load($realm_name);
$adapter = facetapi_adapter_load($searcher);
$builds[$group] = facetapi_build_realm($searcher, $realm_name);
$search_id = key(search_api_current_search());
foreach ($facets_to_proceed as $facet_name) {
$facet = $adapter
->getFacet([
'name' => $facet_name,
]);
$settings = $facet
->getSettings()->settings;
$default_true = isset($settings['default_true']) ? $settings['default_true'] : TRUE;
$facet_search_ids = isset($settings['facet_search_ids']) ? $settings['facet_search_ids'] : [];
if ($default_true == empty($facet_search_ids[$search_id])) {
$blocks['reset_urls'][$facet_name] = ajax_facets_facet_build_reset_path($facet, $adapter);
if (!empty($builds[$group][$facet_name]) && ($build = $facet
->getBuild())) {
$blocks['active_items'][$facet_name] = [];
foreach ($build as $key => $value) {
if (!empty($value['#active'])) {
$blocks['active_items'][$facet_name][] = "{$facet_name}:{$key}";
}
}
if (!empty($blocks['active_items'][$facet_name])) {
sort($blocks['active_items'][$facet_name]);
}
$blocks['facet_blocks'][$builds[$group][$facet_name]['#attributes']['id']] = drupal_render($builds[$group][$facet_name]);
}
else {
$facet_name = rawurlencode($facet_name);
$delta = array_search("{$searcher}:{$realm_name}:{$facet_name}", $map);
$blocks['hide_blocks'][] = 'block-facetapi-' . strtolower($delta);
}
}
}
return $blocks;
}
function ajax_facets_form_facetapi_facet_display_form_alter(&$form, $form_state) {
drupal_add_js(drupal_get_path('module', 'ajax_facets') . '/misc/ajax_facets.admin.js');
}
function ajax_facets_views_pre_render(&$view) {
static $setting;
$name_display = $base = "{$view->name}:{$view->current_display}";
$i = 0;
while (isset($setting['facetapi']['view_args'][$name_display])) {
$name_display = $base . '-' . ++$i;
}
$setting['facetapi']['view_args'][$name_display] = $view->args;
$setting['facetapi']['exposed_input'][$name_display] = $view->exposed_raw_input;
$setting['facetapi']['view_path'][$name_display] = $view
->get_path();
$setting['facetapi']['view_dom_id'][$name_display] = $view->dom_id;
drupal_add_js($setting, 'setting');
}
function ajax_facets_i18n_string_info() {
$groups['ajax_facets'] = [
'title' => t('Ajax facets'),
'description' => t('Translatable ajax facets: label.'),
'format' => FALSE,
'list' => TRUE,
];
return $groups;
}
function ajax_facets_facet_settings_form_submit($form, $form_state) {
$values = $form_state['values'];
if (function_exists('i18n_string_update')) {
if (!empty($values['widget'])) {
if ($values['widget'] == 'facetapi_ajax_select') {
$name = [
'ajax_facets',
'facet_label',
str_replace(':', '_', $form['#facetapi']['facet']['name']),
'label',
];
i18n_string_update($name, $values['ajax_select_default_option_label']);
}
if ($values['show_reset_link'] && !empty($values['reset_link_text'])) {
$reset_link_text = [
'ajax_facets',
'reset_link_text',
str_replace(':', '_', $form['#facetapi']['facet']['name']),
'value',
];
i18n_string_update($reset_link_text, $values['reset_link_text']);
}
}
}
}
function theme_ajax_facets_select($variables) {
$element = $variables['element'];
element_set_attributes($element, [
'id',
'name',
'size',
]);
_form_set_class($element, [
'form-select',
]);
return '<select' . drupal_attributes($element['#attributes']) . '>' . ajax_facets_form_select_options($element) . '</select>';
}
function ajax_facets_form_select_options($element, $choices = NULL) {
if (!isset($choices)) {
$choices = $element['#options'];
}
$value_valid = isset($element['#value']) || array_key_exists('#value', $element);
$value_is_array = $value_valid && is_array($element['#value']);
$options = '';
foreach ($choices as $key => $choice) {
if (is_array($choice)) {
$options .= '<optgroup label="' . check_plain($key) . '">';
$options .= form_select_options($element, $choice);
$options .= '</optgroup>';
}
elseif (is_object($choice)) {
$options .= form_select_options($element, $choice->option);
}
else {
$key = (string) $key;
if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key || $value_is_array && in_array($key, $element['#value']))) {
$selected = ' selected="selected"';
}
else {
$selected = '';
}
$disabled = !empty($element['#disabled_items'][$key]) ? ' disabled' : '';
$options .= '<option value="' . check_plain($key) . '"' . $selected . $disabled . '>' . check_plain($choice) . '</option>';
}
}
return $options;
}