You are here

public function UnitIndex::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/UnitIndex.php, line 110
Contains \Drupal\bat_api\Plugin\ServiceDefinition\UnitIndex.php

Class

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

Namespace

Drupal\bat_api\Plugin\ServiceDefinition

Code

public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
  $event_type = $request->query
    ->get('event_type');
  $unit_types = $request->query
    ->get('types');
  $unit_ids = $request->query
    ->get('ids');
  $create_event_access = FALSE;
  if (bat_event_access(bat_event_create([
    'type' => $event_type,
  ]), 'create', $this->currentUser) == AccessResult::allowed()) {
    $create_event_access = TRUE;
  }
  $ids = array_filter(explode(',', $unit_ids));
  if ($unit_types == 'all') {
    $types = [];
    foreach (bat_unit_get_types() as $type) {
      $type_bundle = bat_type_bundle_load($type
        ->bundle());
      if (isset($type_bundle->default_event_value_field_ids[$event_type]) && !empty($type_bundle->default_event_value_field_ids[$event_type])) {
        $types[] = $type
          ->id();
      }
    }
  }
  else {
    $types = array_filter(explode(',', $unit_types));
  }
  $bat_event_type = bat_event_type_load($event_type);
  $target_entity_type = $bat_event_type
    ->getTargetEntityType();
  $units = [];
  foreach ($types as $type) {
    $entities = $this
      ->getReferencedIds($type, $ids);
    $childrens = [];
    foreach ($entities as $entity) {
      $childrens[$entity['type_id']][] = [
        'id' => 'S' . $entity['id'],
        'title' => $entity['name'],
        'create_event' => $create_event_access,
      ];
    }
    foreach ($childrens as $type_id => $children) {
      $unit_type = bat_type_load($type_id);
      $units[] = [
        'id' => $unit_type
          ->id(),
        'title' => $unit_type
          ->label(),
        'children' => $children,
      ];
    }
  }
  $this->moduleHandler
    ->alter('bat_api_units_index_calendar', $units);
  return $units;
}