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\ICalendar; |
29
|
|
|
use OCP\Calendar\IManager as CalendarManager; |
30
|
|
|
use OCA\Polls\Model\CalendarEvent; |
31
|
|
|
use OCA\Polls\Db\Preferences; |
32
|
|
|
|
33
|
|
|
class CalendarService { |
34
|
|
|
|
35
|
|
|
/** @var CalendarManager */ |
36
|
|
|
private $calendarManager; |
37
|
|
|
|
38
|
|
|
/** @var ICalendar[] */ |
39
|
|
|
private $calendars; |
40
|
|
|
|
41
|
|
|
/** @var PreferencesService */ |
42
|
|
|
private $preferencesService; |
43
|
|
|
|
44
|
|
|
/** @var Preferences */ |
45
|
|
|
private $preferences; |
46
|
|
|
|
47
|
|
|
public function __construct( |
48
|
|
|
CalendarManager $calendarManager, |
49
|
|
|
PreferencesService $preferencesService |
50
|
|
|
) { |
51
|
|
|
$this->calendarManager = $calendarManager; |
52
|
|
|
$this->preferencesService = $preferencesService; |
53
|
|
|
$this->preferences = $this->preferencesService->get(); |
54
|
|
|
$this->calendars = $this->calendarManager->getCalendars(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* getEvents - get events from the user's calendars inside given timespan |
59
|
|
|
* |
60
|
|
|
* @param DateTime $from |
61
|
|
|
* @param DateTime $to |
62
|
|
|
* |
63
|
|
|
* @return CalendarEvent[] |
64
|
|
|
* |
65
|
|
|
* @psalm-return list<CalendarEvent> |
66
|
|
|
*/ |
67
|
|
|
public function getEvents(DateTime $from, DateTime $to): array { |
68
|
|
|
$events = []; |
69
|
|
|
foreach ($this->calendars as $calendar) { |
70
|
|
|
|
71
|
|
|
// Skip not configured calendars |
72
|
|
|
if (!in_array($calendar->getKey(), json_decode($this->preferences->getPreferences())->checkCalendars)) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// search for all events which |
77
|
|
|
// - start before the end of the requested timespan ($to) and |
78
|
|
|
// - end after the start of the requested timespan ($from) |
79
|
|
|
$foundEvents = $calendar->search('', ['SUMMARY'], ['timerange' => ['start' => $from, 'end' => $to]]); |
80
|
|
|
foreach ($foundEvents as $event) { |
81
|
|
|
$calendarEvent = new CalendarEvent($event, $calendar); |
82
|
|
|
|
83
|
|
|
// since we get back recurring events of other days, just make sure this event |
84
|
|
|
// matches the search pattern |
85
|
|
|
// TODO: identify possible time zone issues, when handling all day events |
86
|
|
|
if (($from->getTimestamp() < $calendarEvent->getEnd()) |
87
|
|
|
&& ($to->getTimestamp() > $calendarEvent->getStart())) { |
88
|
|
|
array_push($events, $calendarEvent); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return $events; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get user's calendars |
97
|
|
|
* |
98
|
|
|
* @return array[] |
99
|
|
|
* |
100
|
|
|
* @psalm-return list<array{name: mixed, key: mixed, displayColor: mixed, permissions: mixed}> |
101
|
|
|
*/ |
102
|
|
|
public function getCalendars(): array { |
103
|
|
|
$calendars = []; |
104
|
|
|
foreach ($this->calendars as $calendar) { |
105
|
|
|
$calendars[] = [ |
106
|
|
|
'name' => $calendar->getDisplayName(), |
107
|
|
|
'key' => $calendar->getKey(), |
108
|
|
|
'displayColor' => $calendar->getDisplayColor(), |
109
|
|
|
'permissions' => $calendar->getPermissions(), |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
return $calendars; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|