123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace OC;
- use OCP\Constants;
- use OCP\Contacts\IManager;
- use OCP\IAddressBook;
- class ContactsManager implements IManager {
-
- public function search($pattern, $searchProperties = [], $options = []) {
- $this->loadAddressBooks();
- $result = [];
- foreach ($this->addressBooks as $addressBook) {
- $searchOptions = $options;
- $strictSearch = array_key_exists('strict_search', $options) && $options['strict_search'] === true;
- if ($addressBook->isSystemAddressBook()) {
- $enumeration = !\array_key_exists('enumeration', $options) || $options['enumeration'] !== false;
- $fullMatch = !\array_key_exists('fullmatch', $options) || $options['fullmatch'] !== false;
- if (!$enumeration && !$fullMatch) {
-
- continue;
- }
- if ($strictSearch) {
- $searchOptions['wildcard'] = false;
- } else {
- $searchOptions['wildcard'] = $enumeration;
- }
- } else {
- $searchOptions['wildcard'] = !$strictSearch;
- }
- $r = $addressBook->search($pattern, $searchProperties, $searchOptions);
- $contacts = [];
- foreach ($r as $c) {
- $c['addressbook-key'] = $addressBook->getKey();
- $contacts[] = $c;
- }
- $result = array_merge($result, $contacts);
- }
- return $result;
- }
-
- public function delete($id, $addressBookKey) {
- $addressBook = $this->getAddressBook($addressBookKey);
- if (!$addressBook) {
- return false;
- }
- if ($addressBook->getPermissions() & Constants::PERMISSION_DELETE) {
- return $addressBook->delete($id);
- }
- return false;
- }
-
- public function createOrUpdate($properties, $addressBookKey) {
- $addressBook = $this->getAddressBook($addressBookKey);
- if (!$addressBook) {
- return null;
- }
- if ($addressBook->getPermissions() & Constants::PERMISSION_CREATE) {
- return $addressBook->createOrUpdate($properties);
- }
- return null;
- }
-
- public function isEnabled(): bool {
- return !empty($this->addressBooks) || !empty($this->addressBookLoaders);
- }
-
- public function registerAddressBook(IAddressBook $addressBook) {
- $this->addressBooks[$addressBook->getKey()] = $addressBook;
- }
-
- public function unregisterAddressBook(IAddressBook $addressBook) {
- unset($this->addressBooks[$addressBook->getKey()]);
- }
-
- public function getUserAddressBooks(): array {
- $this->loadAddressBooks();
- return $this->addressBooks;
- }
-
- public function clear() {
- $this->addressBooks = [];
- $this->addressBookLoaders = [];
- }
-
- private $addressBooks = [];
-
- private $addressBookLoaders = [];
-
- public function register(\Closure $callable) {
- $this->addressBookLoaders[] = $callable;
- }
-
- protected function getAddressBook(string $addressBookKey): ?IAddressBook {
- $this->loadAddressBooks();
- if (!array_key_exists($addressBookKey, $this->addressBooks)) {
- return null;
- }
- return $this->addressBooks[$addressBookKey];
- }
-
- protected function loadAddressBooks() {
- foreach ($this->addressBookLoaders as $callable) {
- $callable($this);
- }
- $this->addressBookLoaders = [];
- }
- }
|