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.

Base.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Thomas Citharel <nextcloud@tcit.fr>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV\Activity\Provider;
  25. use OCA\DAV\CalDAV\CalDavBackend;
  26. use OCP\Activity\IEvent;
  27. use OCP\Activity\IProvider;
  28. use OCP\IGroup;
  29. use OCP\IGroupManager;
  30. use OCP\IL10N;
  31. use OCP\IURLGenerator;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. abstract class Base implements IProvider {
  35. /** @var IUserManager */
  36. protected $userManager;
  37. /** @var string[] */
  38. protected $userDisplayNames = [];
  39. /** @var IGroupManager */
  40. protected $groupManager;
  41. /** @var string[] */
  42. protected $groupDisplayNames = [];
  43. /** @var IURLGenerator */
  44. protected $url;
  45. /**
  46. * @param IUserManager $userManager
  47. * @param IGroupManager $groupManager
  48. * @param IURLGenerator $urlGenerator
  49. */
  50. public function __construct(IUserManager $userManager, IGroupManager $groupManager, IURLGenerator $urlGenerator) {
  51. $this->userManager = $userManager;
  52. $this->groupManager = $groupManager;
  53. $this->url = $urlGenerator;
  54. }
  55. /**
  56. * @param IEvent $event
  57. * @param string $subject
  58. * @param array $parameters
  59. */
  60. protected function setSubjects(IEvent $event, $subject, array $parameters) {
  61. $placeholders = $replacements = [];
  62. foreach ($parameters as $placeholder => $parameter) {
  63. $placeholders[] = '{' . $placeholder . '}';
  64. $replacements[] = $parameter['name'];
  65. }
  66. $event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
  67. ->setRichSubject($subject, $parameters);
  68. }
  69. /**
  70. * @param array $data
  71. * @param IL10N $l
  72. * @return array
  73. */
  74. protected function generateCalendarParameter($data, IL10N $l) {
  75. if ($data['uri'] === CalDavBackend::PERSONAL_CALENDAR_URI &&
  76. $data['name'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
  77. return [
  78. 'type' => 'calendar',
  79. 'id' => $data['id'],
  80. 'name' => $l->t('Personal'),
  81. ];
  82. }
  83. return [
  84. 'type' => 'calendar',
  85. 'id' => $data['id'],
  86. 'name' => $data['name'],
  87. ];
  88. }
  89. /**
  90. * @param int $id
  91. * @param string $name
  92. * @return array
  93. */
  94. protected function generateLegacyCalendarParameter($id, $name) {
  95. return [
  96. 'type' => 'calendar',
  97. 'id' => $id,
  98. 'name' => $name,
  99. ];
  100. }
  101. /**
  102. * @param string $uid
  103. * @return array
  104. */
  105. protected function generateUserParameter($uid) {
  106. if (!isset($this->userDisplayNames[$uid])) {
  107. $this->userDisplayNames[$uid] = $this->getUserDisplayName($uid);
  108. }
  109. return [
  110. 'type' => 'user',
  111. 'id' => $uid,
  112. 'name' => $this->userDisplayNames[$uid],
  113. ];
  114. }
  115. /**
  116. * @param string $uid
  117. * @return string
  118. */
  119. protected function getUserDisplayName($uid) {
  120. $user = $this->userManager->get($uid);
  121. if ($user instanceof IUser) {
  122. return $user->getDisplayName();
  123. }
  124. return $uid;
  125. }
  126. /**
  127. * @param string $gid
  128. * @return array
  129. */
  130. protected function generateGroupParameter($gid) {
  131. if (!isset($this->groupDisplayNames[$gid])) {
  132. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  133. }
  134. return [
  135. 'type' => 'user-group',
  136. 'id' => $gid,
  137. 'name' => $this->groupDisplayNames[$gid],
  138. ];
  139. }
  140. /**
  141. * @param string $gid
  142. * @return string
  143. */
  144. protected function getGroupDisplayName($gid) {
  145. $group = $this->groupManager->get($gid);
  146. if ($group instanceof IGroup) {
  147. return $group->getDisplayName();
  148. }
  149. return $gid;
  150. }
  151. }