From c679210764583b8a25df9c5e3a231eb0a8cfb9e3 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Mon, 17 Aug 2020 10:35:43 +0200 Subject: find possible calender conflicts --- lib/Controller/OptionController.php | 55 ++++++++++++++++++- lib/Service/CalendarService.php | 102 ++++++++++++++++++++++++++++++++++++ lib/Service/OptionService.php | 16 ++++++ 3 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 lib/Service/CalendarService.php (limited to 'lib') diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php index 3b1e7f1e..e8fd8cad 100644 --- a/lib/Controller/OptionController.php +++ b/lib/Controller/OptionController.php @@ -23,6 +23,8 @@ namespace OCA\Polls\Controller; +use DateTime; +use DateInterval; use Exception; use OCP\IRequest; @@ -31,12 +33,17 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCA\Polls\Service\OptionService; +use OCA\Polls\Service\CalendarService; + class OptionController extends Controller { /** @var OptionService */ private $optionService; + /** @var CalendarService */ + private $calendarService; + /** * OptionController constructor. * @param string $appName @@ -47,10 +54,12 @@ class OptionController extends Controller { public function __construct( string $appName, IRequest $request, - OptionService $optionService + OptionService $optionService, + CalendarService $calendarService ) { parent::__construct($appName, $request); $this->optionService = $optionService; + $this->calendarService = $calendarService; } /** @@ -113,4 +122,48 @@ class OptionController extends Controller { public function reorder($pollId, $options) { return new DataResponse(['options' => $this->optionService->reorder($pollId, $options)], Http::STATUS_OK); } + + /** + * findCalendarEvents + * @NoAdminRequired + * @param integer $from + * @param integer $to + * @return DataResponse + */ + public function findCalendarEvents($optionId) { + + $searchFrom = new DateTime(); + $searchFrom = $searchFrom->setTimestamp($this->optionService->get($optionId)->getTimestamp())->sub(new DateInterval('PT1H')); + $searchTo = clone $searchFrom; + $searchTo = $searchTo->add(new DateInterval('PT3H')); + + \OC::$server->getLogger()->alert('Search events between ' . $searchFrom->format('Y-m-d H:i:s') . ' and ' . $searchTo->format('Y-m-d H:i:s')); + + return new DataResponse(['events' => array_values($this->calendarService->getEvents($searchFrom, $searchTo))], Http::STATUS_OK); + + + if (is_int($from)) { + $searchFrom = new DateTime(); + $searchFrom = $searchFrom->setTimestamp($from); + } else { + $searchFrom = new DateTime($from); + } + + + if (!$to) { + $searchTo = clone $searchFrom; + $searchTo = $searchTo->add(new DateInterval('PT1H')); + + } else if (is_int($to)) { + $searchTo = new DateTime(); + $searchTo = $searchTo->setTimestamp($to); + } else { + $searchTo = new DateTime($to); + } + + $events = array_values($this->calendarService->getEvents($searchFrom, $searchTo)); + return $events; + + } + } diff --git a/lib/Service/CalendarService.php b/lib/Service/CalendarService.php new file mode 100644 index 00000000..68f9c0f2 --- /dev/null +++ b/lib/Service/CalendarService.php @@ -0,0 +1,102 @@ + + * + * @author René Gieling +* + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Polls\Service; + +use DateTime; +use OCP\Calendar\IManager as CalendarManager; +use OCP\Calendar\ICalendar; + +class CalendarService { + + private $calendarManager; + private $calendars; + + public function __construct( + CalendarManager $calendarManager + ) { + $this->calendarManager = $calendarManager; + $this->calendars = $this->calendarManager->getCalendars(); + } + + /** + * getEvents - get events from the user's calendars inside given timespan + * @NoAdminRequired + * @param DateTime $from + * @param DateTime $to + * @return Array + */ + public function getEvents($from, $to) { + $events = []; + + foreach ($this->calendars as $calendar) { + $foundEvents = $calendar->search('' ,['SUMMARY'], ['timerange' => ['start' => $from, 'end' => $to]]); + foreach ($foundEvents as $event) { + array_push($events, [ + 'relatedFrom' => $from->getTimestamp(), + 'relatedTo' => $to->getTimestamp(), + 'name' => $calendar->getDisplayName(), + 'key' => $calendar->getKey(), + 'displayColor' => $calendar->getDisplayColor(), + 'permissions' => $calendar->getPermissions(), + 'eventId' => $event['id'], + 'UID' => $event['objects'][0]['UID'][0], + 'summary' => isset($event['objects'][0]['SUMMARY'][0])? $event['objects'][0]['SUMMARY'][0] : '', + 'description' => isset($event['objects'][0]['DESCRIPTION'][0])? $event['objects'][0]['DESCRIPTION'][0] : '', + 'location' => isset($event['objects'][0]['LOCATION'][0]) ? $event['objects'][0]['LOCATION'][0] : '', + 'eventFrom' => isset($event['objects'][0]['DTSTART'][0]) ? $event['objects'][0]['DTSTART'][0]->getTimestamp() : 0, + 'eventTo' => isset($event['objects'][0]['DTEND'][0] ) ? $event['objects'][0]['DTEND'][0]->getTimestamp() : 0, + 'calDav' => $event + ]); + } + } + return $events; + } + + /** + * Get user's calendars + * @NoAdminRequired + * @return Array + */ + public function getCalendars() { + return $this->calendars; + } + + + /** + * Get events from the user's calendar which are 2 hours before and 3 hours after the timestamp + * @NoAdminRequired + * @param DateTime $from + * @return Array + */ + public function getEventsAround($from) { + $from = new DateTime($from); + $to = new DateTime($from); + return $this->getEvents( + $from->sub(new DateInterval('P2H')), + $to->add(new DateInterval('P3H')) + ); + } + +} diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php index 7208a154..0e2dbb88 100644 --- a/lib/Service/OptionService.php +++ b/lib/Service/OptionService.php @@ -103,6 +103,22 @@ class OptionService { } } + /** + * Get option + * @NoAdminRequired + * @param int $optionId + * @return Option + * @throws NotAuthorizedException + */ + public function get($optionId) { + + if (!$this->acl->set($this->optionMapper->find($optionId)->getPollId())->getAllowView()) { + throw new NotAuthorizedException; + } + + return $this->optionMapper->find($optionId); + } + /** * Add a new option -- cgit v1.2.3