5
* @author Thomas Müller
6
* @copyright 2013 Thomas Müller thomas.mueller@tmit.eu
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.
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.
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/>.
25
class ContactsManager implements \OCP\Contacts\IManager {
28
* This function is used to search and find contacts within the users address books.
29
* In case $pattern is empty all contacts will be returned.
31
* @param string $pattern which should match within the $searchProperties
32
* @param array $searchProperties defines the properties within the query pattern should match
33
* @param array $options - for future use. One should always have options!
34
* @return array of contacts which are arrays of key-value-pairs
36
public function search($pattern, $searchProperties = array(), $options = array()) {
38
foreach($this->address_books as $address_book) {
39
$r = $address_book->search($pattern, $searchProperties, $options);
40
$result = array_merge($result, $r);
47
* This function can be used to delete the contact identified by the given id
49
* @param object $id the unique identifier to a contact
50
* @param $address_book_key
51
* @return bool successful or not
53
public function delete($id, $address_book_key) {
54
if (!array_key_exists($address_book_key, $this->address_books))
57
$address_book = $this->address_books[$address_book_key];
58
if ($address_book->getPermissions() & \OCP\PERMISSION_DELETE)
61
return $address_book->delete($id);
65
* This function is used to create a new contact if 'id' is not given or not present.
66
* Otherwise the contact will be updated by replacing the entire data set.
68
* @param array $properties this array if key-value-pairs defines a contact
69
* @param $address_book_key string to identify the address book in which the contact shall be created or updated
70
* @return array representing the contact just created or updated
72
public function createOrUpdate($properties, $address_book_key) {
74
if (!array_key_exists($address_book_key, $this->address_books))
77
$address_book = $this->address_books[$address_book_key];
78
if ($address_book->getPermissions() & \OCP\PERMISSION_CREATE)
81
return $address_book->createOrUpdate($properties);
85
* Check if contacts are available (e.g. contacts app enabled)
87
* @return bool true if enabled, false if not
89
public function isEnabled() {
90
return !empty($this->address_books);
94
* @param \OCP\IAddressBook $address_book
96
public function registerAddressBook(\OCP\IAddressBook $address_book) {
97
$this->address_books[$address_book->getKey()] = $address_book;
101
* @param \OCP\IAddressBook $address_book
103
public function unregisterAddressBook(\OCP\IAddressBook $address_book) {
104
unset($this->address_books[$address_book->getKey()]);
110
public function getAddressBooks() {
112
foreach($this->address_books as $address_book) {
113
$result[$address_book->getKey()] = $address_book->getDisplayName();
120
* removes all registered address book instances
122
public function clear() {
123
$this->address_books = array();
127
* @var \OCP\IAddressBook[] which holds all registered address books
129
private $address_books = array();
132
* In order to improve lazy loading a closure can be registered which will be called in case
133
* address books are actually requested
136
* @param \Closure $callable
138
function register($key, \Closure $callable)