1
0

AvailabilitySettings.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. protected IConfig $config;
  19. protected IInitialState $initialState;
  20. protected ?string $userId;
  21. public function __construct(IConfig $config,
  22. IInitialState $initialState,
  23. ?string $userId,
  24. private LoggerInterface $logger,
  25. private IAvailabilityCoordinator $coordinator,
  26. private AbsenceMapper $absenceMapper) {
  27. $this->config = $config;
  28. $this->initialState = $initialState;
  29. $this->userId = $userId;
  30. }
  31. public function getForm(): TemplateResponse {
  32. $this->initialState->provideInitialState(
  33. 'user_status_automation',
  34. $this->config->getUserValue(
  35. $this->userId,
  36. 'dav',
  37. 'user_status_automation',
  38. 'no'
  39. )
  40. );
  41. $hideAbsenceSettings = !$this->coordinator->isEnabled();
  42. $this->initialState->provideInitialState('hide_absence_settings', $hideAbsenceSettings);
  43. if (!$hideAbsenceSettings) {
  44. try {
  45. $absence = $this->absenceMapper->findByUserId($this->userId);
  46. $this->initialState->provideInitialState('absence', $absence);
  47. } catch (DoesNotExistException) {
  48. // The user has not yet set up an absence period.
  49. // Logging this error is not necessary.
  50. } catch (\OCP\DB\Exception $e) {
  51. $this->logger->error("Could not find absence data for user $this->userId: " . $e->getMessage(), [
  52. 'exception' => $e,
  53. ]);
  54. }
  55. }
  56. return new TemplateResponse(Application::APP_ID, 'settings-personal-availability');
  57. }
  58. public function getSection(): string {
  59. return 'availability';
  60. }
  61. public function getPriority(): int {
  62. return 10;
  63. }
  64. }