|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2013 Thomas Tanghus ([email protected]) |
|
4
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
5
|
|
|
* later. |
|
6
|
|
|
* See the COPYING-README file. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OCA\Contacts\Backend; |
|
10
|
|
|
|
|
11
|
|
|
class Mock extends AbstractBackend { |
|
12
|
|
|
|
|
13
|
|
|
public $name = 'mock'; |
|
14
|
|
|
public $addressBooks; |
|
15
|
|
|
public $contacts; |
|
16
|
|
|
public $userid; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $userid |
|
20
|
|
|
*/ |
|
21
|
|
|
function __construct($userid = null, $addressBooks = null, $contacts = null) { |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
$this->userid = $userid ? $userid : \OC::$server->getUserSession()->getUser()->getUId(); |
|
24
|
|
|
$this->addressBooks = $addressBooks; |
|
25
|
|
|
$this->contacts = $contacts; |
|
26
|
|
|
|
|
27
|
|
|
if (is_null($this->addressBooks)) { |
|
28
|
|
|
$this->addressBooks = array( |
|
29
|
|
|
array( |
|
30
|
|
|
'id' => 'foo', |
|
31
|
|
|
'owner' => $userid, |
|
32
|
|
|
'displayname' => 'd-name', |
|
33
|
|
|
'permissions' => \OCP\PERMISSION_ALL, |
|
34
|
|
|
), |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
$this->contacts = array( |
|
38
|
|
|
'foo' => array( |
|
39
|
|
|
'123' => |
|
40
|
|
|
array( |
|
41
|
|
|
'id' => '123', |
|
42
|
|
|
'displayname' => 'Max Mustermann', |
|
43
|
|
|
'carddata' => file_get_contents(__DIR__ . '/../../data/test1.vcf') |
|
44
|
|
|
) |
|
45
|
|
|
), |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
function getAddressBooksForUser(array $options = array()) { |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$books = array(); |
|
55
|
|
|
foreach($this->addressBooks as $book) { |
|
56
|
|
|
if ($book['owner'] === $this->userid) { |
|
57
|
|
|
$books[] = $book; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
return $books; |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
function getAddressBook($addressBookId, array $options = array()) { |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
foreach($this->addressBooks as &$book) { |
|
67
|
|
|
if ($book['id'] === $addressBookId) { |
|
68
|
|
|
return $book; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function updateAddressBook($addressBookId, array $changes, array $options = array()) { |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
if(count($changes) === 0 || !isset($changes['displayname'])) { |
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
foreach($this->addressBooks as &$book) { |
|
81
|
|
|
if ($book['id'] === $addressBookId) { |
|
82
|
|
|
foreach($changes as $key => $value) { |
|
83
|
|
|
$book[$key] = $value; |
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return false; |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
function createAddressBook(array $properties, array $options = array()) { |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
if(count($properties) === 0 || !isset($properties['displayname'])) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$id = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate('4'); |
|
100
|
|
|
$this->addressBooks[] = array_merge($properties, array( |
|
101
|
|
|
'id' => $id, |
|
102
|
|
|
'permissions' => \OCP\PERMISSION_ALL, |
|
103
|
|
|
'owner' => $this->userid, |
|
104
|
|
|
)); |
|
105
|
|
|
|
|
106
|
|
|
return $id; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
function deleteAddressBook($addressBookId, array $options = array()) { |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
foreach($this->addressBooks as $key => $value) { |
|
112
|
|
|
if ($value['id'] === $addressBookId) { |
|
113
|
|
|
unset($this->addressBooks[$key]); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
if(isset($this->contacts[$addressBookId])) { |
|
117
|
|
|
unset($this->contacts[$addressBookId]); |
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
return false; |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
function getContacts($addressBookId, array $options = array()) { |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
$contacts = array(); |
|
127
|
|
|
$omitdata = isset($options['omitdata']) ? $options['omitdata'] : false; |
|
128
|
|
|
$book = $this->getAddressBook($addressBookId); |
|
129
|
|
|
if(!$book) { |
|
130
|
|
|
return $contacts; |
|
131
|
|
|
} |
|
132
|
|
|
foreach($this->contacts[$addressBookId] as $contact) { |
|
133
|
|
|
$contact['permissions'] = $book['permissions']; |
|
134
|
|
|
$contact['owner'] = $book['owner']; |
|
135
|
|
|
if($omitdata) { |
|
136
|
|
|
unset($contact['carddata']); |
|
137
|
|
|
} |
|
138
|
|
|
$contacts[] = $contact; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $contacts; |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
function getContact($addressBookId, $id, array $options = array()) { |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
$book = $this->getAddressBook($addressBookId); |
|
148
|
|
|
if(!$book) { |
|
149
|
|
|
return null; |
|
150
|
|
|
} |
|
151
|
|
|
if (!isset($this->contacts[$addressBookId][$id])) { |
|
152
|
|
|
return null; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$contact = $this->contacts[$addressBookId][$id]; |
|
156
|
|
|
$contact['permissions'] = $book['permissions']; |
|
157
|
|
|
$contact['owner'] = $book['owner']; |
|
158
|
|
|
|
|
159
|
|
|
return $contact; |
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
function createContact($addressBookId, $contact, array $options = array()) { |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
$id = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate('4'); |
|
166
|
|
|
$this->contacts[$addressBookId][$id] = array( |
|
167
|
|
|
'id' => $id, |
|
168
|
|
|
'displayname' => $contact->FN, |
|
169
|
|
|
'carddata' => $contact->serialize() |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
return $id; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
function updateContact($addressBookId, $id, $contact, array $options = array()) { |
|
|
|
|
|
|
176
|
|
|
//echo __METHOD__ . $addressBookId .', ' . $id . ', ' . print_r($contact, true); |
|
177
|
|
|
$this->contacts[$addressBookId][$id] = array( |
|
178
|
|
|
'displayname' => $contact->FN, |
|
179
|
|
|
'carddata' => $contact->serialize() |
|
180
|
|
|
); |
|
181
|
|
|
|
|
182
|
|
|
return true; |
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
function deleteContact($addressBookId, $id, array $options = array()) { |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
if(isset($this->contacts[$addressBookId][$id])) { |
|
189
|
|
|
unset($this->contacts[$addressBookId][$id]); |
|
190
|
|
|
return true; |
|
191
|
|
|
} |
|
192
|
|
|
return false; |
|
193
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
function numContacts($addressBookId) { |
|
|
|
|
|
|
197
|
|
|
return count($this->contacts[$addressBookId]); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.