You are here

public function RoomsBookingController::save in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Implements EntityAPIControllerInterface.

Parameters

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

Overrides EntityAPIController::save

File

modules/rooms_booking/rooms_booking.module, line 1077
Manage Bookings - Bookings are tied to a customer profile and possible a Unit ID and Order ID.

Class

RoomsBookingController
The Controller for RoomsBooking entities.

Code

public function save($entity, DatabaseTransaction $transaction = NULL) {
  $entity->original = entity_load_unchanged($this->entityType, $entity->{$this->idKey});
  $corrected_end_date = new DateTime($entity->end_date);
  $corrected_end_date
    ->sub(new DateInterval('P1D'));

  // We are going to be updating the event - so the first step is to remove
  // the old event unless this is a booking we are deleting - in which case we
  // have already removed the event.
  if (!isset($entity->is_new) && $entity->unit_id != 0 && $entity->original->start_date != '' && $entity->original->end_date != '') {

    // Create a calendar.
    $uc = new UnitCalendar($entity->original->unit_id);
    $event_id = rooms_availability_assign_id($entity->booking_id, $entity->booking_status);

    // The original end date of the BookingEvent to remove
    $corrected_original_end_date = new DateTime($entity->original->end_date);
    $corrected_original_end_date
      ->sub(new DateInterval('P1D'));

    // Create an event representing the event to remove.
    $be = new BookingEvent($entity->original->unit_id, $event_id, new DateTime($entity->original->start_date), $corrected_original_end_date);
    $uc
      ->removeEvents(array(
      $be,
    ));
  }
  parent::save($entity);

  // We have a unit defined so lets block availability there unless its a
  // booking that is to be deleted.
  if ($entity->unit_id != 0) {

    // Set the event_id.
    $event_id = rooms_availability_assign_id($entity->booking_id, $entity->booking_status);

    // Create an event.
    $be = new BookingEvent($entity->unit_id, $event_id, new DateTime($entity->start_date), $corrected_end_date);

    // Create UnitCalendar.
    $rc = new UnitCalendar($entity->unit_id);
    $responses = $rc
      ->updateCalendar(array(
      $be,
    ));
    $entity->rooms_av_update = $responses[$event_id];
    if ($responses[$event_id] == ROOMS_UPDATED) {
      $be
        ->lock();
    }
  }
}