BirthdayCalendarController.php 2.8 KB

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