contactsmanager.php 4.3 KB

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