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