ContactsManager.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 array
  109. */
  110. public function getAddressBooks() {
  111. $this->loadAddressBooks();
  112. $result = array();
  113. foreach($this->addressBooks as $addressBook) {
  114. $result[$addressBook->getKey()] = $addressBook->getDisplayName();
  115. }
  116. return $result;
  117. }
  118. /**
  119. * removes all registered address book instances
  120. */
  121. public function clear() {
  122. $this->addressBooks = array();
  123. $this->addressBookLoaders = array();
  124. }
  125. /**
  126. * @var \OCP\IAddressBook[] which holds all registered address books
  127. */
  128. private $addressBooks = array();
  129. /**
  130. * @var \Closure[] to call to load/register address books
  131. */
  132. private $addressBookLoaders = array();
  133. /**
  134. * In order to improve lazy loading a closure can be registered which will be called in case
  135. * address books are actually requested
  136. *
  137. * @param \Closure $callable
  138. */
  139. public function register(\Closure $callable)
  140. {
  141. $this->addressBookLoaders[] = $callable;
  142. }
  143. /**
  144. * Get (and load when needed) the address book for $key
  145. *
  146. * @param string $addressBookKey
  147. * @return \OCP\IAddressBook
  148. */
  149. protected function getAddressBook($addressBookKey)
  150. {
  151. $this->loadAddressBooks();
  152. if (!array_key_exists($addressBookKey, $this->addressBooks)) {
  153. return null;
  154. }
  155. return $this->addressBooks[$addressBookKey];
  156. }
  157. /**
  158. * Load all address books registered with 'register'
  159. */
  160. protected function loadAddressBooks()
  161. {
  162. foreach($this->addressBookLoaders as $callable) {
  163. $callable($this);
  164. }
  165. $this->addressBookLoaders = array();
  166. }
  167. }
  168. }