AvailabilitySettings.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Settings;
  8. use OCA\DAV\AppInfo\Application;
  9. use OCA\DAV\Db\AbsenceMapper;
  10. use OCP\AppFramework\Db\DoesNotExistException;
  11. use OCP\AppFramework\Http\TemplateResponse;
  12. use OCP\AppFramework\Services\IInitialState;
  13. use OCP\IConfig;
  14. use OCP\Settings\ISettings;
  15. use OCP\User\IAvailabilityCoordinator;
  16. use Psr\Log\LoggerInterface;
  17. class AvailabilitySettings implements ISettings {
  18. public function __construct(
  19. protected IConfig $config,
  20. protected IInitialState $initialState,
  21. protected ?string $userId,
  22. private LoggerInterface $logger,
  23. private IAvailabilityCoordinator $coordinator,
  24. private AbsenceMapper $absenceMapper,
  25. ) {
  26. }
  27. public function getForm(): TemplateResponse {
  28. $this->initialState->provideInitialState(
  29. 'user_status_automation',
  30. $this->config->getUserValue(
  31. $this->userId,
  32. 'dav',
  33. 'user_status_automation',
  34. 'no'
  35. )
  36. );
  37. $hideAbsenceSettings = !$this->coordinator->isEnabled();
  38. $this->initialState->provideInitialState('hide_absence_settings', $hideAbsenceSettings);
  39. if (!$hideAbsenceSettings) {
  40. try {
  41. $absence = $this->absenceMapper->findByUserId($this->userId);
  42. $this->initialState->provideInitialState('absence', $absence);
  43. } catch (DoesNotExistException) {
  44. // The user has not yet set up an absence period.
  45. // Logging this error is not necessary.
  46. } catch (\OCP\DB\Exception $e) {
  47. $this->logger->error("Could not find absence data for user $this->userId: " . $e->getMessage(), [
  48. 'exception' => $e,
  49. ]);
  50. }
  51. }
  52. return new TemplateResponse(Application::APP_ID, 'settings-personal-availability');
  53. }
  54. public function getSection(): string {
  55. return 'availability';
  56. }
  57. public function getPriority(): int {
  58. return 10;
  59. }
  60. }