ContactsManager.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. /** @var CardDavBackend */
  13. private $backend;
  14. /** @var IL10N */
  15. private $l10n;
  16. /**
  17. * ContactsManager constructor.
  18. *
  19. * @param CardDavBackend $backend
  20. * @param IL10N $l10n
  21. */
  22. public function __construct(CardDavBackend $backend, IL10N $l10n) {
  23. $this->backend = $backend;
  24. $this->l10n = $l10n;
  25. }
  26. /**
  27. * @param IManager $cm
  28. * @param string $userId
  29. * @param IURLGenerator $urlGenerator
  30. */
  31. public function setupContactsProvider(IManager $cm, $userId, IURLGenerator $urlGenerator) {
  32. $addressBooks = $this->backend->getAddressBooksForUser("principals/users/$userId");
  33. $this->register($cm, $addressBooks, $urlGenerator);
  34. $this->setupSystemContactsProvider($cm, $urlGenerator);
  35. }
  36. /**
  37. * @param IManager $cm
  38. * @param IURLGenerator $urlGenerator
  39. */
  40. public function setupSystemContactsProvider(IManager $cm, IURLGenerator $urlGenerator) {
  41. $addressBooks = $this->backend->getAddressBooksForUser('principals/system/system');
  42. $this->register($cm, $addressBooks, $urlGenerator);
  43. }
  44. /**
  45. * @param IManager $cm
  46. * @param $addressBooks
  47. * @param IURLGenerator $urlGenerator
  48. */
  49. private function register(IManager $cm, $addressBooks, $urlGenerator) {
  50. foreach ($addressBooks as $addressBookInfo) {
  51. $addressBook = new \OCA\DAV\CardDAV\AddressBook($this->backend, $addressBookInfo, $this->l10n);
  52. $cm->registerAddressBook(
  53. new AddressBookImpl(
  54. $addressBook,
  55. $addressBookInfo,
  56. $this->backend,
  57. $urlGenerator
  58. )
  59. );
  60. }
  61. }
  62. }