AbstractProvider.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\Reminder\NotificationProvider;
  8. use OCA\DAV\CalDAV\Reminder\INotificationProvider;
  9. use OCP\IConfig;
  10. use OCP\IL10N;
  11. use OCP\IURLGenerator;
  12. use OCP\IUser;
  13. use OCP\L10N\IFactory as L10NFactory;
  14. use Psr\Log\LoggerInterface;
  15. use Sabre\VObject\Component\VEvent;
  16. use Sabre\VObject\DateTimeParser;
  17. use Sabre\VObject\Property;
  18. /**
  19. * Class AbstractProvider
  20. *
  21. * @package OCA\DAV\CalDAV\Reminder\NotificationProvider
  22. */
  23. abstract class AbstractProvider implements INotificationProvider {
  24. /** @var string */
  25. public const NOTIFICATION_TYPE = '';
  26. protected LoggerInterface $logger;
  27. /** @var L10NFactory */
  28. protected $l10nFactory;
  29. /** @var IL10N[] */
  30. private $l10ns;
  31. /** @var string */
  32. private $fallbackLanguage;
  33. /** @var IURLGenerator */
  34. protected $urlGenerator;
  35. /** @var IConfig */
  36. protected $config;
  37. public function __construct(LoggerInterface $logger,
  38. L10NFactory $l10nFactory,
  39. IURLGenerator $urlGenerator,
  40. IConfig $config) {
  41. $this->logger = $logger;
  42. $this->l10nFactory = $l10nFactory;
  43. $this->urlGenerator = $urlGenerator;
  44. $this->config = $config;
  45. }
  46. /**
  47. * Send notification
  48. *
  49. * @param VEvent $vevent
  50. * @param string|null $calendarDisplayName
  51. * @param string[] $principalEmailAddresses
  52. * @param IUser[] $users
  53. * @return void
  54. */
  55. abstract public function send(VEvent $vevent,
  56. ?string $calendarDisplayName,
  57. array $principalEmailAddresses,
  58. array $users = []): void;
  59. /**
  60. * @return string
  61. */
  62. protected function getFallbackLanguage():string {
  63. if ($this->fallbackLanguage) {
  64. return $this->fallbackLanguage;
  65. }
  66. $fallbackLanguage = $this->l10nFactory->findGenericLanguage();
  67. $this->fallbackLanguage = $fallbackLanguage;
  68. return $fallbackLanguage;
  69. }
  70. /**
  71. * @param string $lang
  72. * @return bool
  73. */
  74. protected function hasL10NForLang(string $lang):bool {
  75. return $this->l10nFactory->languageExists('dav', $lang);
  76. }
  77. /**
  78. * @param string $lang
  79. * @return IL10N
  80. */
  81. protected function getL10NForLang(string $lang):IL10N {
  82. if (isset($this->l10ns[$lang])) {
  83. return $this->l10ns[$lang];
  84. }
  85. $l10n = $this->l10nFactory->get('dav', $lang);
  86. $this->l10ns[$lang] = $l10n;
  87. return $l10n;
  88. }
  89. /**
  90. * @param VEvent $vevent
  91. * @return string
  92. */
  93. private function getStatusOfEvent(VEvent $vevent):string {
  94. if ($vevent->STATUS) {
  95. return (string) $vevent->STATUS;
  96. }
  97. // Doesn't say so in the standard,
  98. // but we consider events without a status
  99. // to be confirmed
  100. return 'CONFIRMED';
  101. }
  102. /**
  103. * @param VEvent $vevent
  104. * @return bool
  105. */
  106. protected function isEventTentative(VEvent $vevent):bool {
  107. return $this->getStatusOfEvent($vevent) === 'TENTATIVE';
  108. }
  109. /**
  110. * @param VEvent $vevent
  111. * @return Property\ICalendar\DateTime
  112. */
  113. protected function getDTEndFromEvent(VEvent $vevent):Property\ICalendar\DateTime {
  114. if (isset($vevent->DTEND)) {
  115. return $vevent->DTEND;
  116. }
  117. if (isset($vevent->DURATION)) {
  118. $isFloating = $vevent->DTSTART->isFloating();
  119. /** @var Property\ICalendar\DateTime $end */
  120. $end = clone $vevent->DTSTART;
  121. $endDateTime = $end->getDateTime();
  122. $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
  123. $end->setDateTime($endDateTime, $isFloating);
  124. return $end;
  125. }
  126. if (!$vevent->DTSTART->hasTime()) {
  127. $isFloating = $vevent->DTSTART->isFloating();
  128. /** @var Property\ICalendar\DateTime $end */
  129. $end = clone $vevent->DTSTART;
  130. $endDateTime = $end->getDateTime();
  131. $endDateTime = $endDateTime->modify('+1 day');
  132. $end->setDateTime($endDateTime, $isFloating);
  133. return $end;
  134. }
  135. return clone $vevent->DTSTART;
  136. }
  137. protected function getCalendarDisplayNameFallback(string $lang): string {
  138. return $this->getL10NForLang($lang)->t('Untitled calendar');
  139. }
  140. }