|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author René Gieling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
namespace OCA\Polls\Service; |
|
26
|
|
|
|
|
27
|
|
|
use DateTime; |
|
28
|
|
|
use OCP\Calendar\IManager as CalendarManager; |
|
29
|
|
|
use OCP\Calendar\ICalendar; |
|
30
|
|
|
|
|
31
|
|
|
class CalendarService { |
|
32
|
|
|
|
|
33
|
|
|
private $calendarManager; |
|
34
|
|
|
private $calendars; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
CalendarManager $calendarManager |
|
38
|
|
|
) { |
|
39
|
|
|
$this->calendarManager = $calendarManager; |
|
40
|
|
|
$this->calendars = $this->calendarManager->getCalendars(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* getEvents - get events from the user's calendars inside given timespan |
|
45
|
|
|
* @NoAdminRequired |
|
46
|
|
|
* @param DateTime $from |
|
47
|
|
|
* @param DateTime $to |
|
48
|
|
|
* @return Array |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getEvents($from, $to) { |
|
51
|
|
|
$events = []; |
|
52
|
|
|
|
|
53
|
|
|
foreach ($this->calendars as $calendar) { |
|
54
|
|
|
$foundEvents = $calendar->search('' ,['SUMMARY'], ['timerange' => ['start' => $from, 'end' => $to]]); |
|
55
|
|
|
foreach ($foundEvents as $event) { |
|
56
|
|
|
array_push($events, [ |
|
57
|
|
|
'relatedFrom' => $from->getTimestamp(), |
|
58
|
|
|
'relatedTo' => $to->getTimestamp(), |
|
59
|
|
|
'name' => $calendar->getDisplayName(), |
|
60
|
|
|
'key' => $calendar->getKey(), |
|
61
|
|
|
'displayColor' => $calendar->getDisplayColor(), |
|
62
|
|
|
'permissions' => $calendar->getPermissions(), |
|
63
|
|
|
'eventId' => $event['id'], |
|
64
|
|
|
'UID' => $event['objects'][0]['UID'][0], |
|
65
|
|
|
'summary' => isset($event['objects'][0]['SUMMARY'][0])? $event['objects'][0]['SUMMARY'][0] : '', |
|
66
|
|
|
'description' => isset($event['objects'][0]['DESCRIPTION'][0])? $event['objects'][0]['DESCRIPTION'][0] : '', |
|
67
|
|
|
'location' => isset($event['objects'][0]['LOCATION'][0]) ? $event['objects'][0]['LOCATION'][0] : '', |
|
68
|
|
|
'eventFrom' => isset($event['objects'][0]['DTSTART'][0]) ? $event['objects'][0]['DTSTART'][0]->getTimestamp() : 0, |
|
69
|
|
|
'eventTo' => isset($event['objects'][0]['DTEND'][0] ) ? $event['objects'][0]['DTEND'][0]->getTimestamp() : 0, |
|
70
|
|
|
'calDav' => $event |
|
71
|
|
|
]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
return $events; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get user's calendars |
|
79
|
|
|
* @NoAdminRequired |
|
80
|
|
|
* @return Array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getCalendars() { |
|
83
|
|
|
return $this->calendars; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get events from the user's calendar which are 2 hours before and 3 hours after the timestamp |
|
89
|
|
|
* @NoAdminRequired |
|
90
|
|
|
* @param DateTime $from |
|
91
|
|
|
* @return Array |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getEventsAround($from) { |
|
94
|
|
|
$from = new DateTime($from); |
|
|
|
|
|
|
95
|
|
|
$to = new DateTime($from); |
|
96
|
|
|
return $this->getEvents( |
|
97
|
|
|
$from->sub(new DateInterval('P2H')), |
|
|
|
|
|
|
98
|
|
|
$to->add(new DateInterval('P3H')) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|