You are here

public function BatEventController::save in Booking and Availability Management Tools for Drupal 7

Implements EntityAPIControllerInterface.

Parameters

$transaction: Optionally a DatabaseTransaction object to use. Allows overrides to pass in their transaction object.

Overrides EntityAPIController::save

File

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

Class

BatEventController
The Controller for BatEvent entities.

Code

public function save($entity, DatabaseTransaction $transaction = NULL) {

  // Update start_date_object and end_date_object with the new dates.
  $entity->start_date_object = new DateTime($entity->start_date);
  $entity->end_date_object = new DateTime($entity->end_date);
  $event_type = bat_event_type_load($entity->type);

  // Construct target entity reference field name using this event type's target entity type.
  $target_field_name = 'event_' . $event_type->target_entity_type . '_reference';

  // We are going to be updating the event - so the first step is to remove
  // the old event.
  if (!isset($entity->is_new)) {
    $entity->original = entity_load_unchanged($this->entityType, $entity->{$this->idKey});
    if ($entity->original->start_date != '' && $entity->original->end_date != '' && field_get_items('bat_event', $entity->original, $target_field_name) !== FALSE) {

      // Get the referenced entity ID.
      $event_target_entity_reference = field_get_items('bat_event', $entity->original, $target_field_name);
      $target_entity_id = $event_target_entity_reference[0]['target_id'];

      // Load the referenced entity.
      if ($target_entity = entity_load_single($event_type->target_entity_type, $target_entity_id)) {
        $unit = new Unit($target_entity_id, $target_entity
          ->getEventDefaultValue($event_type->type));
        $this
          ->batStoreSave($unit, new \DateTime($entity->original->start_date), new \DateTime($entity->original->end_date), $event_type->type, $event_type->event_granularity, $unit
          ->getDefaultValue(), $entity->event_id, TRUE);
      }
    }
  }
  parent::save($entity);

  // Now we store the new event.
  if (field_get_items('bat_event', $entity, $target_field_name) !== FALSE) {
    if (isset($event_type->default_event_value_field_ids[$entity->type])) {
      $field = $event_type->default_event_value_field_ids[$entity->type];
      $field_info = field_info_field($field);
      $values = field_get_items('bat_event', $entity, $field);
      if (!empty($values)) {
        if ($field_info['type'] == 'bat_event_state_reference') {
          $event_value = $values[0]['state_id'];
        }
        elseif ($field_info['type'] == 'commerce_price') {
          $event_value = $values[0]['amount'];
        }
        elseif ($field_info['type'] == 'text' || $field_info['type'] == 'number_integer' || $field_info['type'] == 'number_decimal') {
          $event_value = $values[0]['value'];
        }
      }
    }
    else {
      $event_state_reference = field_get_items('bat_event', $entity, 'event_state_reference');
      $event_value = $event_state_reference[0]['state_id'];
    }
    $event_target_entity_reference = field_get_items('bat_event', $entity, $target_field_name);
    $target_entity_id = $event_target_entity_reference[0]['target_id'];
    if ($target_entity = entity_load_single($event_type->target_entity_type, $target_entity_id)) {
      $unit = new Unit($target_entity_id, $target_entity
        ->getEventDefaultValue($event_type->type));
      $this
        ->batStoreSave($unit, new \DateTime($entity->start_date), new \DateTime($entity->end_date), $event_type->type, $event_type->event_granularity, $event_value, $entity->event_id);
    }
  }
}