Base.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\IUserManager;
  33. abstract class Base implements IProvider {
  34. /** @var IUserManager */
  35. protected $userManager;
  36. /** @var IGroupManager */
  37. protected $groupManager;
  38. /** @var string[] */
  39. protected $groupDisplayNames = [];
  40. /** @var IURLGenerator */
  41. protected $url;
  42. /**
  43. * @param IUserManager $userManager
  44. * @param IGroupManager $groupManager
  45. * @param IURLGenerator $urlGenerator
  46. */
  47. public function __construct(IUserManager $userManager, IGroupManager $groupManager, IURLGenerator $urlGenerator) {
  48. $this->userManager = $userManager;
  49. $this->groupManager = $groupManager;
  50. $this->url = $urlGenerator;
  51. }
  52. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  53. $event->setRichSubject($subject, $parameters);
  54. }
  55. /**
  56. * @param array $data
  57. * @param IL10N $l
  58. * @return array
  59. */
  60. protected function generateCalendarParameter($data, IL10N $l) {
  61. if ($data['uri'] === CalDavBackend::PERSONAL_CALENDAR_URI &&
  62. $data['name'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
  63. return [
  64. 'type' => 'calendar',
  65. 'id' => $data['id'],
  66. 'name' => $l->t('Personal'),
  67. ];
  68. }
  69. return [
  70. 'type' => 'calendar',
  71. 'id' => $data['id'],
  72. 'name' => $data['name'],
  73. ];
  74. }
  75. /**
  76. * @param int $id
  77. * @param string $name
  78. * @return array
  79. */
  80. protected function generateLegacyCalendarParameter($id, $name) {
  81. return [
  82. 'type' => 'calendar',
  83. 'id' => $id,
  84. 'name' => $name,
  85. ];
  86. }
  87. protected function generateUserParameter(string $uid): array {
  88. return [
  89. 'type' => 'user',
  90. 'id' => $uid,
  91. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  92. ];
  93. }
  94. /**
  95. * @param string $gid
  96. * @return array
  97. */
  98. protected function generateGroupParameter($gid) {
  99. if (!isset($this->groupDisplayNames[$gid])) {
  100. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  101. }
  102. return [
  103. 'type' => 'user-group',
  104. 'id' => $gid,
  105. 'name' => $this->groupDisplayNames[$gid],
  106. ];
  107. }
  108. /**
  109. * @param string $gid
  110. * @return string
  111. */
  112. protected function getGroupDisplayName($gid) {
  113. $group = $this->groupManager->get($gid);
  114. if ($group instanceof IGroup) {
  115. return $group->getDisplayName();
  116. }
  117. return $gid;
  118. }
  119. }