summaryrefslogtreecommitdiff
path: root/app/php/facades/CalendarFacade.php
blob: 48dda8e44157a3d0062aa7532e8d85c89c8553ba (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

Prado::using('Application.facades.Facade');
Prado::using('Application.facades.EventFacade');
Prado::using('Application.dto.CalendarDTO');
Prado::using('Application.dto.CalendarGroupDTO');
Prado::using('Application.model.Calendar');
Prado::using('Application.model.Category');
Prado::using('Application.model.UserPreference');
Prado::using('Application.user.DbUser');

class CalendarFacade extends Facade {

    private function _getCategoriesForCalendars(array $calendars) {
        return Category::finder()->findAllByPks(
            array_map(
                function($calendar) {
                    return $calendar->CategoryID;
                },
                $calendars
            )
        );
    }

    private $_defaultPreference = NULL;
    public function getDefaultPreference() {
        if ($this->_defaultPreference === NULL) {
            $this->_defaultPreference = Calendar::finder()->findAllByIsVisible(1);
        }
        return $this->_defaultPreference;
    }

    public function getCalendarPreference(DbUser $user) {
        if ($user->IsGuest) {
            return $this->getDefaultPreference();
        } else {
            return $user->DbRecord->Calendars;
        }
    }

    public function getPreferenceList(DbUser $user) {
        $calendars = $this->getCalendarPreference($user);
        if ($calendars) {
            $categories = array_map(
                function($category) use($calendars) {
                    $dto = new CalendarGroupDTO();
                    $dto->loadRecord($category, $calendars);
                    return $dto;
                },
                $this->_getCategoriesForCalendars($calendars)
            );
            usort($categories, ['CalendarGroupDTO', '__compare']);
            return $categories;
        }
        return [];
    }

    public function isCalendarPreferred(DbUser $user, $calendarID) {
        return in_array(
            $calendarID,
            array_map(
                function($calendar) {
                    return $calendar->UID;
                },
                $this->getCalendarPreference($user)
            )
        );
    }

    public function addToPreference(DbUser $user, $calendarID) {
        if (!$user->IsGuest) {
            $calendar = Calendar::finder()->findByPk($calendarID);
            if ($calendar) {
                $this->setPreferredCalendar($user->DbRecord, $calendar);
            }
        }
    }

    public function removeFromPreference(DbUser $user, $calendarID) {
        if (!$user->IsGuest) {
            $preferenceRecord = UserPreference::finder()->find(
                '_user = ? AND _calendar = ?',
                $user->DbRecord->ID,
                $calendarID
            );
            if ($preferenceRecord) {
                $preferenceRecord->delete();
            }
        }
    }

    public function setPreferredCalendar(User $user, Calendar $calendar) {
        $preference = new UserPreference();
        $preference->CalendarID = $calendar->UID;
        $preference->UserID = $user->ID;
        $preference->save();
    }

    public function setPreferredCalendars(User $user, array $calendars) {
        //TODO: remove old preference, optionally
        $transaction = $this->beginTransaction();
        try {
            foreach ($calendars as $calendar) {
                $this->setPreferredCalendar($user, $calendar);
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollback();
            throw $e;
        }
    }

    public function getEventsForTimeframe(CalendarDTO $calendar,
                                          DateTime $dateFrom,
                                          DateTime $dateTo,
                                          string $order = 'ASC') {
        $calendar = Calendar::finder()->findAllByUID($calendar->ID);
        if ($calendar) {
            $events = EventFacade::getInstance()->getEventList(
                $dateFrom->format('Y-m-d H:i:s'),
                $dateTo->format('Y-m-d H:i:s'),
                $calendar,
                $order
            );
            return array_map(
                function($event) use($calendar) {
                    $dto = new EventDTO();
                    $dto->loadRecord($event, $calendar);
                    return $dto;
                },
                $events
            );
        }
        return [];
    }

    public function getAll() {
        $records = Calendar::finder()->withCategory()->findAll('ORDER BY name ASC');
        foreach ($records as $record) {
            $this->_fillUrlCache($record);
        }
        return $records;
    }

    public function getCategories() {
        $categories = array_map(
            function($record) {
                $dto = new CalendarGroupDTO();
                $dto->loadRecord($record, []);
                return $dto;
            },
            Category::finder()->findAll()
        );
        usort($categories, ['CalendarGroupDTO', '__compare']);
        return $categories;
    }

    public function get($uid) {
        $records = Calendar::finder()->withCategory()->findAllByPks($uid);
        foreach ($records as $record) {
            $this->_fillUrlCache($record);
        }
        return $records;
    }

    private $_urlCache = [];
    private function _fillUrlCache(Calendar $record = NULL) {
        if ($record && $record->CustomUrl
            && !isset($this->_urlCache[$record->CustomUrl])) {
            $dto = new CalendarDTO();
            if ($record) {
                $dto->loadRecord($record);
            } else {
                $dto = NULL;
            }
            return $this->_urlCache[$record->CustomUrl] = $dto;
        }
    }

    public function resolveUrl(string $url = NULL) {
        if ($url) {
            if (isset($this->_urlCache[$url])) {
                return $this->_urlCache[$url];
            }
            $record = Calendar::finder()->findByCustomUrl($url);
            if ($record) {
                return $this->_fillUrlCache($record);
            }
        }
        return NULL;
    }

}

?>