~pvigo/+junk/owncloud-14.04

« back to all changes in this revision

Viewing changes to usr/share/owncloud/lib/private/contactsmanager.php

  • Committer: Pablo Vigo
  • Date: 2014-12-15 13:36:46 UTC
  • Revision ID: pvigo@xtec.cat-20141215133646-7d6it90e1dbsijc2
2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * ownCloud
4
 
 *
5
 
 * @author Thomas Müller
6
 
 * @copyright 2013 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
 
namespace OC {
24
 
 
25
 
        class ContactsManager implements \OCP\Contacts\IManager {
26
 
 
27
 
                /**
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.
30
 
                 *
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
35
 
                 */
36
 
                public function search($pattern, $searchProperties = array(), $options = array()) {
37
 
                        $result = array();
38
 
                        foreach($this->address_books as $address_book) {
39
 
                                $r = $address_book->search($pattern, $searchProperties, $options);
40
 
                                $result = array_merge($result, $r);
41
 
                        }
42
 
 
43
 
                        return $result;
44
 
                }
45
 
 
46
 
                /**
47
 
                 * This function can be used to delete the contact identified by the given id
48
 
                 *
49
 
                 * @param object $id the unique identifier to a contact
50
 
                 * @param $address_book_key
51
 
                 * @return bool successful or not
52
 
                 */
53
 
                public function delete($id, $address_book_key) {
54
 
                        if (!array_key_exists($address_book_key, $this->address_books))
55
 
                                return null;
56
 
 
57
 
                        $address_book = $this->address_books[$address_book_key];
58
 
                        if ($address_book->getPermissions() & \OCP\PERMISSION_DELETE)
59
 
                                return null;
60
 
 
61
 
                        return $address_book->delete($id);
62
 
                }
63
 
 
64
 
                /**
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.
67
 
                 *
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
71
 
                 */
72
 
                public function createOrUpdate($properties, $address_book_key) {
73
 
 
74
 
                        if (!array_key_exists($address_book_key, $this->address_books))
75
 
                                return null;
76
 
 
77
 
                        $address_book = $this->address_books[$address_book_key];
78
 
                        if ($address_book->getPermissions() & \OCP\PERMISSION_CREATE)
79
 
                                return null;
80
 
 
81
 
                        return $address_book->createOrUpdate($properties);
82
 
                }
83
 
 
84
 
                /**
85
 
                 * Check if contacts are available (e.g. contacts app enabled)
86
 
                 *
87
 
                 * @return bool true if enabled, false if not
88
 
                 */
89
 
                public function isEnabled() {
90
 
                        return !empty($this->address_books);
91
 
                }
92
 
 
93
 
                /**
94
 
                 * @param \OCP\IAddressBook $address_book
95
 
                 */
96
 
                public function registerAddressBook(\OCP\IAddressBook $address_book) {
97
 
                        $this->address_books[$address_book->getKey()] = $address_book;
98
 
                }
99
 
 
100
 
                /**
101
 
                 * @param \OCP\IAddressBook $address_book
102
 
                 */
103
 
                public function unregisterAddressBook(\OCP\IAddressBook $address_book) {
104
 
                        unset($this->address_books[$address_book->getKey()]);
105
 
                }
106
 
 
107
 
                /**
108
 
                 * @return array
109
 
                 */
110
 
                public function getAddressBooks() {
111
 
                        $result = array();
112
 
                        foreach($this->address_books as $address_book) {
113
 
                                $result[$address_book->getKey()] = $address_book->getDisplayName();
114
 
                        }
115
 
 
116
 
                        return $result;
117
 
                }
118
 
 
119
 
                /**
120
 
                 * removes all registered address book instances
121
 
                 */
122
 
                public function clear() {
123
 
                        $this->address_books = array();
124
 
                }
125
 
 
126
 
                /**
127
 
                 * @var \OCP\IAddressBook[] which holds all registered address books
128
 
                 */
129
 
                private $address_books = array();
130
 
 
131
 
                /**
132
 
                 * In order to improve lazy loading a closure can be registered which will be called in case
133
 
                 * address books are actually requested
134
 
                 *
135
 
                 * @param string $key
136
 
                 * @param \Closure $callable
137
 
                 */
138
 
                function register($key, \Closure $callable)
139
 
                {
140
 
                        //
141
 
                        //TODO: implement me
142
 
                        //
143
 
                }
144
 
        }
145
 
}