Base.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\CalDAV\Activity\Provider;
  7. use OCA\DAV\CalDAV\CalDavBackend;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IProvider;
  10. use OCP\IGroup;
  11. use OCP\IGroupManager;
  12. use OCP\IL10N;
  13. use OCP\IURLGenerator;
  14. use OCP\IUserManager;
  15. abstract class Base implements IProvider {
  16. /** @var IUserManager */
  17. protected $userManager;
  18. /** @var IGroupManager */
  19. protected $groupManager;
  20. /** @var string[] */
  21. protected $groupDisplayNames = [];
  22. /** @var IURLGenerator */
  23. protected $url;
  24. /**
  25. * @param IUserManager $userManager
  26. * @param IGroupManager $groupManager
  27. * @param IURLGenerator $urlGenerator
  28. */
  29. public function __construct(IUserManager $userManager, IGroupManager $groupManager, IURLGenerator $urlGenerator) {
  30. $this->userManager = $userManager;
  31. $this->groupManager = $groupManager;
  32. $this->url = $urlGenerator;
  33. }
  34. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  35. $event->setRichSubject($subject, $parameters);
  36. }
  37. /**
  38. * @param array $data
  39. * @param IL10N $l
  40. * @return array
  41. */
  42. protected function generateCalendarParameter($data, IL10N $l) {
  43. if ($data['uri'] === CalDavBackend::PERSONAL_CALENDAR_URI &&
  44. $data['name'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
  45. return [
  46. 'type' => 'calendar',
  47. 'id' => $data['id'],
  48. 'name' => $l->t('Personal'),
  49. ];
  50. }
  51. return [
  52. 'type' => 'calendar',
  53. 'id' => $data['id'],
  54. 'name' => $data['name'],
  55. ];
  56. }
  57. /**
  58. * @param int $id
  59. * @param string $name
  60. * @return array
  61. */
  62. protected function generateLegacyCalendarParameter($id, $name) {
  63. return [
  64. 'type' => 'calendar',
  65. 'id' => $id,
  66. 'name' => $name,
  67. ];
  68. }
  69. protected function generateUserParameter(string $uid): array {
  70. return [
  71. 'type' => 'user',
  72. 'id' => $uid,
  73. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  74. ];
  75. }
  76. /**
  77. * @param string $gid
  78. * @return array
  79. */
  80. protected function generateGroupParameter($gid) {
  81. if (!isset($this->groupDisplayNames[$gid])) {
  82. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  83. }
  84. return [
  85. 'type' => 'user-group',
  86. 'id' => $gid,
  87. 'name' => $this->groupDisplayNames[$gid],
  88. ];
  89. }
  90. /**
  91. * @param string $gid
  92. * @return string
  93. */
  94. protected function getGroupDisplayName($gid) {
  95. $group = $this->groupManager->get($gid);
  96. if ($group instanceof IGroup) {
  97. return $group->getDisplayName();
  98. }
  99. return $gid;
  100. }
  101. }