BirthdayCalendarController.php 2.3 KB

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