You are here

function bat_facets_search_api_query_alter in Booking and Availability Management Tools for Drupal 7

Same name and namespace in other branches
  1. 8 modules/bat_facets/bat_facets.module \bat_facets_search_api_query_alter()

Implements hook_search_api_query_alter().

Parameters

SearchApiQueryInterface $query: The search query being executed.

File

modules/bat_facets/bat_facets.module, line 150

Code

function bat_facets_search_api_query_alter($query) {
  if ($query
    ->getIndex()
    ->getEntityType()) {
    $info = entity_get_info($query
      ->getIndex()
      ->getEntityType());

    // Only modify the query if this query is against bat units.
    if ($info['base table'] == 'bat_types') {

      // Get this widget's settings.
      $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);

        // Get URL parameters.
        $params = drupal_get_query_parameters();

        // See if we have dates to search.
        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']);

          // Remove a minute because BAT considers as included.
          $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();

          // We need to narrow the query to all Types with Units that have the
          // requested state over the requested dates.
          // First, we retrieve all types.
          // TODO: narrow this to only the types already being searched for.
          $type_ids = bat_type_ids();
          $units = array();
          foreach ($type_ids as $type_id => $name) {

            // Get the units of this type.
            $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 this type has associated units, see if any of its units are
            // in the states being searched for over the search period.
            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 there are available units, mark this type as valid.
              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 no types are available, zero out results.
          if (empty($valid_type_ids)) {
            $query
              ->condition($info['entity keys']['id'], 1, '<');
          }
          else {

            // Limit the search API query to entity ids with availability.
            $query
              ->condition($info['entity keys']['id'], $valid_type_ids, 'IN');
          }
        }
      }
    }
  }
}