BirthdayCalendarController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Controller;
  7. use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCA\DAV\Settings\CalDAVSettings;
  10. use OCP\AppFramework\Controller;
  11. use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
  12. use OCP\AppFramework\Http\JSONResponse;
  13. use OCP\AppFramework\Http\Response;
  14. use OCP\BackgroundJob\IJobList;
  15. use OCP\IConfig;
  16. use OCP\IDBConnection;
  17. use OCP\IRequest;
  18. use OCP\IUser;
  19. use OCP\IUserManager;
  20. class BirthdayCalendarController extends Controller {
  21. /**
  22. * @var IDBConnection
  23. */
  24. protected $db;
  25. /**
  26. * @var IConfig
  27. */
  28. protected $config;
  29. /**
  30. * @var IUserManager
  31. */
  32. protected $userManager;
  33. /**
  34. * @var CalDavBackend
  35. */
  36. protected $caldavBackend;
  37. /**
  38. * @var IJobList
  39. */
  40. protected $jobList;
  41. /**
  42. * BirthdayCalendar constructor.
  43. *
  44. * @param string $appName
  45. * @param IRequest $request
  46. * @param IDBConnection $db
  47. * @param IConfig $config
  48. * @param IJobList $jobList
  49. * @param IUserManager $userManager
  50. * @param CalDavBackend $calDavBackend
  51. */
  52. public function __construct($appName, IRequest $request,
  53. IDBConnection $db, IConfig $config,
  54. IJobList $jobList,
  55. IUserManager $userManager,
  56. CalDavBackend $calDavBackend) {
  57. parent::__construct($appName, $request);
  58. $this->db = $db;
  59. $this->config = $config;
  60. $this->userManager = $userManager;
  61. $this->jobList = $jobList;
  62. $this->caldavBackend = $calDavBackend;
  63. }
  64. /**
  65. * @return Response
  66. */
  67. #[AuthorizedAdminSetting(settings: CalDAVSettings::class)]
  68. public function enable() {
  69. $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'yes');
  70. // add background job for each user
  71. $this->userManager->callForSeenUsers(function (IUser $user) {
  72. $this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [
  73. 'userId' => $user->getUID(),
  74. ]);
  75. });
  76. return new JSONResponse([]);
  77. }
  78. /**
  79. * @return Response
  80. */
  81. #[AuthorizedAdminSetting(settings: CalDAVSettings::class)]
  82. public function disable() {
  83. $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'no');
  84. $this->jobList->remove(GenerateBirthdayCalendarBackgroundJob::class);
  85. $this->caldavBackend->deleteAllBirthdayCalendars();
  86. return new JSONResponse([]);
  87. }
  88. }