localaddressbook.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Thomas Müller
  6. * @copyright 2014 Thomas Müller >deepdiver@owncloud.com>
  7. *
  8. */
  9. namespace OC\Contacts;
  10. class LocalAddressBook implements \OCP\IAddressBook {
  11. /**
  12. * @var \OCP\IUserManager
  13. */
  14. private $userManager;
  15. /**
  16. * @param $userManager
  17. */
  18. public function __construct($userManager) {
  19. $this->userManager = $userManager;
  20. }
  21. /**
  22. * @return string defining the technical unique key
  23. */
  24. public function getKey() {
  25. return 'local';
  26. }
  27. /**
  28. * In comparison to getKey() this function returns a human readable (maybe translated) name
  29. *
  30. * @return mixed
  31. */
  32. public function getDisplayName() {
  33. return "Local users";
  34. }
  35. /**
  36. * @param string $pattern which should match within the $searchProperties
  37. * @param array $searchProperties defines the properties within the query pattern should match
  38. * @param array $options - for future use. One should always have options!
  39. * @return array an array of contacts which are arrays of key-value-pairs
  40. */
  41. public function search($pattern, $searchProperties, $options) {
  42. $users = array();
  43. if($pattern == '') {
  44. // Fetch all contacts
  45. $users = $this->userManager->search('');
  46. } else {
  47. foreach($searchProperties as $property) {
  48. $result = array();
  49. if($property === 'FN') {
  50. $result = $this->userManager->searchDisplayName($pattern);
  51. } else if ($property === 'id') {
  52. $result = $this->userManager->search($pattern);
  53. }
  54. if (is_array($result)) {
  55. $users = array_merge($users, $result);
  56. }
  57. }
  58. }
  59. $contacts = array();
  60. foreach($users as $user){
  61. $contact = array(
  62. "id" => $user->getUID(),
  63. "FN" => $user->getDisplayname(),
  64. "EMAIL" => array(),
  65. "IMPP" => array(
  66. "x-owncloud-handle:" . $user->getUID()
  67. )
  68. );
  69. $contacts[] = $contact;
  70. }
  71. return $contacts;
  72. }
  73. /**
  74. * @param array $properties this array if key-value-pairs defines a contact
  75. * @return array an array representing the contact just created or updated
  76. */
  77. public function createOrUpdate($properties) {
  78. return array();
  79. }
  80. /**
  81. * @return int
  82. */
  83. public function getPermissions() {
  84. return \OCP\PERMISSION_READ;
  85. }
  86. /**
  87. * @param object $id the unique identifier to a contact
  88. * @return bool successful or not
  89. */
  90. public function delete($id) {
  91. return false;
  92. }
  93. }