You are here

class RoomsBookingController in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

The Controller for RoomsBooking entities.

Hierarchy

Expanded class hierarchy of RoomsBookingController

1 string reference to 'RoomsBookingController'
rooms_booking_entity_info in modules/rooms_booking/rooms_booking.module
Implements hook_entity_info().

File

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

View source
class RoomsBookingController extends EntityAPIController {

  /**
   * Create a booking - we first set up the values that are specific
   * to our booking but then also go through the EntityAPIController
   * function.
   *
   * @param array $values
   *   The booking to create properties.
   *
   * @return object
   *   A booking object with all default fields initialized.
   */
  public function create(array $values = array()) {
    $booking_type = rooms_booking_type_load($values['type'], TRUE);

    // Add values that are specific to our Room.
    $values += array(
      'booking_id' => '',
      'is_new' => TRUE,
      'title' => '',
      'created' => '',
      'changed' => '',
      'order_id' => '',
      'data' => array(),
    );
    $booking = parent::create($values);
    return $booking;
  }

  /**
   * {@inheritdoc}
   */
  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();
      }
    }
  }
  public function delete($ids, $delete_line_item = TRUE) {
    foreach ($ids as $id) {
      $booking = rooms_booking_load($id);

      // Update the availability calendar.
      $this
        ->delete_event($booking);
      if ($delete_line_item) {

        // Delete the line_item associated with this booking.
        $this
          ->delete_line_item($booking);
      }
    }
    parent::delete($ids);
  }
  public function delete_event($booking) {

    // Check if the booking had a unit associated with it and if so update the
    // availability calendar.
    if (isset($booking->unit_id) && isset($booking->start_date) && isset($booking->end_date)) {
      $uc = new UnitCalendar($booking->unit_id);

      // We are not concerned with the state of the event id (confirmed or
      // unconfirmed here) because we will unlock it no matter what (we look for
      // absolute value).
      $event_id = rooms_availability_assign_id($booking->booking_id);

      // Create an event representing the event to remove.
      $start_date = $booking->start_date_object;
      $end_date = $booking->end_date_object;

      // Remove a day from end date to represent the actual event.
      $end_date
        ->sub(new DateInterval('P1D'));
      $be = new BookingEvent($booking->unit_id, $event_id, $start_date, $end_date);
      $uc
        ->removeEvents(array(
        $be,
      ));
    }
  }
  protected function delete_line_item($booking) {
    if ($booking->order_id != '') {
      $order = commerce_order_load($booking->order_id);
      if (isset($order->commerce_line_items[LANGUAGE_NONE])) {
        foreach ($order->commerce_line_items[LANGUAGE_NONE] as $value) {
          $line_item = commerce_line_item_load($value['line_item_id']);
          if ($line_item->rooms_booking_reference[LANGUAGE_NONE][0]['target_id'] == $booking->booking_id) {
            commerce_line_item_delete($line_item->line_item_id);
            if (count($order->commerce_line_items) == 0) {
              commerce_order_delete($order->order_number);
            }
            break;
          }
        }
      }
    }
  }

  /**
   * Overriding the buildContent function to add entity specific fields.
   */
  public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
    $content = parent::buildContent($entity, $view_mode, $langcode, $content);
    return $content;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
EntityAPIController::$bundleKey protected property
EntityAPIController::$cacheComplete protected property
EntityAPIController::$defaultRevisionKey protected property
EntityAPIController::buildQuery protected function Overrides DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController::buildQuery 1
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::export public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::export 1
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::invoke public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::invoke 1
EntityAPIController::load public function Overridden. Overrides DrupalDefaultEntityController::load 1
EntityAPIController::query public function Builds and executes the query for loading.
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides DrupalDefaultEntityController::resetCache 1
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIController::view public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::view 1
EntityAPIController::__construct public function Overridden. Overrides DrupalDefaultEntityController::__construct 1
RoomsBookingController::buildContent public function Overriding the buildContent function to add entity specific fields. Overrides EntityAPIController::buildContent
RoomsBookingController::create public function Create a booking - we first set up the values that are specific to our booking but then also go through the EntityAPIController function. Overrides EntityAPIController::create
RoomsBookingController::delete public function Implements EntityAPIControllerInterface. Overrides EntityAPIController::delete
RoomsBookingController::delete_event public function
RoomsBookingController::delete_line_item protected function
RoomsBookingController::save public function Implements EntityAPIControllerInterface. Overrides EntityAPIController::save