You are here

function bat_event_get_calendar_response in Booking and Availability Management Tools for Drupal 8

Same name and namespace in other branches
  1. 7 modules/bat_event/bat_event.module \bat_event_get_calendar_response()

Given a date range and a set of valid states it will return all units within the set of valid states.

Parameters

DateTime $start_date:

DateTime $end_date:

$valid_name_states:

$type_id:

$event_type:

$intersect:

Return value

array

File

modules/bat_event/bat_event.module, line 972
Manage Events - Events store the EventValue of a Unit over a period of time.

Code

function bat_event_get_calendar_response(DateTime $start_date, DateTime $end_date, $valid_name_states, $type_id, $event_type, $intersect = FALSE) {
  $results = [
    'included' => [],
    'excluded' => [],
  ];
  $valid_states = [];
  foreach ($valid_name_states as $name) {
    $state = bat_event_load_state_by_machine_name($name);
    $valid_states[] = $state
      ->id();
  }
  $constraints = [];
  foreach (bat_event_constraints_get_info() as $constraint) {
    $constraints[] = $constraint['constraint'];
  }
  $calendar = bat_event_get_calendar($type_id, $event_type);
  $response = $calendar
    ->getMatchingUnits($start_date, $end_date, $valid_states, $constraints, $intersect);
  $valid_unit_ids = array_keys($response
    ->getIncluded());
  $excluded = [];
  foreach ($response
    ->getExcluded() as $unit_id => $ex) {
    if (isset($ex['constraint'])) {
      $p = $ex['constraint']
        ->toString();
      $excluded[$unit_id] = t($p['text'], $p['args']);
    }
    else {
      $excluded[$unit_id] = '';
    }
  }
  $results['excluded'] = $excluded;
  if (count($valid_unit_ids)) {
    $results['included'] = $valid_unit_ids;
  }
  return $results;
}