BirthdayCalendarController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @copyright 2017, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  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. */
  83. public function enable() {
  84. $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'yes');
  85. // add background job for each user
  86. $this->userManager->callForSeenUsers(function(IUser $user) {
  87. $this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [
  88. 'userId' => $user->getUID(),
  89. ]);
  90. });
  91. return new JSONResponse([]);
  92. }
  93. /**
  94. * @return Response
  95. */
  96. public function disable() {
  97. $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'no');
  98. $this->jobList->remove(GenerateBirthdayCalendarBackgroundJob::class);
  99. $this->caldavBackend->deleteAllBirthdayCalendars();
  100. return new JSONResponse([]);
  101. }
  102. }