1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-02 14:24:10 +01:00
OwncloudContactsOfficial/lib/controller/contactcontroller.php

160 lines
4.1 KiB
PHP
Raw Normal View History

<?php
/**
* @author Thomas Tanghus
2014-01-26 00:40:22 +01:00
* @copyright 2013-2014 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;
2013-05-24 20:35:23 +02:00
use OCA\Contacts\App,
OCA\Contacts\JSONResponse,
OCA\Contacts\ImageResponse,
OCA\Contacts\Utils\JSONSerializer,
OCA\Contacts\Utils\Properties,
2013-09-27 16:38:22 +02:00
OCA\Contacts\Controller,
2013-10-23 12:56:30 +02:00
OCP\AppFramework\Http;
/**
* Controller class For Contacts
*/
2013-09-17 18:46:59 +02:00
class ContactController extends Controller {
2013-05-09 05:58:28 +02:00
/**
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
2013-05-09 05:58:28 +02:00
*/
public function getContact() {
2014-03-09 15:03:21 +01:00
$params = $this->request->urlParams;
2013-05-09 05:58:28 +02:00
$response = new JSONResponse();
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
$contact = $addressBook->getChild($params['contactId']);
2013-05-09 05:58:28 +02:00
2014-03-08 16:22:51 +01:00
if (!$contact) {
2013-10-17 02:10:34 +02:00
return $response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
2013-05-09 05:58:28 +02:00
}
$data = JSONSerializer::serializeContact($contact);
2013-10-17 02:10:34 +02:00
return $response->setData($data);
2013-05-09 05:58:28 +02:00
}
2013-05-24 20:35:23 +02:00
/**
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
*/
public function saveContact() {
$params = $this->request->urlParams;
2014-03-09 15:03:21 +01:00
$data = isset($this->request->post['data']) ? $this->request->post['data'] : null;
$response = new JSONResponse();
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
$contact = $addressBook->getChild($params['contactId']);
2014-03-08 16:22:51 +01:00
if (!$data) {
2013-10-17 02:10:34 +02:00
return $response->bailOut(App::$l10n->t('No contact data in request.'));
}
2014-03-08 16:22:51 +01:00
if (!$contact) {
2013-10-17 02:10:34 +02:00
return $response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
}
2014-03-08 16:22:51 +01:00
if (!$contact->mergeFromArray($data)) {
2013-10-17 02:10:34 +02:00
return $response->bailOut(App::$l10n->t('Error merging into contact.'));
}
2014-03-08 16:22:51 +01:00
if (!$contact->save()) {
2013-10-17 02:10:34 +02:00
return $response->bailOut(App::$l10n->t('Error saving contact to backend.'));
}
2013-10-17 02:10:34 +02:00
return $response->setData(JSONSerializer::serializeContact($contact));
}
/**
2013-09-24 13:39:45 +02:00
* @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']);
2014-03-08 16:22:51 +01:00
if (!$contact) {
return $response
->setStatus(Http::STATUS_NOT_FOUND)
->bailOut(App::$l10n->t('Couldn\'t find contact.'));
}
2014-03-08 16:22:51 +01:00
if (!$name) {
return $response
->setStatus(Http::STATUS_PRECONDITION_FAILED)
->bailOut(App::$l10n->t('Property name is not set.'));
}
2014-03-08 16:22:51 +01:00
if (!$checksum && in_array($name, Properties::$multi_properties)) {
return $response
->setStatus(Http::STATUS_PRECONDITION_FAILED)
->bailOut(App::$l10n->t('Property checksum is not set.'));
}
2014-03-08 16:22:51 +01:00
if (is_array($value)) {
// NOTE: Important, otherwise the compound value will be
// set in the order the fields appear in the form!
ksort($value);
}
2014-03-08 16:22:51 +01:00
$result = array('contactId' => $params['contactId']);
2014-03-08 16:22:51 +01:00
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;
}
2014-03-09 15:03:21 +01:00
} catch(\Exception $e) {
return $response
->setStatus(Http::STATUS_PRECONDITION_FAILED)
->bailOut(App::$l10n->t('Information about vCard is incorrect. Please reload the page.'));
}
2014-03-08 16:22:51 +01:00
} elseif (!in_array($name, Properties::$multi_properties)) {
if (is_null($value)) {
unset($contact->{$name});
} else {
2014-03-08 16:22:51 +01:00
if (!$contact->setPropertyByName($name, $value, $parameters)) {
return $response
->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR)
->bailOut(App::$l10n->t('Error updating contact'));
}
2014-03-08 16:22:51 +01:00
}
}
2014-03-08 16:22:51 +01:00
if (!$contact->save()) {
return $response->bailOut(App::$l10n->t('Error saving contact to backend'));
}
2014-03-09 15:03:21 +01:00
$result['lastmodified'] = $contact->lastModified();
2013-09-27 16:38:22 +02:00
return $response->setData($result);
}
}