contacts.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 of contacts which are arrays of key-value-pairs
  87. */
  88. public static function search($pattern, $searchProperties = array(), $options = array()) {
  89. $result = array();
  90. foreach(self::$address_books as $address_book) {
  91. $r = $address_book->search($pattern, $searchProperties, $options);
  92. $result = array_merge($result, $r);
  93. }
  94. return $result;
  95. }
  96. /**
  97. * This function can be used to delete the contact identified by the given id
  98. *
  99. * @param object $id the unique identifier to a contact
  100. * @param $address_book_key
  101. * @return bool successful or not
  102. */
  103. public static function delete($id, $address_book_key) {
  104. if (!array_key_exists($address_book_key, self::$address_books))
  105. return null;
  106. $address_book = self::$address_books[$address_book_key];
  107. if ($address_book->getPermissions() & \OCP\PERMISSION_DELETE)
  108. return null;
  109. return $address_book->delete($id);
  110. }
  111. /**
  112. * This function is used to create a new contact if 'id' is not given or not present.
  113. * Otherwise the contact will be updated by replacing the entire data set.
  114. *
  115. * @param array $properties this array if key-value-pairs defines a contact
  116. * @param $address_book_key string to identify the address book in which the contact shall be created or updated
  117. * @return array representing the contact just created or updated
  118. */
  119. public static function createOrUpdate($properties, $address_book_key) {
  120. if (!array_key_exists($address_book_key, self::$address_books))
  121. return null;
  122. $address_book = self::$address_books[$address_book_key];
  123. if ($address_book->getPermissions() & \OCP\PERMISSION_CREATE)
  124. return null;
  125. return $address_book->createOrUpdate($properties);
  126. }
  127. /**
  128. * Check if contacts are available (e.g. contacts app enabled)
  129. *
  130. * @return bool true if enabled, false if not
  131. */
  132. public static function isEnabled() {
  133. return !empty(self::$address_books);
  134. }
  135. /**
  136. * @param \OCP\IAddressBook $address_book
  137. */
  138. public static function registerAddressBook($address_book) {
  139. self::$address_books[$address_book->getKey()] = $address_book;
  140. }
  141. /**
  142. * @param \OCP\IAddressBook $address_book
  143. */
  144. public static function unregisterAddressBook($address_book) {
  145. unset(self::$address_books[$address_book->getKey()]);
  146. }
  147. /**
  148. * @return array
  149. */
  150. public static function getAddressBooks() {
  151. $result = array();
  152. foreach(self::$address_books as $address_book) {
  153. $result[$address_book->getKey()] = $address_book->getDisplayName();
  154. }
  155. return $result;
  156. }
  157. /**
  158. * removes all registered address book instances
  159. */
  160. public static function clear() {
  161. self::$address_books = array();
  162. }
  163. /**
  164. * @var \OCP\IAddressBook[] which holds all registered address books
  165. */
  166. private static $address_books = array();
  167. }
  168. }