View source
<?php
use Roomify\Bat\Calendar\Calendar;
use Roomify\Bat\Event\Event;
use Roomify\Bat\Store\DrupalDBStore;
use Roomify\Bat\Unit\Unit;
function bat_facets_ctools_plugin_api($module, $api) {
if ($module == 'facetapi' && $api == 'facetapi_defaults') {
return array(
'version' => '1',
);
}
}
function bat_facets_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) {
switch ($searcher_info['adapter']) {
case 'search_api':
foreach ($facet_info as $name => $info) {
if ($name == 'type_id') {
$facet_info[$name]['query types'][] = 'bat_state';
}
}
break;
}
}
function bat_facets_availability($form, &$form_state, $elements) {
$params = drupal_get_query_parameters();
$now = date_create();
$default_year = $now
->format('Y');
$default_month = '';
$form['container'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'container-inline',
),
),
);
if (isset($params['bat_start_date']) && !empty($params['bat_start_date'])) {
$start_date = new DateTime($params['bat_start_date']);
$arrival = $start_date
->format('Y-m-d H:i');
}
if (isset($params['bat_end_date']) && !empty($params['bat_end_date'])) {
$end_date = new DateTime($params['bat_end_date']);
$departure = $end_date
->format('Y-m-d H:i');
}
$start_date_id = drupal_html_id('datepicker-start-date');
$start_date_selector = '#' . $start_date_id . ' .form-text';
$end_date_id = drupal_html_id('datepicker-end-date');
$end_date_selector = '#' . $start_date_id . ' .form-text';
$datepicker_options = array(
'minDate' => '+' . variable_get('bat_event_start_date', 0) . 'd',
);
$form['container']['arrival'] = array(
'#type' => 'date_popup',
'#description' => '',
'#date_format' => variable_get('bat_date_format', 'Y-m-d H:i'),
'#default_value' => isset($arrival) ? $arrival : '',
'#datepicker_options' => array_merge($datepicker_options, array(
'endDateSelector' => $end_date_selector,
)),
'#required' => TRUE,
);
$form['container']['departure'] = array(
'#type' => 'date_popup',
'#description' => '',
'#date_format' => variable_get('bat_date_format', 'Y-m-d H:i'),
'#default_value' => isset($departure) ? $departure : '',
'#datepicker_options' => array_merge($datepicker_options, array(
'startDateSelector' => $start_date_selector,
)),
'#required' => TRUE,
'#attached' => array(
'js' => array(
drupal_get_path('module', 'bat') . '/js/bat_date_popup.js',
array(
'data' => array(
'bat' => array(
'batBookingStartDay' => variable_get('bat_event_start_date', 0),
'batDateFormat' => 'dd/mm/yy',
'datepickers' => array(
$start_date_selector => array(
'endDateSelector' => $end_date_selector,
),
),
),
),
'type' => 'setting',
),
),
),
);
$form['container']['submit'] = array(
'#type' => 'submit',
'#value' => 'Search',
);
return $form;
}
function bat_facets_availability_submit($form, &$form_state) {
$form_state['redirect'] = array(
$_GET['q'],
array(
'query' => array(
'bat_start_date' => $form_state['values']['arrival'],
'bat_end_date' => $form_state['values']['departure'],
),
),
);
}
function bat_facets_search_api_query_alter($query) {
if ($query
->getIndex()
->getEntityType()) {
$info = entity_get_info($query
->getIndex()
->getEntityType());
if ($info['base table'] == 'bat_types') {
$index = $query
->getIndex();
$facets = facetapi_get_enabled_facets('search_api@' . $index->machine_name);
$adapter = facetapi_adapter_load('search_api@' . $index->machine_name);
$realm = facetapi_realm_load('block');
if (isset($facets['type_id'])) {
$settings = $adapter
->getFacetSettings($facets['type_id'], $realm);
$params = drupal_get_query_parameters();
if (isset($params['bat_start_date']) && !empty($params['bat_start_date']) && isset($params['bat_end_date']) && !empty($params['bat_end_date'])) {
$start_date = new DateTime($params['bat_start_date']);
$end_date = new DateTime($params['bat_end_date']);
$end_date
->sub(new DateInterval('PT1M'));
$event_type = $settings->settings['event_type'];
$state_store = new DrupalDBStore($event_type, DrupalDBStore::BAT_STATE, bat_get_db_prefix());
$valid_states = $settings->settings['states'];
$valid_type_ids = array();
$type_ids = bat_type_ids();
$units = array();
foreach ($type_ids as $type_id => $name) {
$drupal_units = bat_unit_load_multiple(FALSE, array(
'type_id' => $type_id,
));
$bat_units = array();
foreach ($drupal_units as $unit_id => $unit) {
$bat_units[] = new Unit($unit_id, $unit
->getEventDefaultValue($event_type));
}
if (count($bat_units)) {
$calendar = new Calendar($bat_units, $state_store);
$constraints = array();
foreach (bat_event_constraints_get_info() as $constraint) {
$constraints[] = $constraint['constraint'];
}
$response = $calendar
->getMatchingUnits($start_date, $end_date, $valid_states, $constraints, TRUE);
$valid_unit_ids = array_keys($response
->getIncluded());
if (count($valid_unit_ids)) {
$valid_type_ids[] = $type_id;
}
}
}
$context = array(
'types_before_search' => $type_ids,
'start_date' => $start_date,
'end_date' => $end_date,
'event_type' => $event_type,
'valid_states' => $valid_states,
);
drupal_alter('bat_facets_search_results', $valid_type_ids, $context);
if (empty($valid_type_ids)) {
$query
->condition($info['entity keys']['id'], 1, '<');
}
else {
$query
->condition($info['entity keys']['id'], $valid_type_ids, 'IN');
}
}
}
}
}
}