You are here

function bat_api_services_units_index_calendar in Booking and Availability Management API 7.2

Retrieve a list of units to use with Fullcalendar scheduler.

Parameters

string $unit_types:

string $unit_ids:

string $event_type:

bool $return_children:

string $grouping_ids:

string $grouping_entity_type:

bool $collapse_childrens:

string $offset:

string $limit:

1 string reference to 'bat_api_services_units_index_calendar'
bat_api_services_resources in ./bat_api.module
Implements hook_services_resources().

File

./bat_api.module, line 578
API access to booking data for BAT.

Code

function bat_api_services_units_index_calendar($unit_types, $unit_ids, $event_type, $return_children, $grouping_ids, $grouping_entity_type, $collapse_childrens, $offset, $limit) {
  $create_event_access = FALSE;
  if (bat_event_access('create', bat_event_create(array(
    'type' => $event_type,
  )))) {
    $create_event_access = TRUE;
  }
  $units_groups = array();
  if ($grouping_ids) {
    $groups = array_filter(explode(';', $grouping_ids));
    foreach ($groups as $group) {
      list($group_entity_id, $group_types) = explode(':', $group);
      foreach (explode(',', $group_types) as $type_id) {
        $units_groups[$type_id] = $group_entity_id;
      }
    }
  }
  $ids = array_filter(explode(',', $unit_ids));
  if ($unit_types == 'all') {
    $types = array();
    foreach (bat_unit_get_types() as $type) {
      $type_bundle = bat_type_bundle_load($type->type);
      if (is_array($type_bundle->default_event_value_field_ids)) {
        if (isset($type_bundle->default_event_value_field_ids[$event_type]) && !empty($type_bundle->default_event_value_field_ids[$event_type])) {
          $types[] = $type->type_id;
        }
      }
    }
  }
  else {
    $types = array_filter(explode(',', $unit_types));
    $types = array_unique($types);
  }
  $bat_event_type = bat_event_type_load($event_type);
  $target_entity_type = $bat_event_type->target_entity_type;
  $controller = entity_get_controller($target_entity_type);
  $units = array();
  foreach ($types as $type) {
    $entities = $controller
      ->getReferencedIds($type, $ids);
    $childrens = array();
    foreach ($entities as $entity) {
      $childrens[$entity['type_id']][] = array(
        '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);
      if (isset($units_groups[$type_id])) {
        $entity_id = $units_groups[$type_id];
        if (isset($units['P' . $entity_id])) {
          if ($return_children) {
            $units['P' . $entity_id]['children'][] = array(
              'id' => $unit_type->type_id,
              'title' => $unit_type->name,
              'children' => $children,
            );
          }
          else {
            $units['P' . $entity_id]['children'][] = array(
              'id' => $unit_type->type_id,
              'title' => $unit_type->name,
            );
          }
        }
        else {
          $grouping_entity = entity_load_single($grouping_entity_type, $entity_id);
          if ($return_children) {
            $units['P' . $entity_id] = array(
              'id' => 'P' . $entity_id,
              'title' => $grouping_entity->name,
              'children' => array(
                array(
                  'id' => $unit_type->type_id,
                  'title' => $unit_type->name,
                  'children' => $children,
                ),
              ),
            );
          }
          else {
            $units['P' . $entity_id] = array(
              'id' => 'P' . $entity_id,
              'title' => $grouping_entity->name,
              'children' => array(
                array(
                  'id' => $unit_type->type_id,
                  'title' => $unit_type->name,
                ),
              ),
            );
          }
        }
      }
      else {
        if ($return_children) {
          $units[$unit_type->type_id] = array(
            'id' => $unit_type->type_id,
            'title' => $unit_type->name,
            'children' => $children,
          );
        }
        else {
          $units[$unit_type->type_id] = array(
            'id' => $unit_type->type_id,
            'title' => $unit_type->name,
          );
        }
      }
    }
  }
  if ($collapse_childrens) {
    foreach ($units as $id => $unit1) {
      if (count($unit1['children']) == 1) {
        foreach ($unit1['children'] as $unit2) {
          if (count($unit2['children']) == 1) {
            $units[$id] = $unit2['children'][0];
            $units[$id]['title'] = $unit1['title'];
          }
        }
      }
      else {
        foreach ($unit1['children'] as $id2 => $unit2) {
          if (count($unit2['children']) == 1) {
            $units[$id]['children'][$id2] = $unit2['children'][0];
            $units[$id]['children'][$id2]['title'] = $unit2['title'];
          }
        }
      }
    }
  }
  drupal_alter('bat_api_units_index_calendar', $units);
  return array_values($units);
}