2019-08-09 21:25:21 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2019, Thomas Citharel
|
|
|
|
* @copyright Copyright (c) 2019, Georg Ehrke
|
|
|
|
*
|
|
|
|
* @author Thomas Citharel <tcit@tcit.fr>
|
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
|
|
|
*
|
|
|
|
* @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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OCA\DAV\CalDAV\Reminder\NotificationProvider;
|
|
|
|
|
|
|
|
use OCA\DAV\CalDAV\Reminder\INotificationProvider;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\L10N\IFactory as L10NFactory;
|
|
|
|
use OCP\IUser;
|
|
|
|
use Sabre\VObject\Component\VEvent;
|
|
|
|
use Sabre\VObject\DateTimeParser;
|
|
|
|
use Sabre\VObject\Property;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AbstractProvider
|
|
|
|
*
|
|
|
|
* @package OCA\DAV\CalDAV\Reminder\NotificationProvider
|
|
|
|
*/
|
|
|
|
abstract class AbstractProvider implements INotificationProvider {
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
public const NOTIFICATION_TYPE = '';
|
|
|
|
|
2019-08-12 14:20:03 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
2019-08-09 21:25:21 +03:00
|
|
|
|
2019-08-12 14:20:03 +03:00
|
|
|
/** @var L10NFactory */
|
|
|
|
private $l10nFactory;
|
2019-08-09 21:25:21 +03:00
|
|
|
|
2019-08-12 14:20:03 +03:00
|
|
|
/** @var IL10N[] */
|
2019-08-09 21:25:21 +03:00
|
|
|
private $l10ns;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $fallbackLanguage;
|
|
|
|
|
2019-08-12 14:20:03 +03:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
2019-08-09 21:25:21 +03:00
|
|
|
|
2019-08-12 14:20:03 +03:00
|
|
|
/** @var IConfig */
|
2019-08-09 21:25:21 +03:00
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param L10NFactory $l10nFactory
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param IUrlGenerator $urlGenerator
|
|
|
|
*/
|
|
|
|
public function __construct(ILogger $logger,
|
|
|
|
L10NFactory $l10nFactory,
|
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
IConfig $config) {
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->l10nFactory = $l10nFactory;
|
2019-08-12 14:20:03 +03:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2019-08-09 21:25:21 +03:00
|
|
|
$this->config = $config;
|
2019-08-12 14:20:03 +03:00
|
|
|
}
|
2019-08-09 21:25:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send notification
|
|
|
|
*
|
|
|
|
* @param VEvent $vevent
|
|
|
|
* @param string $calendarDisplayName
|
|
|
|
* @param IUser[] $users
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-08-12 14:20:03 +03:00
|
|
|
abstract public function send(VEvent $vevent,
|
2019-08-09 21:25:21 +03:00
|
|
|
string $calendarDisplayName,
|
|
|
|
array $users=[]): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-08-12 14:20:03 +03:00
|
|
|
protected function getFallbackLanguage():string {
|
|
|
|
if ($this->fallbackLanguage) {
|
|
|
|
return $this->fallbackLanguage;
|
2019-08-09 21:25:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-12 14:20:03 +03:00
|
|
|
$fallbackLanguage = $this->l10nFactory->findLanguage();
|
|
|
|
$this->fallbackLanguage = $fallbackLanguage;
|
2019-08-09 21:25:21 +03:00
|
|
|
|
2019-08-12 14:20:03 +03:00
|
|
|
return $fallbackLanguage;
|
2019-08-09 21:25:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $lang
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function hasL10NForLang(string $lang):bool {
|
2019-08-12 14:20:03 +03:00
|
|
|
return $this->l10nFactory->languageExists('dav', $lang);
|
2019-08-09 21:25:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $lang
|
|
|
|
* @return IL10N
|
|
|
|
*/
|
|
|
|
protected function getL10NForLang(string $lang):IL10N {
|
|
|
|
if (isset($this->l10ns[$lang])) {
|
|
|
|
return $this->l10ns[$lang];
|
|
|
|
}
|
|
|
|
|
|
|
|
$l10n = $this->l10nFactory->get('dav', $lang);
|
|
|
|
$this->l10ns[$lang] = $l10n;
|
|
|
|
|
|
|
|
return $l10n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param VEvent $vevent
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getStatusOfEvent(VEvent $vevent):string {
|
|
|
|
if ($vevent->STATUS) {
|
|
|
|
return (string) $vevent->STATUS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't say so in the standard,
|
|
|
|
// but we consider events without a status
|
|
|
|
// to be confirmed
|
|
|
|
return 'CONFIRMED';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param VEvent $vevent
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isEventTentative(VEvent $vevent):bool {
|
|
|
|
return $this->getStatusOfEvent($vevent) === 'TENTATIVE';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param VEvent $vevent
|
|
|
|
* @return Property\ICalendar\DateTime
|
|
|
|
*/
|
|
|
|
protected function getDTEndFromEvent(VEvent $vevent):Property\ICalendar\DateTime {
|
|
|
|
if (isset($vevent->DTEND)) {
|
|
|
|
return $vevent->DTEND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($vevent->DURATION)) {
|
|
|
|
$isFloating = $vevent->DTSTART->isFloating();
|
|
|
|
/** @var Property\ICalendar\DateTime $end */
|
|
|
|
$end = clone $vevent->DTSTART;
|
|
|
|
$endDateTime = $end->getDateTime();
|
|
|
|
$endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
|
|
|
|
$end->setDateTime($endDateTime, $isFloating);
|
|
|
|
|
|
|
|
return $end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$vevent->DTSTART->hasTime()) {
|
|
|
|
$isFloating = $vevent->DTSTART->isFloating();
|
|
|
|
/** @var Property\ICalendar\DateTime $end */
|
|
|
|
$end = clone $vevent->DTSTART;
|
|
|
|
$endDateTime = $end->getDateTime();
|
|
|
|
$endDateTime = $endDateTime->modify('+1 day');
|
|
|
|
$end->setDateTime($endDateTime, $isFloating);
|
|
|
|
|
|
|
|
return $end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone $vevent->DTSTART;
|
|
|
|
}
|
|
|
|
}
|