diff options
-rw-r--r-- | app/php/dto/EventDTO.php | 54 |
1 files changed, 40 insertions, 14 deletions
diff --git a/app/php/dto/EventDTO.php b/app/php/dto/EventDTO.php index 4131210..8f5cdf5 100644 --- a/app/php/dto/EventDTO.php +++ b/app/php/dto/EventDTO.php @@ -11,29 +11,53 @@ class EventDTO { public $Location; public $Calendar; - public function loadRecord(Entry $event, array $calendars) { - $this->Name = $event->Name; - $this->Location = $event->Location; + private $_utc; + private $_targetTZ; - $utc = new DateTimeZone('UTC'); - $targetTz = new DateTimeZone( - UserFacade::getInstance()->getTimezonePreference( + public function __construct(TimezoneDTO $tz = NULL) { + $this->_utc = new DateTimeZone('UTC'); + $this->_targetTZ = new DateTimeZone( + $tz + ? $tz->Name + : UserFacade::getInstance()->getTimezonePreference( Prado::getApplication()->getUser() )->Name ); - $beginDate = new DateTime($event->BeginDate, $utc); - $endDate = new DateTime($event->EndDate, $utc); + } + + private $_beginDate; + protected function getBeginDate(Entry $event) { + if (!$this->_beginDate) { + $this->_beginDate = new DateTime($event->BeginDate, $this->_utc); + } + return $this->_beginDate; + } + + private $_endDate; + protected function getEndDate(Entry $event) { + if (!$this->_endDate) { + $this->_endDate = new DateTime($event->EndDate, $this->_utc); + if ($event->AllDay) { + $this->_endDate = $this->_endDate->modify('-1 day'); + } + } + return $this->_endDate; + } + + public function loadRecord(Entry $event, array $calendars) { + $this->Name = $event->Name; + $this->Location = $event->Location; + if ($event->AllDay) { - $endDate = $endDate->modify('-1 day'); - $this->DateString = $beginDate->format('Y-m-d'); - if ($beginDate != $endDate) { + $this->DateString = $this->getBeginDate($event)->format('Y-m-d'); + if ($this->getBeginDate($event) != $this->getEndDate($event)) { $this->DateString .= sprintf( ' - %s', - $endDate->format('Y-m-d') + $this->getEndDate($event)->format('Y-m-d') ); } } else { - $beginDate = $beginDate->setTimezone($targetTz); + $beginDate = $this->getBeginDate($event)->setTimezone($this->_targetTZ); $this->DateString = $beginDate->format('Y-m-d H:i'); } @@ -44,7 +68,9 @@ class EventDTO { } ); $this->Calendar = new CalendarDTO(); - $this->Calendar->loadRecord($calendars ? array_values($calendars)[0] : $event->Calendar); + $this->Calendar->loadRecord( + $calendars ? array_values($calendars)[0] : $event->Calendar + ); } public static function __compare(EventDTO $ev1, EventDTO $ev2) { |