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
|
<?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
)
);
}
public function getPreferenceList(DbUser $user) {
$calendars = $user->getCalendarPreference();
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 addToPreference(DbUser $user, $calendarID) {
if (!$user->IsGuest) {
$calendar = Calendar::finder()->findByPk($calendarID);
if ($calendar) {
$preference = new UserPreference();
$preference->CalendarID = $calendar->UID;
$preference->UserID = $user->DbRecord->ID;
$preference->save();
}
}
}
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 getEventsForTimeframe(CalendarDTO $calendar,
DateTime $dateFrom,
DateTime $dateTo,
$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() {
return Calendar::finder()->withCategory()->findAll('ORDER BY name ASC');
}
public function getCategories() {
return Category::finder()->findAll('ORDER BY name ASC');
}
public function get($uid) {
return Calendar::finder()->withCategory()->findAllByPks($uid);
}
public function resolveUrl($url) {
$dto = new CalendarDTO();
$record = Calendar::finder()->findByCustomUrl($url);
if ($record) {
$dto->loadRecord($record);
return $dto;
}
return NULL;
}
}
?>
|