You are here

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

@file Class BookingEvent

Hierarchy

Expanded class hierarchy of BookingEvent

1 string reference to 'BookingEvent'
UnitCalendar::getEvents in modules/rooms_availability/includes/rooms_availability.unit_calendar.inc
Given a date range returns an array of RoomEvents. The heavy lifting really takes place in the getRawDayData function - here we are simply acting as a factory for event objects

File

modules/rooms_availability/includes/rooms_availability.booking_event.inc, line 8
Class BookingEvent

View source
class BookingEvent extends RoomsEvent implements BookingEventInterface {

  /**
   * The type of event.
   *
   * @var int
   */
  public $id;

  /**
   * Constructs a BookingEvent instance.
   *
   * @param int $unit_id
   *   The bookable unit.
   * @param int $event_id
   *   The event ID.
   * @param DateTime $start_date
   *   The start date of the event.
   * @param DateTime $end_date
   *   The end date of the event.
   */
  public function __construct($unit_id, $event_id, $start_date, $end_date) {
    $this->unit_id = $unit_id;
    $this->id = $event_id;
    $this->start_date = $start_date;
    $this->end_date = $end_date;
  }

  /**
   * {@inheritdoc}
   */
  protected function createEvent(DateTime $start_date, DateTime $end_date) {
    return new BookingEvent($this->unit_id, $this->id, $start_date, $end_date);
  }

