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.

contacts.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Thomas Müller
  6. * @copyright 2012 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. /**
  23. * Public interface of ownCloud for apps to use.
  24. * Contacts Class
  25. *
  26. */
  27. // use OCP namespace for all classes that are considered public.
  28. // This means that they should be used by apps instead of the internal ownCloud classes
  29. namespace OCP {
  30. /**
  31. * This class provides access to the contacts app. Use this class exclusively if you want to access contacts.
  32. *
  33. * Contacts in general will be expressed as an array of key-value-pairs.
  34. * The keys will match the property names defined in https://tools.ietf.org/html/rfc2426#section-1
  35. *
  36. * Proposed workflow for working with contacts:
  37. * - search for the contacts
  38. * - manipulate the results array
  39. * - createOrUpdate will save the given contacts overwriting the existing data
  40. *
  41. * For updating it is mandatory to keep the id.
  42. * Without an id a new contact will be created.
  43. *
  44. */
  45. class Contacts {
  46. /**
  47. * This function is used to search and find contacts within the users address books.
  48. * In case $pattern is empty all contacts will be returned.
  49. *
  50. * Example:
  51. * Following function shows how to search for contacts for the name and the email address.
  52. *
  53. * public static function getMatchingRecipient($term) {
  54. * // The API is not active -> nothing to do
  55. * if (!\OCP\Contacts::isEnabled()) {
  56. * return array();
  57. * }
  58. *
  59. * $result = \OCP\Contacts::search($term, array('FN', 'EMAIL'));
  60. * $receivers = array();
  61. * foreach ($result as $r) {
  62. * $id = $r['id'];
  63. * $fn = $r['FN'];
  64. * $email = $r['EMAIL'];
  65. * if (!is_array($email)) {
  66. * $email = array($email);
  67. * }
  68. *
  69. * // loop through all email addresses of this contact
  70. * foreach ($email as $e) {
  71. * $displayName = $fn . " <$e>";
  72. * $receivers[] = array(
  73. * 'id' => $id,
  74. * 'label' => $displayName,
  75. * 'value' => $displayName);
  76. * }
  77. * }
  78. *
  79. * return $receivers;
  80. * }
  81. *
  82. *
  83. * @param string $pattern which should match within the $searchProperties
  84. * @param array $searchProperties defines the properties within the query pattern should match
  85. * @param array $options - for future use. One should always have options!
  86. * @return array an array of contacts which are arrays of key-value-pairs
  87. */
  88. public static function search($pattern, $searchProperties = array(), $options = array()) {
  89. $cm = \OC::$server->getContactsManager();
  90. return $cm->search($pattern, $searchProperties, $options);
  91. }
  92. /**
  93. * This function can be used to delete the contact identified by the given id
  94. *
  95. * @param object $id the unique identifier to a contact
  96. * @param string $address_book_key
  97. * @return bool successful or not
  98. */
  99. public static function delete($id, $address_book_key) {
  100. $cm = \OC::$server->getContactsManager();
  101. return $cm->delete($id, $address_book_key);
  102. }
  103. /**
  104. * This function is used to create a new contact if 'id' is not given or not present.
  105. * Otherwise the contact will be updated by replacing the entire data set.
  106. *
  107. * @param array $properties this array if key-value-pairs defines a contact
  108. * @param string $address_book_key identifier of the address book in which the contact shall be created or updated
  109. * @return array an array representing the contact just created or updated
  110. */
  111. public static function createOrUpdate($properties, $address_book_key) {
  112. $cm = \OC::$server->getContactsManager();
  113. return $cm->createOrUpdate($properties, $address_book_key);
  114. }
  115. /**
  116. * Check if contacts are available (e.g. contacts app enabled)
  117. *
  118. * @return bool true if enabled, false if not
  119. */
  120. public static function isEnabled() {
  121. $cm = \OC::$server->getContactsManager();
  122. return $cm->isEnabled();
  123. }
  124. /**
  125. * @param \OCP\IAddressBook $address_book
  126. */
  127. public static function registerAddressBook(\OCP\IAddressBook $address_book) {
  128. $cm = \OC::$server->getContactsManager();
  129. $cm->registerAddressBook($address_book);
  130. }
  131. /**
  132. * @param \OCP\IAddressBook $address_book
  133. */
  134. public static function unregisterAddressBook(\OCP\IAddressBook $address_book) {
  135. $cm = \OC::$server->getContactsManager();
  136. $cm->unregisterAddressBook($address_book);
  137. }
  138. /**
  139. * @return array
  140. */
  141. public static function getAddressBooks() {
  142. $cm = \OC::$server->getContactsManager();
  143. return $cm->getAddressBooks();
  144. }
  145. /**
  146. * removes all registered address book instances
  147. */
  148. public static function clear() {
  149. $cm = \OC::$server->getContactsManager();
  150. $cm->clear();
  151. }
  152. }
  153. }