|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Thomas Tanghus |
|
4
|
|
|
* @copyright 2013-2014 Thomas Tanghus ([email protected]) |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. |
|
7
|
|
|
* See the COPYING-README file. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace OCA\Contacts\Controller; |
|
11
|
|
|
|
|
12
|
|
|
use OCA\Contacts\App, |
|
13
|
|
|
OCA\Contacts\JSONResponse, |
|
14
|
|
|
OCA\Contacts\ImageResponse, |
|
15
|
|
|
OCA\Contacts\Utils\JSONSerializer, |
|
16
|
|
|
OCA\Contacts\Utils\Properties, |
|
17
|
|
|
OCA\Contacts\Controller, |
|
18
|
|
|
OCP\AppFramework\Http; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Controller class For Contacts |
|
22
|
|
|
*/ |
|
23
|
|
|
class ContactController extends Controller { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @NoAdminRequired |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getContact() { |
|
29
|
|
|
|
|
30
|
|
|
$params = $this->request->urlParams; |
|
31
|
|
|
$response = new JSONResponse(); |
|
32
|
|
|
|
|
33
|
|
|
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); |
|
34
|
|
|
$contact = $addressBook->getChild($params['contactId']); |
|
35
|
|
|
|
|
36
|
|
|
if (!$contact) { |
|
37
|
|
|
return $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$data = JSONSerializer::serializeContact($contact); |
|
41
|
|
|
|
|
42
|
|
|
return $response->setData($data); |
|
43
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @NoAdminRequired |
|
48
|
|
|
*/ |
|
49
|
|
|
public function saveContact() { |
|
50
|
|
|
|
|
51
|
|
|
$params = $this->request->urlParams; |
|
52
|
|
|
$data = isset($this->request->post['data']) ? $this->request->post['data'] : null; |
|
53
|
|
|
$response = new JSONResponse(); |
|
54
|
|
|
|
|
55
|
|
|
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); |
|
56
|
|
|
$contact = $addressBook->getChild($params['contactId']); |
|
57
|
|
|
|
|
58
|
|
|
if (!$data) { |
|
59
|
|
|
return $response->bailOut(App::$l10n->t('No contact data in request.')); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (!$contact) { |
|
63
|
|
|
return $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (!$contact->mergeFromArray($data)) { |
|
67
|
|
|
return $response->bailOut(App::$l10n->t('Error merging into contact.')); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (!$contact->save()) { |
|
71
|
|
|
return $response->bailOut(App::$l10n->t('Error saving contact to backend.')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $response->setData(JSONSerializer::serializeContact($contact)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @NoAdminRequired |
|
79
|
|
|
*/ |
|
80
|
|
|
public function patch() { |
|
81
|
|
|
$params = $this->request->urlParams; |
|
82
|
|
|
|
|
83
|
|
|
$patch = $this->request->patch; |
|
84
|
|
|
$response = new JSONResponse(); |
|
85
|
|
|
|
|
86
|
|
|
$name = isset($patch['name']) ? $patch['name'] : null; |
|
87
|
|
|
$value = isset($patch['value']) ? $patch['value'] : null; |
|
88
|
|
|
$checksum = isset($patch['checksum']) ? $patch['checksum'] : null; |
|
89
|
|
|
$parameters = isset($patch['parameters']) ? $patch['parameters'] : null; |
|
90
|
|
|
|
|
91
|
|
|
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); |
|
92
|
|
|
$contact = $addressBook->getChild($params['contactId']); |
|
93
|
|
|
|
|
94
|
|
|
if (!$contact) { |
|
95
|
|
|
return $response |
|
96
|
|
|
->setStatus(Http::STATUS_NOT_FOUND) |
|
97
|
|
|
->bailOut(App::$l10n->t('Couldn\'t find contact.')); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (!$name) { |
|
101
|
|
|
return $response |
|
102
|
|
|
->setStatus(Http::STATUS_PRECONDITION_FAILED) |
|
103
|
|
|
->bailOut(App::$l10n->t('Property name is not set.')); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (!$checksum && in_array($name, Properties::$multiProperties)) { |
|
107
|
|
|
return $response |
|
108
|
|
|
->setStatus(Http::STATUS_PRECONDITION_FAILED) |
|
109
|
|
|
->bailOut(App::$l10n->t('Property checksum is not set.')); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (is_array($value)) { |
|
113
|
|
|
// NOTE: Important, otherwise the compound value will be |
|
114
|
|
|
// set in the order the fields appear in the form! |
|
115
|
|
|
ksort($value); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$result = array('contactId' => $params['contactId']); |
|
119
|
|
|
|
|
120
|
|
|
if ($checksum && in_array($name, Properties::$multiProperties)) { |
|
121
|
|
|
try { |
|
122
|
|
|
if(is_null($value)) { |
|
123
|
|
|
$contact->unsetPropertyByChecksum($checksum); |
|
124
|
|
|
} else { |
|
125
|
|
|
$checksum = $contact->setPropertyByChecksum($checksum, $name, $value, $parameters); |
|
126
|
|
|
$result['checksum'] = $checksum; |
|
127
|
|
|
} |
|
128
|
|
|
} catch(\Exception $e) { |
|
129
|
|
|
return $response |
|
130
|
|
|
->setStatus(Http::STATUS_PRECONDITION_FAILED) |
|
131
|
|
|
->bailOut(App::$l10n->t('Information about vCard is incorrect. Please reload the page.')); |
|
132
|
|
|
} |
|
133
|
|
|
} elseif (!in_array($name, Properties::$multiProperties)) { |
|
134
|
|
|
|
|
135
|
|
|
if (is_null($value)) { |
|
136
|
|
|
unset($contact->{$name}); |
|
137
|
|
|
} else { |
|
138
|
|
|
|
|
139
|
|
|
if (!$contact->setPropertyByName($name, $value, $parameters)) { |
|
140
|
|
|
return $response |
|
141
|
|
|
->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR) |
|
142
|
|
|
->bailOut(App::$l10n->t('Error updating contact')); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (!$contact->save()) { |
|
149
|
|
|
return $response->bailOut(App::$l10n->t('Error saving contact to backend')); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$result['lastmodified'] = $contact->lastModified(); |
|
153
|
|
|
|
|
154
|
|
|
return $response->setData($result); |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|