BirthdayCalendarController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * BirthdayCalendar constructor.
  23. *
  24. * @param string $appName
  25. * @param IRequest $request
  26. * @param IDBConnection $db
  27. * @param IConfig $config
  28. * @param IJobList $jobList
  29. * @param IUserManager $userManager
  30. * @param CalDavBackend $caldavBackend
  31. */
  32. public function __construct(
  33. $appName,
  34. IRequest $request,
  35. protected IDBConnection $db,
  36. protected IConfig $config,
  37. protected IJobList $jobList,
  38. protected IUserManager $userManager,
  39. protected CalDavBackend $caldavBackend,
  40. ) {
  41. parent::__construct($appName, $request);
  42. }
  43. /**
  44. * @return Response
  45. */
  46. #[AuthorizedAdminSetting(settings: CalDAVSettings::class)]
  47. public function enable() {
  48. $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'yes');
  49. // add background job for each user
  50. $this->userManager->callForSeenUsers(function (IUser $user): void {
  51. $this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [
  52. 'userId' => $user->getUID(),
  53. ]);
  54. });
  55. return new JSONResponse([]);
  56. }
  57. /**
  58. * @return Response
  59. */
  60. #[AuthorizedAdminSetting(settings: CalDAVSettings::class)]
  61. public function disable() {
  62. $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'no');
  63. $this->jobList->remove(GenerateBirthdayCalendarBackgroundJob::class);
  64. $this->caldavBackend->deleteAllBirthdayCalendars();
  65. return new JSONResponse([]);
  66. }
  67. }