AvailabilitySettings.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Richard Steinmetz <richard@steinmetz.cloud>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. namespace OCA\DAV\Settings;
  25. use OCA\DAV\AppInfo\Application;
  26. use OCA\DAV\Db\AbsenceMapper;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\Http\TemplateResponse;
  29. use OCP\AppFramework\Services\IInitialState;
  30. use OCP\IConfig;
  31. use OCP\Settings\ISettings;
  32. use OCP\User\IAvailabilityCoordinator;
  33. use Psr\Log\LoggerInterface;
  34. class AvailabilitySettings implements ISettings {
  35. protected IConfig $config;
  36. protected IInitialState $initialState;
  37. protected ?string $userId;
  38. public function __construct(IConfig $config,
  39. IInitialState $initialState,
  40. ?string $userId,
  41. private LoggerInterface $logger,
  42. private IAvailabilityCoordinator $coordinator,
  43. private AbsenceMapper $absenceMapper) {
  44. $this->config = $config;
  45. $this->initialState = $initialState;
  46. $this->userId = $userId;
  47. }
  48. public function getForm(): TemplateResponse {
  49. $this->initialState->provideInitialState(
  50. 'user_status_automation',
  51. $this->config->getUserValue(
  52. $this->userId,
  53. 'dav',
  54. 'user_status_automation',
  55. 'no'
  56. )
  57. );
  58. $hideAbsenceSettings = !$this->coordinator->isEnabled();
  59. $this->initialState->provideInitialState('hide_absence_settings', $hideAbsenceSettings);
  60. if (!$hideAbsenceSettings) {
  61. try {
  62. $absence = $this->absenceMapper->findByUserId($this->userId);
  63. $this->initialState->provideInitialState('absence', $absence);
  64. } catch (DoesNotExistException) {
  65. // The user has not yet set up an absence period.
  66. // Logging this error is not necessary.
  67. } catch (\OCP\DB\Exception $e) {
  68. $this->logger->error("Could not find absence data for user $this->userId: " . $e->getMessage(), [
  69. 'exception' => $e,
  70. ]);
  71. }
  72. }
  73. return new TemplateResponse(Application::APP_ID, 'settings-personal-availability');
  74. }
  75. public function getSection(): string {
  76. return 'availability';
  77. }
  78. public function getPriority(): int {
  79. return 10;
  80. }
  81. }