1
0

BirthdayCalendarController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @copyright 2017, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Controller;
  25. use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob;
  26. use OCA\DAV\CalDAV\CalDavBackend;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\JSONResponse;
  29. use OCP\AppFramework\Http\Response;
  30. use OCP\BackgroundJob\IJobList;
  31. use OCP\IConfig;
  32. use OCP\IDBConnection;
  33. use OCP\IRequest;
  34. use OCP\IUser;
  35. use OCP\IUserManager;
  36. class BirthdayCalendarController extends Controller {
  37. /**
  38. * @var IDBConnection
  39. */
  40. protected $db;
  41. /**
  42. * @var IConfig
  43. */
  44. protected $config;
  45. /**
  46. * @var IUserManager
  47. */
  48. protected $userManager;
  49. /**
  50. * @var CalDavBackend
  51. */
  52. protected $caldavBackend;
  53. /**
  54. * @var IJobList
  55. */
  56. protected $jobList;
  57. /**
  58. * BirthdayCalendar constructor.
  59. *
  60. * @param string $appName
  61. * @param IRequest $request
  62. * @param IDBConnection $db
  63. * @param IConfig $config
  64. * @param IJobList $jobList
  65. * @param IUserManager $userManager
  66. * @param CalDavBackend $calDavBackend
  67. */
  68. public function __construct($appName, IRequest $request,
  69. IDBConnection $db, IConfig $config,
  70. IJobList $jobList,
  71. IUserManager $userManager,
  72. CalDavBackend $calDavBackend) {
  73. parent::__construct($appName, $request);
  74. $this->db = $db;
  75. $this->config = $config;
  76. $this->userManager = $userManager;
  77. $this->jobList = $jobList;
  78. $this->caldavBackend = $calDavBackend;
  79. }
  80. /**
  81. * @return Response
  82. * @AuthorizedAdminSetting(settings=OCA\DAV\Settings\CalDAVSettings)
  83. */
  84. public function enable() {
  85. $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'yes');
  86. // add background job for each user
  87. $this->userManager->callForSeenUsers(function (IUser $user) {
  88. $this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [
  89. 'userId' => $user->getUID(),
  90. ]);
  91. });
  92. return new JSONResponse([]);
  93. }
  94. /**
  95. * @return Response
  96. * @AuthorizedAdminSetting(settings=OCA\DAV\Settings\CalDAVSettings)
  97. */
  98. public function disable() {
  99. $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'no');
  100. $this->jobList->remove(GenerateBirthdayCalendarBackgroundJob::class);
  101. $this->caldavBackend->deleteAllBirthdayCalendars();
  102. return new JSONResponse([]);
  103. }
  104. }