You are here

public function EventsIndex::processRequest in Booking and Availability Management API 8

Processes the request and returns an array of data as appropriate.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match object.

\Symfony\Component\Serializer\SerializerInterface $serializer: The serializer. Some methods might require the plugin to leverage the serializer after extracting the request contents.

Return value

array The response.

Throws

\Symfony\Component\HttpKernel\Exception\HttpException

Overrides ServiceDefinitionInterface::processRequest

File

src/Plugin/ServiceDefinition/EventsIndex.php, line 106
Contains \Drupal\bat_api\Plugin\ServiceDefinition\EventsIndex.php

Class

EventsIndex
Plugin annotation @ServiceDefinition( id = "events_index", methods = { "GET" }, translatable = true, deriver = "\Drupal\bat_api\Plugin\Deriver\EventsIndex" )

Namespace

Drupal\bat_api\Plugin\ServiceDefinition

Code

public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
  $target_ids = $request->query
    ->get('target_ids');
  $target_types = $request->query
    ->get('target_types');
  $target_entity_type = $request->query
    ->get('target_entity_type');
  $start_date = $request->query
    ->get('start_date');
  $end_date = $request->query
    ->get('end_date');
  $event_types = $request->query
    ->get('event_types');
  $target_types = array_filter(explode(',', $target_types));
  $types = array_filter(explode(',', $event_types));
  $events_json = [];
  foreach ($types as $type) {
    $database = Database::getConnectionInfo('default');
    $prefix = isset($database['default']['prefix']['default']) ? $database['default']['prefix']['default'] : '';
    $event_store = new DrupalDBStore($type, DrupalDBStore::BAT_EVENT, $prefix);
    $start_date_object = new \DateTime($start_date);
    $end_date_object = new \DateTime($end_date);
    $today = new \DateTime();
    if (!$this->currentUser
      ->hasPermission('view past event information') && $today > $start_date_object) {
      if ($today > $end_date_object) {
        return [];
      }
      $start_date_object = $today;
    }
    $ids = explode(',', $target_ids);
    $units = [];
    foreach ($ids as $id) {
      if ($target_entity = $this->entityTypeManager
        ->getStorage($target_entity_type)
        ->load($id)) {
        if (in_array($target_entity->type, $target_types) || empty($target_types)) {

          // Setting the default value to 0 since we are dealing with the events array
          // so getting event IDs.
          $units[] = new Unit($id, 0);
        }
      }
    }
    if (!empty($units)) {
      $event_calendar = new Calendar($units, $event_store);
      $event_ids = $event_calendar
        ->getEvents($start_date_object, $end_date_object);
      $bat_event_type = bat_event_type_load($type);
      if ($bat_event_type
        ->getFixedEventStates()) {
        $event_formatter = $this->fixedStateEventFormatter;
      }
      else {
        $event_formatter = $this->openStateEventFormatter;
      }
      $event_formatter
        ->setEventType($bat_event_type);
      foreach ($event_ids as $unit_id => $unit_events) {
        foreach ($unit_events as $key => $event) {
          $events_json[] = [
            'id' => (string) $key . $unit_id,
            'bat_id' => $event
              ->getValue(),
            'resourceId' => 'S' . $unit_id,
          ] + $event
            ->toJson($event_formatter);
        }
      }
    }
  }
  return $events_json;
}