ContactsManager.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\CardDAV;
  8. use OCP\Contacts\IManager;
  9. use OCP\IL10N;
  10. use OCP\IURLGenerator;
  11. class ContactsManager {
  12. /**
  13. * ContactsManager constructor.
  14. *
  15. * @param CardDavBackend $backend
  16. * @param IL10N $l10n
  17. */
  18. public function __construct(
  19. private CardDavBackend $backend,
  20. private IL10N $l10n,
  21. ) {
  22. }
  23. /**
  24. * @param IManager $cm
  25. * @param string $userId
  26. * @param IURLGenerator $urlGenerator
  27. */
  28. public function setupContactsProvider(IManager $cm, $userId, IURLGenerator $urlGenerator) {
  29. $addressBooks = $this->backend->getAddressBooksForUser("principals/users/$userId");
  30. $this->register($cm, $addressBooks, $urlGenerator);
  31. $this->setupSystemContactsProvider($cm, $urlGenerator);
  32. }
  33. /**
  34. * @param IManager $cm
  35. * @param IURLGenerator $urlGenerator
  36. */
  37. public function setupSystemContactsProvider(IManager $cm, IURLGenerator $urlGenerator) {
  38. $addressBooks = $this->backend->getAddressBooksForUser('principals/system/system');
  39. $this->register($cm, $addressBooks, $urlGenerator);
  40. }
  41. /**
  42. * @param IManager $cm
  43. * @param $addressBooks
  44. * @param IURLGenerator $urlGenerator
  45. */
  46. private function register(IManager $cm, $addressBooks, $urlGenerator) {
  47. foreach ($addressBooks as $addressBookInfo) {
  48. $addressBook = new AddressBook($this->backend, $addressBookInfo, $this->l10n);
  49. $cm->registerAddressBook(
  50. new AddressBookImpl(
  51. $addressBook,
  52. $addressBookInfo,
  53. $this->backend,
  54. $urlGenerator
  55. )
  56. );
  57. }
  58. }
  59. }