You are here

function bat_event_unit_set_state in Booking and Availability Management Tools for Drupal 7

VBO action: Assign fixed-state event to units.

1 string reference to 'bat_event_unit_set_state'
bat_event_action_info in modules/bat_event/bat_event.module
Implements hook_action_info().

File

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

Code

function bat_event_unit_set_state(&$unit, $context) {
  $type = bat_type_load($unit->type_id);
  $type_bundle = bat_type_bundle_load($type->type);
  $event_state = $context['form_values']['event_state'];
  $event_type = $context['form_values']['event_type'];
  $start_date = new DateTime($context['form_values']['bat_start_date']);
  $end_date = new DateTime($context['form_values']['bat_end_date']);
  $end_date
    ->sub(new DateInterval('PT1M'));
  if (isset($type_bundle->default_event_value_field_ids[$event_type]) && !empty($type_bundle->default_event_value_field_ids[$event_type])) {
    $event = bat_event_create(array(
      'type' => $event_type,
      'start_date' => $start_date
        ->format('Y-m-d H:i:s'),
      'end_date' => $end_date
        ->format('Y-m-d H:i:s'),
      'uid' => $type->uid,
      'created' => REQUEST_TIME,
    ));
    $event->event_bat_unit_reference[LANGUAGE_NONE][0]['target_id'] = $unit->unit_id;
    $state = bat_event_load_state_by_machine_name($event_state);
    $event->event_state_reference[LANGUAGE_NONE][0]['state_id'] = $state['id'];
    $event
      ->save();
  }
}