LocalTimeProvider.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Contacts\ContactsMenu\Providers;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\Contacts\ContactsMenu\IActionFactory;
  10. use OCP\Contacts\ContactsMenu\IEntry;
  11. use OCP\Contacts\ContactsMenu\IProvider;
  12. use OCP\IConfig;
  13. use OCP\IDateTimeFormatter;
  14. use OCP\IURLGenerator;
  15. use OCP\IUserManager;
  16. use OCP\L10N\IFactory as IL10NFactory;
  17. class LocalTimeProvider implements IProvider {
  18. public function __construct(
  19. private IActionFactory $actionFactory,
  20. private IL10NFactory $l10nFactory,
  21. private IURLGenerator $urlGenerator,
  22. private IUserManager $userManager,
  23. private ITimeFactory $timeFactory,
  24. private IDateTimeFormatter $dateTimeFormatter,
  25. private IConfig $config,
  26. ) {
  27. }
  28. public function process(IEntry $entry): void {
  29. $targetUserId = $entry->getProperty('UID');
  30. $targetUser = $this->userManager->get($targetUserId);
  31. if (!empty($targetUser)) {
  32. $timezone = $this->config->getUserValue($targetUser->getUID(), 'core', 'timezone') ?: $this->config->getSystemValueString('default_timezone', 'UTC');
  33. $dateTimeZone = new \DateTimeZone($timezone);
  34. $localTime = $this->dateTimeFormatter->formatTime($this->timeFactory->getDateTime(), 'short', $dateTimeZone);
  35. $iconUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/recent.svg'));
  36. $l = $this->l10nFactory->get('lib');
  37. $profileActionText = $l->t('Local time: %s', [$localTime]);
  38. $action = $this->actionFactory->newLinkAction($iconUrl, $profileActionText, '#', 'timezone');
  39. // Order after the profile page
  40. $action->setPriority(19);
  41. $entry->addAction($action);
  42. }
  43. }
  44. }