View source
<?php
abstract class RoomsEvent implements RoomsEventInterface {
public $unit_id;
public $start_date;
public $end_date;
public function startDay($format = 'j') {
return $this->start_date
->format($format);
}
public function startMonth($format = 'n') {
return $this->start_date
->format($format);
}
public function startYear($format = 'Y') {
return $this->start_date
->format($format);
}
public function endDay($format = 'j') {
return $this->end_date
->format($format);
}
public function endMonth($format = 'n') {
return $this->end_date
->format($format);
}
public function endYear($format = 'Y') {
return $this->end_date
->format($format);
}
public function diff() {
$interval = $this->start_date
->diff($this->end_date);
return $interval;
}
public function sameMonth() {
if ($this
->startMonth() == $this
->endMonth() && $this
->startYear() == $this
->endYear()) {
return TRUE;
}
return FALSE;
}
public function sameYear() {
if ($this
->startYear() == $this
->endYear()) {
return TRUE;
}
return FALSE;
}
public function transformToYearlyEvents() {
if ($this
->sameYear()) {
$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,
);
}
$events = array();
for ($i = $this
->startYear(); $i <= $this
->endYear(); $i++) {
$sd = new DateTime();
$ed = new DateTime();
if ($i == $this
->startYear()) {
$sd
->setDate($i, $this
->startMonth(), $this
->startDay());
$ed
->setDate($i, 12, 31);
$events[$i] = $this
->createEvent($sd, $ed);
}
elseif ($i == $this
->endYear()) {
$sd
->setDate($i, 1, 1);
$ed
->setDate($i, $this
->endMonth(), $this
->endDay());
$events[$i] = $this
->createEvent($sd, $ed);
}
else {
$sd
->setDate($i, 1, 1);
$ed
->setDate($i, 12, 31);
$events[$i] = $this
->createEvent($sd, $ed);
}
}
return $events;
}
public function transformToMonthlyEvents() {
$events = array();
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;
}
protected abstract function createEvent(DateTime $start_date, DateTime $end_date);
}