You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.7 KiB

3 years ago
  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 OC\Calendar;
  25. use OCP\Calendar\ICalendar;
  26. class Manager implements \OCP\Calendar\IManager {
  27. /**
  28. * @var ICalendar[] holds all registered calendars
  29. */
  30. private $calendars=[];
  31. /**
  32. * @var \Closure[] to call to load/register calendar providers
  33. */
  34. private $calendarLoaders=[];
  35. /**
  36. * This function is used to search and find objects within the user's calendars.
  37. * In case $pattern is empty all events/journals/todos will be returned.
  38. *
  39. * @param string $pattern which should match within the $searchProperties
  40. * @param array $searchProperties defines the properties within the query pattern should match
  41. * @param array $options - optional parameters:
  42. * ['timerange' => ['start' => new DateTime(...), 'end' => new DateTime(...)]]
  43. * @param integer|null $limit - limit number of search results
  44. * @param integer|null $offset - offset for paging of search results
  45. * @return array an array of events/journals/todos which are arrays of arrays of key-value-pairs
  46. * @since 13.0.0
  47. */
  48. public function search($pattern, array $searchProperties=[], array $options=[], $limit=null, $offset=null) {
  49. $this->loadCalendars();
  50. $result = [];
  51. foreach ($this->calendars as $calendar) {
  52. $r = $calendar->search($pattern, $searchProperties, $options, $limit, $offset);
  53. foreach ($r as $o) {
  54. $o['calendar-key'] = $calendar->getKey();
  55. $result[] = $o;
  56. }
  57. }
  58. return $result;
  59. }
  60. /**
  61. * Check if calendars are available
  62. *
  63. * @return bool true if enabled, false if not
  64. * @since 13.0.0
  65. */
  66. public function isEnabled() {
  67. return !empty($this->calendars) || !empty($this->calendarLoaders);
  68. }
  69. /**
  70. * Registers a calendar
  71. *
  72. * @param ICalendar $calendar
  73. * @return void
  74. * @since 13.0.0
  75. */
  76. public function registerCalendar(ICalendar $calendar) {
  77. $this->calendars[$calendar->getKey()] = $calendar;
  78. }
  79. /**
  80. * Unregisters a calendar
  81. *
  82. * @param ICalendar $calendar
  83. * @return void
  84. * @since 13.0.0
  85. */
  86. public function unregisterCalendar(ICalendar $calendar) {
  87. unset($this->calendars[$calendar->getKey()]);
  88. }
  89. /**
  90. * In order to improve lazy loading a closure can be registered which will be called in case
  91. * calendars are actually requested
  92. *
  93. * @param \Closure $callable
  94. * @return void
  95. * @since 13.0.0
  96. */
  97. public function register(\Closure $callable) {
  98. $this->calendarLoaders[] = $callable;
  99. }
  100. /**
  101. * @return ICalendar[]
  102. * @since 13.0.0
  103. */
  104. public function getCalendars() {
  105. $this->loadCalendars();
  106. return array_values($this->calendars);
  107. }
  108. /**
  109. * removes all registered calendar instances
  110. * @return void
  111. * @since 13.0.0
  112. */
  113. public function clear() {
  114. $this->calendars = [];
  115. $this->calendarLoaders = [];
  116. }
  117. /**
  118. * loads all calendars
  119. */
  120. private function loadCalendars() {
  121. foreach ($this->calendarLoaders as $callable) {
  122. $callable($this);
  123. }
  124. $this->calendarLoaders = [];
  125. }
  126. }