ContactsManager.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Tobia De Koninck <tobia@ledfan.be>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC;
  28. use OCP\Constants;
  29. use OCP\Contacts\IManager;
  30. use OCP\IAddressBook;
  31. class ContactsManager implements IManager {
  32. /**
  33. * This function is used to search and find contacts within the users address books.
  34. * In case $pattern is empty all contacts will be returned.
  35. *
  36. * @param string $pattern which should match within the $searchProperties
  37. * @param array $searchProperties defines the properties within the query pattern should match
  38. * @param array $options = array() to define the search behavior
  39. * - 'escape_like_param' - If set to false wildcards _ and % are not escaped
  40. * - 'limit' - Set a numeric limit for the search results
  41. * - 'offset' - Set the offset for the limited search results
  42. * @return array an array of contacts which are arrays of key-value-pairs
  43. */
  44. public function search($pattern, $searchProperties = [], $options = []) {
  45. $this->loadAddressBooks();
  46. $result = [];
  47. foreach ($this->addressBooks as $addressBook) {
  48. $r = $addressBook->search($pattern, $searchProperties, $options);
  49. $contacts = [];
  50. foreach ($r as $c) {
  51. $c['addressbook-key'] = $addressBook->getKey();
  52. $contacts[] = $c;
  53. }
  54. $result = array_merge($result, $contacts);
  55. }
  56. return $result;
  57. }
  58. /**
  59. * This function can be used to delete the contact identified by the given id
  60. *
  61. * @param object $id the unique identifier to a contact
  62. * @param string $address_book_key identifier of the address book in which the contact shall be deleted
  63. * @return bool successful or not
  64. */
  65. public function delete($id, $address_book_key) {
  66. $addressBook = $this->getAddressBook($address_book_key);
  67. if (!$addressBook) {
  68. return null;
  69. }
  70. if ($addressBook->getPermissions() & Constants::PERMISSION_DELETE) {
  71. return $addressBook->delete($id);
  72. }
  73. return null;
  74. }
  75. /**
  76. * This function is used to create a new contact if 'id' is not given or not present.
  77. * Otherwise the contact will be updated by replacing the entire data set.
  78. *
  79. * @param array $properties this array if key-value-pairs defines a contact
  80. * @param string $address_book_key identifier of the address book in which the contact shall be created or updated
  81. * @return array representing the contact just created or updated
  82. */
  83. public function createOrUpdate($properties, $address_book_key) {
  84. $addressBook = $this->getAddressBook($address_book_key);
  85. if (!$addressBook) {
  86. return null;
  87. }
  88. if ($addressBook->getPermissions() & Constants::PERMISSION_CREATE) {
  89. return $addressBook->createOrUpdate($properties);
  90. }
  91. return null;
  92. }
  93. /**
  94. * Check if contacts are available (e.g. contacts app enabled)
  95. *
  96. * @return bool true if enabled, false if not
  97. */
  98. public function isEnabled() {
  99. return !empty($this->addressBooks) || !empty($this->addressBookLoaders);
  100. }
  101. /**
  102. * @param IAddressBook $addressBook
  103. */
  104. public function registerAddressBook(IAddressBook $addressBook) {
  105. $this->addressBooks[$addressBook->getKey()] = $addressBook;
  106. }
  107. /**
  108. * @param IAddressBook $addressBook
  109. */
  110. public function unregisterAddressBook(IAddressBook $addressBook) {
  111. unset($this->addressBooks[$addressBook->getKey()]);
  112. }
  113. /**
  114. * Return a list of the user's addressbooks display names
  115. * ! The addressBook displayName are not unique, please use getUserAddressBooks
  116. *
  117. * @return IAddressBook[]
  118. * @since 6.0.0
  119. * @deprecated 16.0.0 - Use `$this->getUserAddressBooks()` instead
  120. */
  121. public function getAddressBooks() {
  122. $this->loadAddressBooks();
  123. $result = [];
  124. foreach ($this->addressBooks as $addressBook) {
  125. $result[$addressBook->getKey()] = $addressBook->getDisplayName();
  126. }
  127. return $result;
  128. }
  129. /**
  130. * Return a list of the user's addressbooks
  131. *
  132. * @return IAddressBook[]
  133. * @since 16.0.0
  134. */
  135. public function getUserAddressBooks(): array {
  136. $this->loadAddressBooks();
  137. return $this->addressBooks;
  138. }
  139. /**
  140. * removes all registered address book instances
  141. */
  142. public function clear() {
  143. $this->addressBooks = [];
  144. $this->addressBookLoaders = [];
  145. }
  146. /**
  147. * @var IAddressBook[] which holds all registered address books
  148. */
  149. private $addressBooks = [];
  150. /**
  151. * @var \Closure[] to call to load/register address books
  152. */
  153. private $addressBookLoaders = [];
  154. /**
  155. * In order to improve lazy loading a closure can be registered which will be called in case
  156. * address books are actually requested
  157. *
  158. * @param \Closure $callable
  159. */
  160. public function register(\Closure $callable) {
  161. $this->addressBookLoaders[] = $callable;
  162. }
  163. /**
  164. * Get (and load when needed) the address book for $key
  165. *
  166. * @param string $addressBookKey
  167. * @return IAddressBook
  168. */
  169. protected function getAddressBook($addressBookKey) {
  170. $this->loadAddressBooks();
  171. if (!array_key_exists($addressBookKey, $this->addressBooks)) {
  172. return null;
  173. }
  174. return $this->addressBooks[$addressBookKey];
  175. }
  176. /**
  177. * Load all address books registered with 'register'
  178. */
  179. protected function loadAddressBooks() {
  180. foreach ($this->addressBookLoaders as $callable) {
  181. $callable($this);
  182. }
  183. $this->addressBookLoaders = [];
  184. }
  185. }