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.

189 lines
5.5 KiB

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