1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-29 11:24:11 +01:00

More fixes

This commit is contained in:
Thomas Tanghus 2014-03-08 19:09:17 +01:00
parent b670a85387
commit d724149c56
6 changed files with 77 additions and 46 deletions

View File

@ -59,6 +59,7 @@ class Addressbook extends AbstractPIMCollection {
/**
* @param AbstractBackend $backend The storage backend
* @param array $addressBookInfo
* @throws \Exception
*/
public function __construct(Backend\AbstractBackend $backend, array $addressBookInfo) {
self::$l10n = \OCP\Util::getL10N('contacts');
@ -69,8 +70,10 @@ class Addressbook extends AbstractPIMCollection {
if ($id === false) {
throw new \Exception('Error creating address book.', 500);
}
$this->addressBookInfo = $this->backend->getAddressBook($id);
}
//\OCP\Util::writeLog('contacts', __METHOD__.' backend: ' . print_r($this->backend, true), \OCP\Util::DEBUG);
}
@ -152,12 +155,14 @@ class Addressbook extends AbstractPIMCollection {
*
* @param string $id
* @return Contact|null
* @throws \Exception On not found
*/
public function getChild($id) {
//\OCP\Util::writeLog('contacts', __METHOD__.' id: '.$id, \OCP\Util::DEBUG);
if (!$this->hasPermission(\OCP\PERMISSION_READ)) {
throw new \Exception(self::$l10n->t('You do not have permissions to see this contacts'), 403);
}
if (!isset($this->objects[$id])) {
$contact = $this->backend->getContact($this->getId(), $id);
if ($contact) {
@ -166,6 +171,7 @@ class Addressbook extends AbstractPIMCollection {
throw new \Exception(self::$l10n->t('Contact not found'), 404);
}
}
// When requesting a single contact we preparse it
if (isset($this->objects[$id])) {
$this->objects[$id]->retrieve();
@ -201,8 +207,10 @@ class Addressbook extends AbstractPIMCollection {
if (!isset($this->objects[$contact['id']])) {
$this->objects[$contact['id']] = new Contact($this, $this->backend, $contact);
}
$contacts[] = $this->objects[$contact['id']];
}
//\OCP\Util::writeLog('contacts', __METHOD__.' children: '.count($contacts), \OCP\Util::DEBUG);
return $contacts;
}
@ -214,22 +222,29 @@ class Addressbook extends AbstractPIMCollection {
*
* @param array|VObject\VCard $data
* @return int|bool
* @throws \Exception on missing permissions
*/
public function addChild($data = null) {
if (!$this->hasPermission(\OCP\PERMISSION_CREATE)) {
throw new \Exception(self::$l10n->t('You do not have permissions add contacts to the address book'), 403);
}
if (!$this->getBackend()->hasContactMethodFor(\OCP\PERMISSION_CREATE)) {
throw new \Exception(self::$l10n->t('The backend for this address book does not support adding contacts'), 501);
}
$contact = new Contact($this, $this->backend, $data);
if ($contact->save() === false) {
return false;
}
$id = $contact->getId();
if ($this->count() !== null) {
$this->_count += 1;
}
\OCP\Util::writeLog('contacts', __METHOD__.' id: '.$id, \OCP\Util::DEBUG);
return $id;
}
@ -246,18 +261,23 @@ class Addressbook extends AbstractPIMCollection {
if (!$this->hasPermission(\OCP\PERMISSION_DELETE)) {
throw new \Exception(self::$l10n->t('You do not have permissions to delete this contact'), 403);
}
if (!$this->getBackend()->hasContactMethodFor(\OCP\PERMISSION_DELETE)) {
throw new \Exception(self::$l10n->t('The backend for this address book does not support deleting contacts'), 501);
}
if ($this->backend->deleteContact($this->getId(), $id, $options)) {
if (isset($this->objects[$id])) {
unset($this->objects[$id]);
}
if ($this->count() !== null) {
$this->_count -= 1;
}
return true;
}
return false;
}
@ -272,6 +292,7 @@ class Addressbook extends AbstractPIMCollection {
if (!$this->hasPermission(\OCP\PERMISSION_DELETE)) {
throw new \Exception(self::$l10n->t('You do not have permissions to delete this contact'), 403);
}
if (!$this->getBackend()->hasContactMethodFor(\OCP\PERMISSION_DELETE)) {
throw new \Exception(self::$l10n->t('The backend for this address book does not support deleting contacts'), 501);
}
@ -321,6 +342,7 @@ class Addressbook extends AbstractPIMCollection {
if (!isset($this->_count)) {
$this->_count = $this->backend->numContacts($this->getId());
}
return $this->_count;
}
@ -335,12 +357,15 @@ class Addressbook extends AbstractPIMCollection {
if (!$this->hasPermission(\OCP\PERMISSION_UPDATE)) {
throw new \Exception('Access denied');
}
if (!$this->getBackend()->hasContactMethodFor(\OCP\PERMISSION_UPDATE)) {
throw new \Exception(self::$l10n->t('The backend for this address book does not support updating'), 501);
}
if (count($data) === 0) {
return false;
}
foreach ($data as $key => $value) {
switch ($key) {
case 'displayname':
@ -351,6 +376,7 @@ class Addressbook extends AbstractPIMCollection {
break;
}
}
return $this->backend->updateAddressBook($this->getId(), $data);
}
@ -363,6 +389,7 @@ class Addressbook extends AbstractPIMCollection {
if (!$this->hasPermission(\OCP\PERMISSION_DELETE)) {
throw new Exception(self::$l10n->t('You don\'t have permissions to delete the address book.'), 403);
}
return $this->backend->deleteAddressBook($this->getId());
}
@ -386,11 +413,13 @@ class Addressbook extends AbstractPIMCollection {
public function getBirthdayEvents() {
$events = array();
foreach ($this->getChildren() as $contact) {
if ($event = $contact->getBirthdayEvent()) {
$events[] = $event;
}
}
return $events;
}
}

View File

@ -113,13 +113,18 @@ class App {
\OCP\Util::ERROR
);
}
}
foreach ($addressBooks as $addressBook) {
$addressBook['backend'] = $backendName;
self::$addressBooks[] = new AddressBook($backend, $addressBook);
}
}
}
return self::$addressBooks;
}
@ -142,9 +147,11 @@ class App {
$backend = self::getBackend($backendName, $this->user);
$info = $backend->getAddressBook($addressbookid);
if (!$info) {
throw new \Exception(self::$l10n->t('Address book not found'), 404);
}
$addressBook = new AddressBook($backend, $info);
self::$addressBooks[] = $addressBook;
return $addressBook;

View File

@ -94,7 +94,6 @@ abstract class AbstractBackend {
$permissions = 0;
foreach ($this->possibleContactPermissions as $permission => $methodName) {
if(method_exists($this, $methodName)) {
$permissions |= $permission;
}
@ -117,7 +116,6 @@ abstract class AbstractBackend {
$permissions = 0;
foreach ($this->possibleAddressBookPermissions as $permission => $methodName) {
if (method_exists($this, $methodName)) {
$permissions |= $permission;
}

View File

@ -159,7 +159,6 @@ class Database extends AbstractBackend {
return null;
}
return null;
}
public function hasAddressBook($addressbookid, array $options = array()) {
@ -190,7 +189,6 @@ class Database extends AbstractBackend {
$updates = array();
if (isset($changes['displayname'])) {
$query .= '`displayname` = ?, ';
$updates[] = $changes['displayname'];
@ -331,7 +329,6 @@ class Database extends AbstractBackend {
}
if (!is_null($result)) {
while ($id = $result->fetchOne()) {
$ids[] = $id;
}

View File

@ -53,7 +53,7 @@ class Contact extends VObject\VCard implements IPIMObject {
* Create a new Contact object
*
* @param AddressBook $parent
* @param AbstractBackend $backend
* @param Backend\AbstractBackend $backend
* @param mixed $data
*/
public function __construct($parent, $backend, $data = null) {