Blame view

sources/apps/contacts/lib/controller/contactcontroller.php 4.17 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
3
  <?php
  /**
   * @author Thomas Tanghus
6d9380f96   Cédric Dupont   Update sources OC...
4
   * @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
d1bafeea1   Kload   [fix] Upgrade to ...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   * 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() {
6d9380f96   Cédric Dupont   Update sources OC...
29
  		$params = $this->request->urlParams;
d1bafeea1   Kload   [fix] Upgrade to ...
30
31
32
33
  		$response = new JSONResponse();
  
  		$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
  		$contact = $addressBook->getChild($params['contactId']);
6d9380f96   Cédric Dupont   Update sources OC...
34
  		if (!$contact) {
d1bafeea1   Kload   [fix] Upgrade to ...
35
36
37
38
39
40
41
42
43
44
45
46
47
  			return $response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
  		}
  
  		$data = JSONSerializer::serializeContact($contact);
  
  		return $response->setData($data);
  
  	}
  
  	/**
  	 * @NoAdminRequired
  	 */
  	public function saveContact() {
d1bafeea1   Kload   [fix] Upgrade to ...
48
  		$params = $this->request->urlParams;
6d9380f96   Cédric Dupont   Update sources OC...
49
  		$data = isset($this->request->post['data']) ? $this->request->post['data'] : null;
d1bafeea1   Kload   [fix] Upgrade to ...
50
51
52
53
  		$response = new JSONResponse();
  
  		$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
  		$contact = $addressBook->getChild($params['contactId']);
6d9380f96   Cédric Dupont   Update sources OC...
54
  		if (!$data) {
d1bafeea1   Kload   [fix] Upgrade to ...
55
56
  			return $response->bailOut(App::$l10n->t('No contact data in request.'));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
57
  		if (!$contact) {
d1bafeea1   Kload   [fix] Upgrade to ...
58
59
  			return $response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
60
  		if (!$contact->mergeFromArray($data)) {
d1bafeea1   Kload   [fix] Upgrade to ...
61
62
  			return $response->bailOut(App::$l10n->t('Error merging into contact.'));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
63
64
  
  		if (!$contact->save()) {
d1bafeea1   Kload   [fix] Upgrade to ...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  			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();
6d9380f96   Cédric Dupont   Update sources OC...
79
80
  		$name = isset($patch['name']) ? $patch['name'] : null;
  		$value = isset($patch['value']) ? $patch['value'] : null;
d1bafeea1   Kload   [fix] Upgrade to ...
81
82
83
84
85
  		$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']);
6d9380f96   Cédric Dupont   Update sources OC...
86
  		if (!$contact) {
d1bafeea1   Kload   [fix] Upgrade to ...
87
88
89
90
  			return $response
  				->setStatus(Http::STATUS_NOT_FOUND)
  				->bailOut(App::$l10n->t('Couldn\'t find contact.'));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
91
92
  
  		if (!$name) {
d1bafeea1   Kload   [fix] Upgrade to ...
93
94
95
96
  			return $response
  				->setStatus(Http::STATUS_PRECONDITION_FAILED)
  				->bailOut(App::$l10n->t('Property name is not set.'));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
97
98
  
  		if (!$checksum && in_array($name, Properties::$multiProperties)) {
d1bafeea1   Kload   [fix] Upgrade to ...
99
100
101
102
  			return $response
  				->setStatus(Http::STATUS_PRECONDITION_FAILED)
  				->bailOut(App::$l10n->t('Property checksum is not set.'));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
103
104
  
  		if (is_array($value)) {
d1bafeea1   Kload   [fix] Upgrade to ...
105
106
107
108
  			// NOTE: Important, otherwise the compound value will be
  			// set in the order the fields appear in the form!
  			ksort($value);
  		}
6d9380f96   Cédric Dupont   Update sources OC...
109

d1bafeea1   Kload   [fix] Upgrade to ...
110
  		$result = array('contactId' => $params['contactId']);
6d9380f96   Cédric Dupont   Update sources OC...
111
112
  
  		if ($checksum && in_array($name, Properties::$multiProperties)) {
d1bafeea1   Kload   [fix] Upgrade to ...
113
114
115
116
117
118
119
  			try {
  				if(is_null($value)) {
  					$contact->unsetPropertyByChecksum($checksum);
  				} else {
  					$checksum = $contact->setPropertyByChecksum($checksum, $name, $value, $parameters);
  					$result['checksum'] = $checksum;
  				}
6d9380f96   Cédric Dupont   Update sources OC...
120
  			} catch(\Exception $e)	{
d1bafeea1   Kload   [fix] Upgrade to ...
121
122
123
124
  				return $response
  					->setStatus(Http::STATUS_PRECONDITION_FAILED)
  					->bailOut(App::$l10n->t('Information about vCard is incorrect. Please reload the page.'));
  			}
6d9380f96   Cédric Dupont   Update sources OC...
125
126
127
  		} elseif (!in_array($name, Properties::$multiProperties)) {
  
  			if (is_null($value)) {
d1bafeea1   Kload   [fix] Upgrade to ...
128
129
  				unset($contact->{$name});
  			} else {
6d9380f96   Cédric Dupont   Update sources OC...
130
131
  
  				if (!$contact->setPropertyByName($name, $value, $parameters)) {
d1bafeea1   Kload   [fix] Upgrade to ...
132
133
134
135
  					return $response
  						->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR)
  						->bailOut(App::$l10n->t('Error updating contact'));
  				}
6d9380f96   Cédric Dupont   Update sources OC...
136

d1bafeea1   Kload   [fix] Upgrade to ...
137
138
  			}
  		}
6d9380f96   Cédric Dupont   Update sources OC...
139
140
  
  		if (!$contact->save()) {
d1bafeea1   Kload   [fix] Upgrade to ...
141
142
  			return $response->bailOut(App::$l10n->t('Error saving contact to backend'));
  		}
6d9380f96   Cédric Dupont   Update sources OC...
143

d1bafeea1   Kload   [fix] Upgrade to ...
144
145
146
147
148
149
150
  		$result['lastmodified'] = $contact->lastModified();
  
  		return $response->setData($result);
  
  	}
  
  }