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-04-19 09:59:30 +02:00
|
|
|
use OCA\Contacts\App;
|
|
|
|
use OCA\Contacts\JSONResponse;
|
|
|
|
use OCA\Contacts\Utils\JSONSerializer;
|
|
|
|
//use OCA\Contacts\Request;
|
2013-04-24 23:36:54 +02:00
|
|
|
use OCA\AppFramework\Controller\Controller as BaseController;
|
2013-04-18 01:12:04 +02:00
|
|
|
use OCA\AppFramework\Core\API;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-24 23:36:54 +02:00
|
|
|
* Controller class For Address Books
|
2013-04-18 01:12:04 +02:00
|
|
|
*/
|
|
|
|
class AddressBookController extends BaseController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function userAddressBooks() {
|
2013-04-25 04:21:14 +02:00
|
|
|
$app = new App($this->api->getUserId());
|
2013-04-18 01:12:04 +02:00
|
|
|
$addressBooks = $app->getAddressBooksForUser();
|
|
|
|
$response = array();
|
|
|
|
foreach($addressBooks as $addressBook) {
|
|
|
|
$response[] = $addressBook->getMetaData();
|
|
|
|
}
|
2013-04-24 23:36:54 +02:00
|
|
|
$response = new JSONResponse(
|
|
|
|
array(
|
2013-04-18 01:12:04 +02:00
|
|
|
'addressbooks' => $response,
|
2013-04-19 09:59:30 +02:00
|
|
|
));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function getAddressBook() {
|
|
|
|
$params = $this->request->urlParams;
|
2013-04-25 04:21:14 +02:00
|
|
|
$app = new App($this->api->getUserId());
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
$addressBook = $app->getAddressBook($params['backend'], $params['addressbookid']);
|
|
|
|
$lastModified = $addressBook->lastModified();
|
|
|
|
$response = new JSONResponse();
|
|
|
|
|
|
|
|
if(!is_null($lastModified)) {
|
2013-04-30 19:58:54 +02:00
|
|
|
$response->addHeader('Cache-Control', 'private, must-revalidate');
|
2013-04-24 23:36:54 +02:00
|
|
|
$response->setLastModified(\DateTime::createFromFormat('U', $lastModified));
|
|
|
|
$response->setETag(md5($lastModified));
|
2013-04-19 09:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$contacts = array();
|
|
|
|
foreach($addressBook->getChildren() as $i => $contact) {
|
|
|
|
$result = JSONSerializer::serializeContact($contact);
|
2013-04-24 23:36:54 +02:00
|
|
|
//\OCP\Util::writeLog('contacts', __METHOD__.' contact: '.print_r($result, true), \OCP\Util::DEBUG);
|
2013-04-19 09:59:30 +02:00
|
|
|
if($result !== null) {
|
|
|
|
$contacts[] = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$response->setParams(array(
|
|
|
|
'contacts' => $contacts,
|
|
|
|
));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function addAddressBook() {
|
2013-04-25 04:21:14 +02:00
|
|
|
$app = new App($this->api->getUserId());
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
$response = new JSONResponse();
|
|
|
|
|
2013-04-25 04:21:14 +02:00
|
|
|
$backend = App::getBackend('local', $this->api->getUserId());
|
2013-05-05 22:23:11 +02:00
|
|
|
// TODO: Check actual permissions
|
|
|
|
if(!$backend->hasAddressBookMethodFor(\OCP\PERMISSION_CREATE)) {
|
|
|
|
throw new \Exception('Not implemented');
|
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
$id = $backend->createAddressBook($this->request->post);
|
|
|
|
if($id === false) {
|
|
|
|
$response->bailOut(App::$l10n->t('Error creating address book'));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->setParams($backend->getAddressBook($id));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function deleteAddressBook() {
|
|
|
|
$params = $this->request->urlParams;
|
2013-04-25 04:21:14 +02:00
|
|
|
$app = new App($this->api->getUserId());
|
2013-04-19 09:59:30 +02:00
|
|
|
|
|
|
|
$response = new JSONResponse();
|
|
|
|
|
2013-04-25 04:21:14 +02:00
|
|
|
$backend = App::getBackend('local', $this->api->getUserId());
|
2013-05-05 22:23:11 +02:00
|
|
|
// TODO: Check actual permissions
|
|
|
|
if(!$backend->hasAddressBookMethodFor(\OCP\PERMISSION_DELETE)) {
|
|
|
|
throw new \Exception('Not implemented');
|
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
if(!$backend->deleteAddressBook($params['addressbookid'])) {
|
|
|
|
$response->bailOut(App::$l10n->t('Error deleting address book'));
|
|
|
|
}
|
|
|
|
return $response;
|
2013-04-18 01:12:04 +02:00
|
|
|
}
|
2013-04-24 23:36:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function addChild() {
|
|
|
|
$params = $this->request->urlParams;
|
2013-04-25 04:21:14 +02:00
|
|
|
$app = new App($this->api->getUserId());
|
2013-04-24 23:36:54 +02:00
|
|
|
|
|
|
|
$response = new JSONResponse();
|
|
|
|
|
|
|
|
$addressBook = $app->getAddressBook($params['backend'], $params['addressbookid']);
|
|
|
|
$id = $addressBook->addChild();
|
|
|
|
if($id === false) {
|
|
|
|
$response->bailOut(App::$l10n->t('Error creating contact.'));
|
|
|
|
}
|
|
|
|
$contact = $addressBook->getChild($id);
|
2013-05-04 22:48:30 +02:00
|
|
|
$response->setParams(JSONSerializer::serializeContact($contact));
|
2013-04-24 23:36:54 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function deleteChild() {
|
|
|
|
$params = $this->request->urlParams;
|
2013-04-25 04:21:14 +02:00
|
|
|
$app = new App($this->api->getUserId());
|
2013-04-24 23:36:54 +02:00
|
|
|
|
|
|
|
$response = new JSONResponse();
|
|
|
|
|
|
|
|
$addressBook = $app->getAddressBook($params['backend'], $params['addressbookid']);
|
|
|
|
$result = $addressBook->deleteChild($params['contactid']);
|
|
|
|
if($result === false) {
|
|
|
|
$response->bailOut(App::$l10n->t('Error deleting contact.'));
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2013-05-06 01:49:10 +02:00
|
|
|
/**
|
|
|
|
* @IsAdminExemption
|
|
|
|
* @IsSubAdminExemption
|
|
|
|
* @Ajax
|
|
|
|
*/
|
|
|
|
public function moveChild() {
|
|
|
|
$params = $this->request->urlParams;
|
|
|
|
$targetInfo = $this->request->post['target'];
|
|
|
|
$app = new App($this->api->getUserId());
|
|
|
|
|
|
|
|
$response = new JSONResponse();
|
|
|
|
|
|
|
|
$fromAddressBook = $app->getAddressBook($params['backend'], $params['addressbookid']);
|
|
|
|
$targetAddressBook = $app->getAddressBook($targetInfo['backend'], $targetInfo['id']);
|
|
|
|
$contact = $fromAddressBook->getChild($params['contactid']);
|
|
|
|
if(!$contact) {
|
|
|
|
$response->bailOut(App::$l10n->t('Error retrieving contact.'));
|
|
|
|
}
|
|
|
|
$contactid = $targetAddressBook->addChild($contact);
|
|
|
|
$contact = $targetAddressBook->getChild($contactid);
|
|
|
|
if(!$contact) {
|
|
|
|
$response->bailOut(App::$l10n->t('Error saving contact.'));
|
|
|
|
}
|
|
|
|
$result = $fromAddressBook->deleteChild($params['contactid']);
|
|
|
|
if($result === false) {
|
|
|
|
// 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-24 23:36:54 +02:00
|
|
|
|
2013-04-18 01:12:04 +02:00
|
|
|
}
|
2013-04-19 09:59:30 +02:00
|
|
|
|