1
0

Manager.php 3.7 KB

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