contactsmanager.php 5.2 KB

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