You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractProvider.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Thomas Citharel
  5. * @copyright Copyright (c) 2019, Georg Ehrke
  6. *
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\DAV\CalDAV\Reminder\NotificationProvider;
  29. use OCA\DAV\CalDAV\Reminder\INotificationProvider;
  30. use OCP\IConfig;
  31. use OCP\IL10N;
  32. use OCP\ILogger;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\L10N\IFactory as L10NFactory;
  36. use Sabre\VObject\Component\VEvent;
  37. use Sabre\VObject\DateTimeParser;
  38. use Sabre\VObject\Property;
  39. /**
  40. * Class AbstractProvider
  41. *
  42. * @package OCA\DAV\CalDAV\Reminder\NotificationProvider
  43. */
  44. abstract class AbstractProvider implements INotificationProvider {
  45. /** @var string */
  46. public const NOTIFICATION_TYPE = '';
  47. /** @var ILogger */
  48. protected $logger;
  49. /** @var L10NFactory */
  50. protected $l10nFactory;
  51. /** @var IL10N[] */
  52. private $l10ns;
  53. /** @var string */
  54. private $fallbackLanguage;
  55. /** @var IURLGenerator */
  56. protected $urlGenerator;
  57. /** @var IConfig */
  58. protected $config;
  59. /**
  60. * @param ILogger $logger
  61. * @param L10NFactory $l10nFactory
  62. * @param IConfig $config
  63. * @param IUrlGenerator $urlGenerator
  64. */
  65. public function __construct(ILogger $logger,
  66. L10NFactory $l10nFactory,
  67. IURLGenerator $urlGenerator,
  68. IConfig $config) {
  69. $this->logger = $logger;
  70. $this->l10nFactory = $l10nFactory;
  71. $this->urlGenerator = $urlGenerator;
  72. $this->config = $config;
  73. }
  74. /**
  75. * Send notification
  76. *
  77. * @param VEvent $vevent
  78. * @param string $calendarDisplayName
  79. * @param IUser[] $users
  80. * @return void
  81. */
  82. abstract public function send(VEvent $vevent,
  83. string $calendarDisplayName,
  84. array $users = []): void;
  85. /**
  86. * @return string
  87. */
  88. protected function getFallbackLanguage():string {
  89. if ($this->fallbackLanguage) {
  90. return $this->fallbackLanguage;
  91. }
  92. $fallbackLanguage = $this->l10nFactory->findLanguage();
  93. $this->fallbackLanguage = $fallbackLanguage;
  94. return $fallbackLanguage;
  95. }
  96. /**
  97. * @param string $lang
  98. * @return bool
  99. */
  100. protected function hasL10NForLang(string $lang):bool {
  101. return $this->l10nFactory->languageExists('dav', $lang);
  102. }
  103. /**
  104. * @param string $lang
  105. * @return IL10N
  106. */
  107. protected function getL10NForLang(string $lang):IL10N {
  108. if (isset($this->l10ns[$lang])) {
  109. return $this->l10ns[$lang];
  110. }
  111. $l10n = $this->l10nFactory->get('dav', $lang);
  112. $this->l10ns[$lang] = $l10n;
  113. return $l10n;
  114. }
  115. /**
  116. * @param VEvent $vevent
  117. * @return string
  118. */
  119. private function getStatusOfEvent(VEvent $vevent):string {
  120. if ($vevent->STATUS) {
  121. return (string) $vevent->STATUS;
  122. }
  123. // Doesn't say so in the standard,
  124. // but we consider events without a status
  125. // to be confirmed
  126. return 'CONFIRMED';
  127. }
  128. /**
  129. * @param VEvent $vevent
  130. * @return bool
  131. */
  132. protected function isEventTentative(VEvent $vevent):bool {
  133. return $this->getStatusOfEvent($vevent) === 'TENTATIVE';
  134. }
  135. /**
  136. * @param VEvent $vevent
  137. * @return Property\ICalendar\DateTime
  138. */
  139. protected function getDTEndFromEvent(VEvent $vevent):Property\ICalendar\DateTime {
  140. if (isset($vevent->DTEND)) {
  141. return $vevent->DTEND;
  142. }
  143. if (isset($vevent->DURATION)) {
  144. $isFloating = $vevent->DTSTART->isFloating();
  145. /** @var Property\ICalendar\DateTime $end */
  146. $end = clone $vevent->DTSTART;
  147. $endDateTime = $end->getDateTime();
  148. $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
  149. $end->setDateTime($endDateTime, $isFloating);
  150. return $end;
  151. }
  152. if (!$vevent->DTSTART->hasTime()) {
  153. $isFloating = $vevent->DTSTART->isFloating();
  154. /** @var Property\ICalendar\DateTime $end */
  155. $end = clone $vevent->DTSTART;
  156. $endDateTime = $end->getDateTime();
  157. $endDateTime = $endDateTime->modify('+1 day');
  158. $end->setDateTime($endDateTime, $isFloating);
  159. return $end;
  160. }
  161. return clone $vevent->DTSTART;
  162. }
  163. }