ContactsManager.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  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. class ContactsManager implements \OCP\Contacts\IManager {
  29. /**
  30. * This function is used to search and find contacts within the users address books.
  31. * In case $pattern is empty all contacts will be returned.
  32. *
  33. * @param string $pattern which should match within the $searchProperties
  34. * @param array $searchProperties defines the properties within the query pattern should match
  35. * @param array $options - for future use. One should always have options!
  36. * @return array an array of contacts which are arrays of key-value-pairs
  37. */
  38. public function search($pattern, $searchProperties = array(), $options = array()) {
  39. $this->loadAddressBooks();
  40. $result = array();
  41. foreach($this->addressBooks as $addressBook) {
  42. $r = $addressBook->search($pattern, $searchProperties, $options);
  43. $contacts = array();
  44. foreach($r as $c){
  45. $c['addressbook-key'] = $addressBook->getKey();
  46. $contacts[] = $c;
  47. }
  48. $result = array_merge($result, $contacts);
  49. }
  50. return $result;
  51. }
  52. /**
  53. * This function can be used to delete the contact identified by the given id
  54. *
  55. * @param object $id the unique identifier to a contact
  56. * @param string $addressBookKey identifier of the address book in which the contact shall be deleted
  57. * @return bool successful or not
  58. */
  59. public function delete($id, $addressBookKey) {
  60. $addressBook = $this->getAddressBook($addressBookKey);
  61. if (!$addressBook) {
  62. return null;
  63. }
  64. if ($addressBook->getPermissions() & \OCP\Constants::PERMISSION_DELETE) {
  65. return $addressBook->delete($id);
  66. }
  67. return null;
  68. }
  69. /**
  70. * This function is used to create a new contact if 'id' is not given or not present.
  71. * Otherwise the contact will be updated by replacing the entire data set.
  72. *
  73. * @param array $properties this array if key-value-pairs defines a contact
  74. * @param string $addressBookKey identifier of the address book in which the contact shall be created or updated
  75. * @return array representing the contact just created or updated
  76. */
  77. public function createOrUpdate($properties, $addressBookKey) {
  78. $addressBook = $this->getAddressBook($addressBookKey);
  79. if (!$addressBook) {
  80. return null;
  81. }
  82. if ($addressBook->getPermissions() & \OCP\Constants::PERMISSION_CREATE) {
  83. return $addressBook->createOrUpdate($properties);
  84. }
  85. return null;
  86. }
  87. /**
  88. * Check if contacts are available (e.g. contacts app enabled)
  89. *
  90. * @return bool true if enabled, false if not
  91. */
  92. public function isEnabled() {
  93. return !empty($this->addressBooks) || !empty($this->addressBookLoaders);
  94. }
  95. /**
  96. * @param \OCP\IAddressBook $addressBook
  97. */
  98. public function registerAddressBook(\OCP\IAddressBook $addressBook) {
  99. $this->addressBooks[$addressBook->getKey()] = $addressBook;
  100. }
  101. /**
  102. * @param \OCP\IAddressBook $addressBook
  103. */
  104. public function unregisterAddressBook(\OCP\IAddressBook $addressBook) {
  105. unset($this->addressBooks[$addressBook->getKey()]);
  106. }
  107. /**
  108. * Return a list of the user's addressbooks display names
  109. * ! The addressBook displayName are not unique, please use getUserAddressBooks
  110. *
  111. * @return array
  112. * @since 6.0.0
  113. * @deprecated 16.0.0 - Use `$this->getUserAddressBooks()` instead
  114. */
  115. public function getAddressBooks() {
  116. $this->loadAddressBooks();
  117. $result = array();
  118. foreach($this->addressBooks as $addressBook) {
  119. $result[$addressBook->getKey()] = $addressBook->getDisplayName();
  120. }
  121. return $result;
  122. }
  123. /**
  124. * Return a list of the user's addressbooks
  125. *
  126. * @return IAddressBook[]
  127. * @since 16.0.0
  128. */
  129. public function getUserAddressBooks(): Array {
  130. $this->loadAddressBooks();
  131. return $this->addressBooks;
  132. }
  133. /**
  134. * removes all registered address book instances
  135. */
  136. public function clear() {
  137. $this->addressBooks = array();
  138. $this->addressBookLoaders = array();
  139. }
  140. /**
  141. * @var \OCP\IAddressBook[] which holds all registered address books
  142. */
  143. private $addressBooks = array();
  144. /**
  145. * @var \Closure[] to call to load/register address books
  146. */
  147. private $addressBookLoaders = array();
  148. /**
  149. * In order to improve lazy loading a closure can be registered which will be called in case
  150. * address books are actually requested
  151. *
  152. * @param \Closure $callable
  153. */
  154. public function register(\Closure $callable)
  155. {
  156. $this->addressBookLoaders[] = $callable;
  157. }
  158. /**
  159. * Get (and load when needed) the address book for $key
  160. *
  161. * @param string $addressBookKey
  162. * @return \OCP\IAddressBook
  163. */
  164. protected function getAddressBook($addressBookKey)
  165. {
  166. $this->loadAddressBooks();
  167. if (!array_key_exists($addressBookKey, $this->addressBooks)) {
  168. return null;
  169. }
  170. return $this->addressBooks[$addressBookKey];
  171. }
  172. /**
  173. * Load all address books registered with 'register'
  174. */
  175. protected function loadAddressBooks()
  176. {
  177. foreach($this->addressBookLoaders as $callable) {
  178. $callable($this);
  179. }
  180. $this->addressBookLoaders = array();
  181. }
  182. }
  183. }