1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-11 22:24:32 +01:00
OwncloudContactsOfficial/lib/controller/addressbookcontroller.php

330 lines
8.6 KiB
PHP
Raw Normal View History

2013-04-18 01:12:04 +02:00
<?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\Utils\JSONSerializer,
2013-09-17 18:46:59 +02:00
OCA\Contacts\Controller,
2013-05-24 20:35:23 +02:00
OCA\AppFramework\Http\TextDownloadResponse;
2013-04-18 01:12:04 +02:00
/**
* Controller class For Address Books
2013-04-18 01:12:04 +02:00
*/
2013-09-17 18:46:59 +02:00
class AddressBookController extends Controller {
2013-04-18 01:12:04 +02:00
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
2013-04-18 01:12:04 +02:00
*/
public function userAddressBooks() {
2013-09-17 18:46:59 +02:00
$addressBooks = $this->app->getAddressBooksForUser();
2013-04-18 01:12:04 +02:00
$response = array();
2013-09-10 07:17:05 +02:00
$lastModified = 0;
2013-04-18 01:12:04 +02:00
foreach($addressBooks as $addressBook) {
2013-09-10 07:17:05 +02:00
$data = $addressBook->getMetaData();
$response[] = $data;
if(!is_null($data['lastmodified'])) {
$lastModified = max($lastModified, $data['lastmodified']);
}
2013-04-18 01:12:04 +02:00
}
2013-09-10 07:17:05 +02:00
$response = new JSONResponse(array(
2013-04-18 01:12:04 +02:00
'addressbooks' => $response,
));
2013-09-10 07:17:05 +02:00
if($lastModified > 0) {
$response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null);
$response->setETag(md5($lastModified));
}
return $response;
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function getAddressBook() {
2013-05-24 20:35:23 +02:00
\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG);
$params = $this->request->urlParams;
2013-09-17 18:46:59 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
$lastModified = $addressBook->lastModified();
$response = new JSONResponse();
if(!is_null($lastModified)) {
2013-09-06 06:15:23 +02:00
//$response->addHeader('Cache-Control', 'private, must-revalidate');
$response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null);
$response->setETag(md5($lastModified));
}
2013-09-06 06:15:23 +02:00
$response->debug('method: ' . $this->request->method);
if($this->request->method === 'GET') {
$contacts = array();
foreach($addressBook->getChildren() as $i => $contact) {
$result = JSONSerializer::serializeContact($contact);
if($result !== null) {
$contacts[] = $result;
}
}
$response->setParams(array('contacts' => $contacts));
}
return $response;
}
2013-05-24 20:35:23 +02:00
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
* @NoCSRFRequired
2013-05-24 20:35:23 +02:00
*/
public function exportAddressBook() {
\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG);
$params = $this->request->urlParams;
2013-09-17 18:46:59 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
2013-05-24 20:35:23 +02:00
$lastModified = $addressBook->lastModified();
$response = new JSONResponse();
if(!is_null($lastModified)) {
$response->addHeader('Cache-Control', 'private, must-revalidate');
$response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null);
$response->setETag(md5($lastModified));
}
$contacts = '';
foreach($addressBook->getChildren() as $i => $contact) {
$contacts .= $contact->serialize() . "\r\n";
}
$name = str_replace(' ', '_', $addressBook->getDisplayName()) . '.vcf';
return new TextDownloadResponse($contacts, $name, 'text/directory');
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
* @NoCSRFRequired
*/
public function addAddressBook() {
$params = $this->request->urlParams;
$response = new JSONResponse();
2013-09-17 18:46:59 +02:00
$backend = $this->app->getBackend($params['backend']);
if(!$backend->hasAddressBookMethodFor(\OCP\PERMISSION_CREATE)) {
throw new \Exception('Not implemented');
}
try {
$id = $backend->createAddressBook($this->request->post);
} catch(Exception $e) {
$response->bailOut($e->getMessage());
return $response;
}
if($id === false) {
$response->bailOut(App::$l10n->t('Error creating address book'));
return $response;
}
$response->setStatus('201');
$response->setParams($backend->getAddressBook($id));
return $response;
}
2013-05-07 19:16:06 +02:00
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
2013-05-07 19:16:06 +02:00
*/
public function updateAddressBook() {
$params = $this->request->urlParams;
$response = new JSONResponse();
2013-09-17 18:46:59 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
try {
if(!$addressBook->update($this->request['properties'])) {
$response->bailOut(App::$l10n->t('Error updating address book'));
return $response;
}
} catch(Exception $e) {
$response->bailOut($e->getMessage());
return $response;
2013-05-07 19:16:06 +02:00
}
$response->setParams($addressBook->getMetaData());
2013-05-07 19:16:06 +02:00
return $response;
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function deleteAddressBook() {
$params = $this->request->urlParams;
$response = new JSONResponse();
2013-09-17 18:46:59 +02:00
$backend = $this->app->getBackend($params['backend']);
if(!$backend->hasAddressBookMethodFor(\OCP\PERMISSION_DELETE)) {
throw new \Exception(
'The "%s" backend does not support deleting address books', array($backend->name)
);
}
$addressBookInfo = $backend->getAddressBook($params['addressbookid']);
if(!$addressBookInfo['permissions'] & \OCP\PERMISSION_DELETE) {
$response->bailOut(App::$l10n->t(
'You do not have permissions to delete the "%s" address book'),
array($addressBookInfo['displayname']
));
return $response;
}
if(!$backend->deleteAddressBook($params['addressbookid'])) {
$response->bailOut(App::$l10n->t('Error deleting address book'));
return $response;
}
return $response;
2013-04-18 01:12:04 +02:00
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function activateAddressBook() {
$params = $this->request->urlParams;
$response = new JSONResponse();
2013-09-17 18:46:59 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
$addressBook->setActive($this->request->post['state']);
return $response;
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function addChild() {
$params = $this->request->urlParams;
$response = new JSONResponse();
2013-09-17 18:46:59 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
try {
$id = $addressBook->addChild();
} catch(Exception $e) {
$response->bailOut($e->getMessage());
return $response;
}
if($id === false) {
$response->bailOut(App::$l10n->t('Error creating contact.'));
return $response;
}
$contact = $addressBook->getChild($id);
$response->setStatus('201');
$response->setETag($contact->getETag());
$response->addHeader('Location',
\OCP\Util::linkToRoute(
'contacts_contact_get',
array(
'backend' => $params['backend'],
'addressbookid' => $params['addressbookid'],
'contactid' => $id
)
)
);
2013-05-04 22:48:30 +02:00
$response->setParams(JSONSerializer::serializeContact($contact));
return $response;
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function deleteChild() {
$params = $this->request->urlParams;
$response = new JSONResponse();
2013-09-17 18:46:59 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
try {
$result = $addressBook->deleteChild($params['contactid']);
} catch(Exception $e) {
$response->bailOut($e->getMessage());
return $response;
}
if($result === false) {
$response->bailOut(App::$l10n->t('Error deleting contact.'));
}
2013-09-03 14:08:12 +02:00
$response->setStatus('204');
return $response;
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
2013-09-03 14:08:12 +02:00
*/
public function deleteChildren() {
$params = $this->request->urlParams;
$response = new JSONResponse();
2013-09-17 18:46:59 +02:00
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
2013-09-03 14:08:12 +02:00
$contacts = $this->request->post['contacts'];
try {
$result = $addressBook->deleteChildren($contacts);
} catch(Exception $e) {
$response->bailOut($e->getMessage());
return $response;
}
$response->setParams(array('result' => $result));
return $response;
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function moveChild() {
$params = $this->request->urlParams;
$targetInfo = $this->request->post['target'];
$response = new JSONResponse();
// TODO: Check if the backend supports move (is 'local' or 'shared') and use that operation instead.
// If so, set status 204 and don't return the serialized contact.
2013-09-17 18:46:59 +02:00
$fromAddressBook = $this->app->getAddressBook($params['backend'], $params['addressbookid']);
$targetAddressBook = $this->app->getAddressBook($targetInfo['backend'], $targetInfo['id']);
$contact = $fromAddressBook->getChild($params['contactid']);
if(!$contact) {
$response->bailOut(App::$l10n->t('Error retrieving contact.'));
return $response;
}
try {
$contactid = $targetAddressBook->addChild($contact);
} catch(Exception $e) {
$response->bailOut($e->getMessage());
return $response;
}
$contact = $targetAddressBook->getChild($contactid);
if(!$contact) {
$response->bailOut(App::$l10n->t('Error saving contact.'));
return $response;
}
if(!$fromAddressBook->deleteChild($params['contactid'])) {
// Don't bail out because we have to return the contact
$response->debug(App::$l10n->t('Error removing contact from other address book.'));
}
$response->setParams(JSONSerializer::serializeContact($contact));
return $response;
}
2013-04-18 01:12:04 +02:00
}