summaryrefslogtreecommitdiff
path: root/app/php/dto/EventDTO.php
blob: b1042b107cc77a9846767b6cc61aeaa52747fd3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

Prado::using('Application.model.Entry');
Prado::using('Application.dto.CalendarDTO');

class EventDTO {

    public $DateString;
    public $Name;
    public $Location;
    public $Calendar;

    public function loadRecord(Entry $event, array $calendars) {
        $this->Name = $event->Name;
        $this->Location = $event->Location;

        $utc = new DateTimeZone('UTC');
        $targetTz = new DateTimeZone(
            Prado::getApplication()->getUser()->getTimezonePreference()->Name
        );
        $beginDate = new DateTime($event->BeginDate, $utc);
        $endDate = new DateTime($event->EndDate, $utc);
        if ($event->AllDay) {
            $endDate = $endDate->modify('-1 day');
            $this->DateString = $beginDate->format('Y-m-d');
            if ($beginDate != $endDate) {
                $this->DateString .= sprintf(
                    ' - %s',
                    $endDate->format('Y-m-d')
                );
            }
        } else {
            $beginDate = $beginDate->setTimezone($targetTz);
            $this->DateString = $beginDate->format('Y-m-d H:i');
        }

        $calendars = array_filter(
            $calendars,
            function ($calendar) use($event) {
                return $calendar->UID == $event->CalendarID;
            }
        );
        $this->Calendar = new CalendarDTO();
        $this->Calendar->loadRecord($calendars ? array_values($calendars)[0] : $event->Calendar);
    }

}

?>