class RoomsBookingController in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7
The Controller for RoomsBooking entities.
Hierarchy
- class \DrupalDefaultEntityController implements DrupalEntityControllerInterface
- class \EntityAPIController implements EntityAPIControllerRevisionableInterface
- class \RoomsBookingController
- class \EntityAPIController implements EntityAPIControllerRevisionableInterface
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DrupalDefaultEntityController:: |
protected | property | Whether this entity type should use the static cache. | |
DrupalDefaultEntityController:: |
protected | property | Static cache of entities, keyed by entity ID. | |
DrupalDefaultEntityController:: |
protected | property | Array of information about the entity. | |
DrupalDefaultEntityController:: |
protected | property | Entity type for this controller instance. | |
DrupalDefaultEntityController:: |
protected | property | Additional arguments to pass to hook_TYPE_load(). | |
DrupalDefaultEntityController:: |
protected | property | Name of the entity's ID field in the entity database table. | |
DrupalDefaultEntityController:: |
protected | property | Name of entity's revision database table field, if it supports revisions. | |
DrupalDefaultEntityController:: |
protected | property | The table that stores revisions, if the entity supports revisions. | |
DrupalDefaultEntityController:: |
protected | function | Attaches data to entities upon loading. | 4 |
DrupalDefaultEntityController:: |
protected | function | Gets entities from the static cache. | 1 |
DrupalDefaultEntityController:: |
protected | function | Stores entities in the static entity cache. | |
DrupalDefaultEntityController:: |
protected | function | Ensures integer entity IDs are valid. | |
DrupalDefaultEntityController:: |
protected | function | Callback for array_filter that removes non-integer IDs. | |
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
protected | function |
Overrides DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController:: |
1 |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface:: |
|
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
|
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function |
Overridden. Overrides DrupalDefaultEntityController:: |
1 |
EntityAPIController:: |
public | function | Builds and executes the query for loading. | |
EntityAPIController:: |
protected | function | Renders a single entity property. | |
EntityAPIController:: |
public | function |
Overrides DrupalDefaultEntityController::resetCache(). Overrides DrupalDefaultEntityController:: |
1 |
EntityAPIController:: |
protected | function | Saves an entity revision. | |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function |
Overridden. Overrides DrupalDefaultEntityController:: |
1 |
RoomsBookingController:: |
public | function |
Overriding the buildContent function to add entity specific fields. Overrides EntityAPIController:: |
|
RoomsBookingController:: |
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:: |
|
RoomsBookingController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIController:: |
|
RoomsBookingController:: |
public | function | ||
RoomsBookingController:: |
protected | function | ||
RoomsBookingController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIController:: |