You are here

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

Takes a single event that spans several months and transforms it to monthly events - this assumes that the event is contained within a year

Return value

RoomsEventInterface[] The event split if is necessary, a single item array otherwise.

Overrides RoomsEventInterface::transformToMonthlyEvents

File

includes/rooms.event.inc, line 137

Class

RoomsEvent

Code

public function transformToMonthlyEvents() {
  $events = array();

  //First we need to split into events in separate years
  if (!$this
    ->sameYear()) {
    return FALSE;
  }
  if ($this
    ->sameMonth()) {
    $sd = new DateTime();
    $sd
      ->setDate($this
      ->startYear(), $this
      ->startMonth(), $this
      ->startDay());
    $ed = new DateTime();
    $ed
      ->setDate($this
      ->endYear(), $this
      ->endMonth(), $this
      ->endDay());
    $be = $this
      ->createEvent($sd, $ed);
    return array(
      $be,
    );
  }
  $months = rooms_end_of_month_dates($this
    ->startYear());
  for ($i = $this
    ->startMonth(); $i <= $this
    ->endMonth(); $i++) {
    $sd = new DateTime();
    $ed = new DateTime();
    if ($i == $this
      ->startMonth()) {
      $sd
        ->setDate($this
        ->startYear(), $i, $this
        ->startDay());
      $ed
        ->setDate($this
        ->startYear(), $i, $months[$i]);
      $events[$i] = $this
        ->createEvent($sd, $ed);
    }
    elseif ($i == $this
      ->endMonth()) {
      $sd
        ->setDate($this
        ->startYear(), $i, 1);
      $ed
        ->setDate($this
        ->startYear(), $i, $this
        ->endDay());
      $events[$i] = $this
        ->createEvent($sd, $ed);
    }
    else {
      $sd
        ->setDate($this
        ->startYear(), $i, 1);
      $ed
        ->setDate($this
        ->startYear(), $i, $months[$i]);
      $events[$i] = $this
        ->createEvent($sd, $ed);
    }
  }
  return $events;
}