  /**
   * {@inheritdoc}
   */
  public function lock() {

    // Check that the event is not already locked.
    $query = db_select('rooms_booking_locks', 'l');
    $query
      ->addField('l', 'unit_id');
    $query
      ->addField('l', 'state');
    $query
      ->addField('l', 'locked');
    $query
      ->condition('l.unit_id', $this->unit_id);
    $query
      ->condition('l.state', $this->id);
    $query
      ->condition('l.locked', 1);
    $result = $query
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);
    if (count($result) == 1) {
      return FALSE;
    }
    else {
      $fields = array(
        'unit_id' => $this->unit_id,
        'state' => $this->id,
        'locked' => 1,
      );
      $lock = db_insert('rooms_booking_locks')
        ->fields($fields);
      $lock
        ->execute();
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function unlock() {
    db_delete('rooms_booking_locks')
      ->condition(db_or()
      ->condition('state', $this->id)
      ->condition('state', -$this->id))
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function formatJson($style = ROOMS_AVAILABILITY_ADMIN_STYLE, $unit_name = '') {
    $event = array(
      'id' => $this->id,
      'start' => $this
        ->startYear() . '-' . $this
        ->startMonth('m') . '-' . $this
        ->startDay('d') . 'T13:00:00',
      'end' => $this
        ->endYear() . '-' . $this
        ->endMonth('m') . '-' . $this
        ->endDay('d') . 'T13:00:00',
      'title' => $this->id,
    );

    // Check if we are dealing with a booking.
    if ($this->id > 10 || $this->id < -10) {

      // Get the actual booking id.
      $booking_id = rooms_availability_return_id($this->id);
      $booking = rooms_booking_load($booking_id);
      if ($style == ROOMS_AVAILABILITY_ADMIN_STYLE) {
        $name = isset($booking->name) && !empty($booking->name) ? $booking->name : t('Booking') . ': ' . $booking->booking_id;
        $interval = $this
          ->diff();
        if (strlen($name) > 7 && $interval->d < 1) {
          $event['title'] = substr($name, 0, 6) . '...';
        }
        else {
          $event['title'] = $name;
        }
      }
      elseif ($style == ROOMS_AVAILABILITY_GENERIC_STYLE) {
        $this->id = ROOMS_NOT_AVAILABLE;
        $event['id'] = ROOMS_NOT_AVAILABLE;
      }
    }
    $view_unit_name = array_filter(variable_get('rooms_view_unit_name', array(
      '',
    )));

    // Set the color.
    switch ($this->id) {
      case ROOMS_NOT_AVAILABLE:
        $event['color'] = variable_get('rooms_not_available_color', '#CC2727');
        $event['borderColor'] = variable_get('rooms_not_available_color', '#CC2727');
        $event['title'] = variable_get('rooms_not_available_text', 'N/A');
        break;
      case ROOMS_AVAILABLE:
        $event['color'] = variable_get('rooms_available_color', '#8BA175');
        $event['borderColor'] = '#8BA175';
        $event['title'] = variable_get('rooms_available_text', 'AV');
        break;
      case ROOMS_ON_REQUEST:
        $event['color'] = variable_get('rooms_on_request_color', '#C5C5C5');
        $event['title'] = variable_get('rooms_on_request_text', 'ON-REQ');
        break;
      case $this->id < 0:
        $event['color'] = variable_get('rooms_unconfirmed_booking_color', '#6D8C9C');
        $event['title'] = variable_get('rooms_unconfirmed_booking_text', 'UNCONF');
        break;
      case ROOMS_ANON_BOOKED:
        if ($style == ROOMS_AVAILABILITY_ADMIN_STYLE) {
          $event['color'] = variable_get('rooms_anon_booking_color', '#8C6A5A');
          $event['title'] = variable_get('rooms_anon_booking_text', 'A-B');
        }
        elseif ($style == ROOMS_AVAILABILITY_GENERIC_STYLE) {
          $event['color'] = variable_get('rooms_not_available_color', '#910a1c');
          $event['title'] = variable_get('rooms_not_available_text', 'N/A');
        }
        break;
      default:
        $event['color'] = '#017eba';
        break;
    }
    if (!empty($view_unit_name)) {
      $event['title'] = $unit_name;
    }
    return $event;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BookingEvent::$id public property The type of event.
BookingEvent::createEvent protected function Creates a new event Overrides RoomsEvent::createEvent
BookingEvent::formatJson public function Returns event in a format amenable to FullCalendar display or generally sensible JSON. Overrides BookingEventInterface::formatJson
BookingEvent::lock public function Locks event - updates the rooms_booking_locks table to indicate that this event is locked. Overrides BookingEventInterface::lock
BookingEvent::unlock public function Unlocks the event. Overrides BookingEventInterface::unlock
BookingEvent::__construct public function Constructs a BookingEvent instance.
RoomsEvent::$end_date public property The end date for the event.
RoomsEvent::$start_date public property The default state for the room if it has no specific booking.
RoomsEvent::$unit_id public property The booking unit the event is relevant to
RoomsEvent::diff public function Returns the date interval between end and start date. Overrides RoomsEventInterface::diff
RoomsEvent::endDay public function Returns the booking end day. Overrides RoomsEventInterface::endDay
RoomsEvent::endMonth public function Returns the booking end month. Overrides RoomsEventInterface::endMonth
RoomsEvent::endYear public function Returns the booking end year. Overrides RoomsEventInterface::endYear
RoomsEvent::sameMonth public function Checks if the event starts and ends in the same month. Overrides RoomsEventInterface::sameMonth
RoomsEvent::sameYear public function Checks if the event starts and ends in the same year. Overrides RoomsEventInterface::sameYear
RoomsEvent::startDay public function Returns the booking start day. Overrides RoomsEventInterface::startDay
RoomsEvent::startMonth public function Returns the booking start month. Overrides RoomsEventInterface::startMonth
RoomsEvent::startYear public function Returns the booking start year. Overrides RoomsEventInterface::startYear
RoomsEvent::transformToMonthlyEvents public function Takes a single event that spans several months and transforms it to monthly events - this assumes that the event is contained within a year Overrides RoomsEventInterface::transformToMonthlyEvents
RoomsEvent::transformToYearlyEvents public function Takes an event that spans several years and transforms it to yearly events Overrides RoomsEventInterface::transformToYearlyEvents