summaryrefslogtreecommitdiff
path: root/app/frontend/dto/EventDTO.php
blob: 8f5cdf54c7acbee27e21ed19f14981d39c924cb6 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php

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

class EventDTO {

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

    private $_utc;
    private $_targetTZ;

    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
        );
    }

    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) {
            $this->DateString = $this->getBeginDate($event)->format('Y-m-d');
            if ($this->getBeginDate($event) != $this->getEndDate($event)) {
                $this->DateString .= sprintf(
                    ' - %s',
                    $this->getEndDate($event)->format('Y-m-d')
                );
            }
        } else {
            $beginDate = $this->getBeginDate($event)->setTimezone($this->_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
        );
    }

    public static function __compare(EventDTO $ev1, EventDTO $ev2) {
        if ($ev1->DateString === $ev2->DateString) {
            return strcmp($ev1->Calendar->Name, $ev2->Calendar->Name);
        }
        return strcmp($ev1->DateString, $ev2->DateString);
    }

}

?>