1
0

contacts.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. /**
  25. * Public interface of ownCloud for apps to use.
  26. * Contacts Class
  27. *
  28. */
  29. // use OCP namespace for all classes that are considered public.
  30. // This means that they should be used by apps instead of the internal ownCloud classes
  31. namespace OCP {
  32. /**
  33. * This class provides access to the contacts app. Use this class exclusively if you want to access contacts.
  34. *
  35. * Contacts in general will be expressed as an array of key-value-pairs.
  36. * The keys will match the property names defined in https://tools.ietf.org/html/rfc2426#section-1
  37. *
  38. * Proposed workflow for working with contacts:
  39. * - search for the contacts
  40. * - manipulate the results array
  41. * - createOrUpdate will save the given contacts overwriting the existing data
  42. *
  43. * For updating it is mandatory to keep the id.
  44. * Without an id a new contact will be created.
  45. *
  46. */
  47. class Contacts {
  48. /**
  49. * This function is used to search and find contacts within the users address books.
  50. * In case $pattern is empty all contacts will be returned.
  51. *
  52. * Example:
  53. * Following function shows how to search for contacts for the name and the email address.
  54. *
  55. * public static function getMatchingRecipient($term) {
  56. * // The API is not active -> nothing to do
  57. * if (!\OCP\Contacts::isEnabled()) {
  58. * return array();
  59. * }
  60. *
  61. * $result = \OCP\Contacts::search($term, array('FN', 'EMAIL'));
  62. * $receivers = array();
  63. * foreach ($result as $r) {
  64. * $id = $r['id'];
  65. * $fn = $r['FN'];
  66. * $email = $r['EMAIL'];
  67. * if (!is_array($email)) {
  68. * $email = array($email);
  69. * }
  70. *
  71. * // loop through all email addresses of this contact
  72. * foreach ($email as $e) {
  73. * $displayName = $fn . " <$e>";
  74. * $receivers[] = array(
  75. * 'id' => $id,
  76. * 'label' => $displayName,
  77. * 'value' => $displayName);
  78. * }
  79. * }
  80. *
  81. * return $receivers;
  82. * }
  83. *
  84. *
  85. * @param string $pattern which should match within the $searchProperties
  86. * @param array $searchProperties defines the properties within the query pattern should match
  87. * @param array $options - for future use. One should always have options!
  88. * @return array an array of contacts which are arrays of key-value-pairs
  89. */
  90. public static function search($pattern, $searchProperties = array(), $options = array()) {
  91. $cm = \OC::$server->getContactsManager();
  92. return $cm->search($pattern, $searchProperties, $options);
  93. }
  94. /**
  95. * This function can be used to delete the contact identified by the given id
  96. *
  97. * @param object $id the unique identifier to a contact
  98. * @param string $address_book_key
  99. * @return bool successful or not
  100. */
  101. public static function delete($id, $address_book_key) {
  102. $cm = \OC::$server->getContactsManager();
  103. return $cm->delete($id, $address_book_key);
  104. }
  105. /**
  106. * This function is used to create a new contact if 'id' is not given or not present.
  107. * Otherwise the contact will be updated by replacing the entire data set.
  108. *
  109. * @param array $properties this array if key-value-pairs defines a contact
  110. * @param string $address_book_key identifier of the address book in which the contact shall be created or updated
  111. * @return array an array representing the contact just created or updated
  112. */
  113. public static function createOrUpdate($properties, $address_book_key) {
  114. $cm = \OC::$server->getContactsManager();
  115. return $cm->createOrUpdate($properties, $address_book_key);
  116. }
  117. /**
  118. * Check if contacts are available (e.g. contacts app enabled)
  119. *
  120. * @return bool true if enabled, false if not
  121. */
  122. public static function isEnabled() {
  123. $cm = \OC::$server->getContactsManager();
  124. return $cm->isEnabled();
  125. }
  126. /**
  127. * @param \OCP\IAddressBook $address_book
  128. */
  129. public static function registerAddressBook(\OCP\IAddressBook $address_book) {
  130. $cm = \OC::$server->getContactsManager();
  131. $cm->registerAddressBook($address_book);
  132. }
  133. /**
  134. * @param \OCP\IAddressBook $address_book
  135. */
  136. public static function unregisterAddressBook(\OCP\IAddressBook $address_book) {
  137. $cm = \OC::$server->getContactsManager();
  138. $cm->unregisterAddressBook($address_book);
  139. }
  140. /**
  141. * @return array
  142. */
  143. public static function getAddressBooks() {
  144. $cm = \OC::$server->getContactsManager();
  145. return $cm->getAddressBooks();
  146. }
  147. /**
  148. * removes all registered address book instances
  149. */
  150. public static function clear() {
  151. $cm = \OC::$server->getContactsManager();
  152. $cm->clear();
  153. }
  154. }
  155. }