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

199 lines
5.9 KiB
PHP
Raw Normal View History

<?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;
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,
OCP\AppFramework\Http\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() {
$request = $this->request;
$response = new JSONResponse();
2013-09-24 13:39:45 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
$contact = $addressBook->getChild($params['contactid']);
2013-05-09 05:58:28 +02:00
if(!$contact) {
$response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
return $response;
2013-05-09 05:58:28 +02:00
}
$data = JSONSerializer::serializeContact($contact);
$response->setParams($data);
return $response;
}
2013-05-24 20:35:23 +02:00
/**
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
*/
public function saveContact() {
$request = $this->request;
$params = $this->request->urlParams;
$response = new JSONResponse();
2013-09-24 13:39:45 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
$contact = $addressBook->getChild($params['contactid']);
if(!$contact) {
$response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
return $response;
}
if(!$contact->mergeFromArray($request->params)) {
$response->bailOut(App::$l10n->t('Error merging into contact.'));
return $response;
}
if(!$contact->save()) {
$response->bailOut(App::$l10n->t('Error saving contact to backend.'));
return $response;
}
$data = JSONSerializer::serializeContact($contact);
$response->setParams($data);
return $response;
}
/**
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
*/
public function deleteProperty() {
$request = $this->request;
2013-05-05 16:58:51 +02:00
$params = $request->urlParams;
$response = new JSONResponse();
$name = $request->post['name'];
$checksum = isset($request->post['checksum']) ? $request->post['checksum'] : null;
2013-04-30 22:39:46 +02:00
$response->debug(__METHOD__ . ', name: ' . print_r($name, true));
$response->debug(__METHOD__ . ', checksum: ' . print_r($checksum, true));
2013-09-24 13:39:45 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
$contact = $addressBook->getChild($params['contactid']);
if(!$contact) {
$response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
return $response;
}
if(!$name) {
$response->bailOut(App::$l10n->t('Property name is not set.'));
return $response;
}
if(!$checksum && in_array($name, Properties::$multi_properties)) {
$response->bailOut(App::$l10n->t('Property checksum is not set.'));
return $response;
}
if(!is_null($checksum)) {
try {
$contact->unsetPropertyByChecksum($checksum);
} catch(Exception $e) {
$response->bailOut(App::$l10n->t('Information about vCard is incorrect. Please reload the page.'));
return $response;
}
} else {
unset($contact->{$name});
}
if(!$contact->save()) {
$response->bailOut(App::$l10n->t('Error saving contact to backend.'));
return $response;
}
$response->setParams(array(
'backend' => $request->parameters['backend'],
'addressbookid' => $request->parameters['addressbookid'],
'contactid' => $request->parameters['contactid'],
'lastmodified' => $contact->lastModified(),
));
return $response;
}
/**
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
*/
public function saveProperty() {
$params = $this->request->urlParams;
2013-09-27 16:38:22 +02:00
$request = json_decode(file_get_contents('php://input'), true);
2013-09-27 16:38:22 +02:00
//$request = $requestData;
$response = new JSONResponse();
2013-09-27 16:38:22 +02:00
$response->debug(__METHOD__ .', upload_max_filesize: ' . ini_get('upload_max_filesize'));
2013-09-27 16:38:22 +02:00
$name = $request['name'];
$value = $request['value'];
$checksum = isset($request['checksum']) ? $request['checksum'] : null;
$parameters = isset($request['parameters']) ? $request['parameters'] : null;
2013-04-30 22:39:46 +02:00
$response->debug(__METHOD__ . ', name: ' . print_r($name, true));
$response->debug(__METHOD__ . ', value: ' . print_r($value, true));
$response->debug(__METHOD__ . ', checksum: ' . print_r($checksum, true));
$response->debug(__METHOD__ . ', parameters: ' . print_r($parameters, true));
2013-09-24 13:39:45 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
2013-09-27 16:38:22 +02:00
//$response->debug(__METHOD__ . ', addressBook: ' . print_r($addressBook, true));
$contact = $addressBook->getChild($params['contactid']);
if(!$contact) {
2013-09-27 16:38:22 +02:00
return $response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
}
if(!$name) {
2013-09-27 16:38:22 +02:00
return $response->bailOut(App::$l10n->t('Property name is not set.'));
}
if(!$checksum && in_array($name, Properties::$multi_properties)) {
2013-09-27 16:38:22 +02:00
return $response->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)) {
2013-09-27 16:38:22 +02:00
return $response->bailOut(App::$l10n->t('Property checksum is not set.'));
} elseif($checksum && in_array($name, Properties::$multi_properties)) {
try {
$checksum = $contact->setPropertyByChecksum($checksum, $name, $value, $parameters);
$result['checksum'] = $checksum;
} catch(Exception $e) {
2013-09-27 16:38:22 +02:00
return $response->bailOut(App::$l10n->t('Information about vCard is incorrect. Please reload the page.'));
}
} elseif(!in_array($name, Properties::$multi_properties)) {
if(!$contact->setPropertyByName($name, $value, $parameters)) {
2013-09-27 16:38:22 +02:00
return $response->bailOut(App::$l10n->t('Error setting property'));
}
}
if(!$contact->save()) {
2013-09-27 16:38:22 +02:00
return $response->bailOut(App::$l10n->t('Error saving property to backend'));
}
$result['lastmodified'] = $contact->lastModified();
2013-09-27 16:38:22 +02:00
return $response->setData($result);
}
}