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.

contactsmanager.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 of contacts which are arrays of key-value-pairs
  32. */
  33. public function search($pattern, $searchProperties = array(), $options = array()) {
  34. $result = array();
  35. foreach($this->address_books as $address_book) {
  36. $r = $address_book->search($pattern, $searchProperties, $options);
  37. $contacts = array();
  38. foreach($r as $c){
  39. $c['addressbook-key'] = $address_book->getKey();
  40. $contacts[] = $c;
  41. }
  42. $result = array_merge($result, $contacts);
  43. }
  44. return $result;
  45. }
  46. /**
  47. * This function can be used to delete the contact identified by the given id
  48. *
  49. * @param object $id the unique identifier to a contact
  50. * @param string $address_book_key identifier of the address book in which the contact shall be deleted
  51. * @return bool successful or not
  52. */
  53. public function delete($id, $address_book_key) {
  54. if (!array_key_exists($address_book_key, $this->address_books))
  55. return null;
  56. $address_book = $this->address_books[$address_book_key];
  57. if ($address_book->getPermissions() & \OCP\PERMISSION_DELETE)
  58. return null;
  59. return $address_book->delete($id);
  60. }
  61. /**
  62. * This function is used to create a new contact if 'id' is not given or not present.
  63. * Otherwise the contact will be updated by replacing the entire data set.
  64. *
  65. * @param array $properties this array if key-value-pairs defines a contact
  66. * @param string $address_book_key identifier of the address book in which the contact shall be created or updated
  67. * @return array representing the contact just created or updated
  68. */
  69. public function createOrUpdate($properties, $address_book_key) {
  70. if (!array_key_exists($address_book_key, $this->address_books))
  71. return null;
  72. $address_book = $this->address_books[$address_book_key];
  73. if ($address_book->getPermissions() & \OCP\PERMISSION_CREATE)
  74. return null;
  75. return $address_book->createOrUpdate($properties);
  76. }
  77. /**
  78. * Check if contacts are available (e.g. contacts app enabled)
  79. *
  80. * @return bool true if enabled, false if not
  81. */
  82. public function isEnabled() {
  83. return !empty($this->address_books);
  84. }
  85. /**
  86. * @param \OCP\IAddressBook $address_book
  87. */
  88. public function registerAddressBook(\OCP\IAddressBook $address_book) {
  89. $this->address_books[$address_book->getKey()] = $address_book;
  90. }
  91. /**
  92. * @param \OCP\IAddressBook $address_book
  93. */
  94. public function unregisterAddressBook(\OCP\IAddressBook $address_book) {
  95. unset($this->address_books[$address_book->getKey()]);
  96. }
  97. /**
  98. * @return array
  99. */
  100. public function getAddressBooks() {
  101. $result = array();
  102. foreach($this->address_books as $address_book) {
  103. $result[$address_book->getKey()] = $address_book->getDisplayName();
  104. }
  105. return $result;
  106. }
  107. /**
  108. * removes all registered address book instances
  109. */
  110. public function clear() {
  111. $this->address_books = array();
  112. }
  113. /**
  114. * @var \OCP\IAddressBook[] which holds all registered address books
  115. */
  116. private $address_books = array();
  117. /**
  118. * In order to improve lazy loading a closure can be registered which will be called in case
  119. * address books are actually requested
  120. *
  121. * @param string $key
  122. * @param \Closure $callable
  123. */
  124. function register($key, \Closure $callable)
  125. {
  126. //
  127. //TODO: implement me
  128. //
  129. }
  130. }
  131. }