AbstractProvider.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /** @var IL10N[] */
  27. private $l10ns;
  28. /** @var string */
  29. private $fallbackLanguage;
  30. public function __construct(
  31. protected LoggerInterface $logger,
  32. protected L10NFactory $l10nFactory,
  33. protected IURLGenerator $urlGenerator,
  34. protected IConfig $config,
  35. ) {
  36. }
  37. /**
  38. * Send notification
  39. *
  40. * @param VEvent $vevent
  41. * @param string|null $calendarDisplayName
  42. * @param string[] $principalEmailAddresses
  43. * @param IUser[] $users
  44. * @return void
  45. */
  46. abstract public function send(VEvent $vevent,
  47. ?string $calendarDisplayName,
  48. array $principalEmailAddresses,
  49. array $users = []): void;
  50. /**
  51. * @return string
  52. */
  53. protected function getFallbackLanguage():string {
  54. if ($this->fallbackLanguage) {
  55. return $this->fallbackLanguage;
  56. }
  57. $fallbackLanguage = $this->l10nFactory->findGenericLanguage();
  58. $this->fallbackLanguage = $fallbackLanguage;
  59. return $fallbackLanguage;
  60. }
  61. /**
  62. * @param string $lang
  63. * @return bool
  64. */
  65. protected function hasL10NForLang(string $lang):bool {
  66. return $this->l10nFactory->languageExists('dav', $lang);
  67. }
  68. /**
  69. * @param string $lang
  70. * @return IL10N
  71. */
  72. protected function getL10NForLang(string $lang):IL10N {
  73. if (isset($this->l10ns[$lang])) {
  74. return $this->l10ns[$lang];
  75. }
  76. $l10n = $this->l10nFactory->get('dav', $lang);
  77. $this->l10ns[$lang] = $l10n;
  78. return $l10n;
  79. }
  80. /**
  81. * @param VEvent $vevent
  82. * @return string
  83. */
  84. private function getStatusOfEvent(VEvent $vevent):string {
  85. if ($vevent->STATUS) {
  86. return (string)$vevent->STATUS;
  87. }
  88. // Doesn't say so in the standard,
  89. // but we consider events without a status
  90. // to be confirmed
  91. return 'CONFIRMED';
  92. }
  93. /**
  94. * @param VEvent $vevent
  95. * @return bool
  96. */
  97. protected function isEventTentative(VEvent $vevent):bool {
  98. return $this->getStatusOfEvent($vevent) === 'TENTATIVE';
  99. }
  100. /**
  101. * @param VEvent $vevent
  102. * @return Property\ICalendar\DateTime
  103. */
  104. protected function getDTEndFromEvent(VEvent $vevent):Property\ICalendar\DateTime {
  105. if (isset($vevent->DTEND)) {
  106. return $vevent->DTEND;
  107. }
  108. if (isset($vevent->DURATION)) {
  109. $isFloating = $vevent->DTSTART->isFloating();
  110. /** @var Property\ICalendar\DateTime $end */
  111. $end = clone $vevent->DTSTART;
  112. $endDateTime = $end->getDateTime();
  113. $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
  114. $end->setDateTime($endDateTime, $isFloating);
  115. return $end;
  116. }
  117. if (!$vevent->DTSTART->hasTime()) {
  118. $isFloating = $vevent->DTSTART->isFloating();
  119. /** @var Property\ICalendar\DateTime $end */
  120. $end = clone $vevent->DTSTART;
  121. $endDateTime = $end->getDateTime();
  122. $endDateTime = $endDateTime->modify('+1 day');
  123. $end->setDateTime($endDateTime, $isFloating);
  124. return $end;
  125. }
  126. return clone $vevent->DTSTART;
  127. }
  128. protected function getCalendarDisplayNameFallback(string $lang): string {
  129. return $this->getL10NForLang($lang)->t('Untitled calendar');
  130. }
  131. }