DashboardService.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Dashboard\Service;
  8. use JsonException;
  9. use OCP\Accounts\IAccountManager;
  10. use OCP\Accounts\PropertyDoesNotExistException;
  11. use OCP\IConfig;
  12. use OCP\IUserManager;
  13. class DashboardService {
  14. public function __construct(
  15. private IConfig $config,
  16. private ?string $userId,
  17. private IUserManager $userManager,
  18. private IAccountManager $accountManager,
  19. ) {
  20. }
  21. /**
  22. * @return list<string>
  23. */
  24. public function getLayout(): array {
  25. $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
  26. return array_values(array_filter(explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)), fn (string $value) => $value !== ''));
  27. }
  28. /**
  29. * @return list<string>
  30. */
  31. public function getStatuses() {
  32. $configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', '');
  33. try {
  34. // Parse the old format
  35. /** @var array<string, bool> $statuses */
  36. $statuses = json_decode($configStatuses, true, 512, JSON_THROW_ON_ERROR);
  37. // We avoid getting an empty array as it will not produce an object in UI's JS
  38. return array_keys(array_filter($statuses, static fn (bool $value) => $value));
  39. } catch (JsonException $e) {
  40. return array_values(array_filter(explode(',', $configStatuses), fn (string $value) => $value !== ''));
  41. }
  42. }
  43. public function getBirthdate(): string {
  44. if ($this->userId === null) {
  45. return '';
  46. }
  47. $user = $this->userManager->get($this->userId);
  48. if ($user === null) {
  49. return '';
  50. }
  51. $account = $this->accountManager->getAccount($user);
  52. try {
  53. $birthdate = $account->getProperty(IAccountManager::PROPERTY_BIRTHDATE);
  54. } catch (PropertyDoesNotExistException) {
  55. return '';
  56. }
  57. return $birthdate->getValue();
  58. }
  59. }