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

@ -75,7 +75,7 @@ abstract class AbstractPIMCollection extends AbstractPIMObject implements \Itera
*/
public abstract function deleteChild($id, $options = array());
// Iterator methods
// Iterator methods
public function rewind() {
$this->counter = 0;

View File

@ -59,18 +59,21 @@ 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');
$this->backend = $backend;
$this->addressBookInfo = $addressBookInfo;
if(is_null($this->getId())) {
if (is_null($this->getId())) {
$id = $this->backend->createAddressBook($addressBookInfo);
if($id === false) {
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,22 +155,25 @@ 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)) {
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])) {
if (!isset($this->objects[$id])) {
$contact = $this->backend->getContact($this->getId(), $id);
if($contact) {
if ($contact) {
$this->objects[$id] = new Contact($this, $this->backend, $contact);
} else {
throw new \Exception(self::$l10n->t('Contact not found'), 404);
}
}
// When requesting a single contact we preparse it
if(isset($this->objects[$id])) {
if (isset($this->objects[$id])) {
$this->objects[$id]->retrieve();
return $this->objects[$id];
}
@ -189,20 +195,22 @@ class Addressbook extends AbstractPIMCollection {
* @return Contact[]
*/
public function getChildren($limit = null, $offset = null, $omitdata = false) {
if(!$this->hasPermission(\OCP\PERMISSION_READ)) {
if (!$this->hasPermission(\OCP\PERMISSION_READ)) {
throw new \Exception(self::$l10n->t('You do not have permissions to see these contacts'), 403);
}
$contacts = array();
$options = array('limit' => $limit, 'offset' => $offset, 'omitdata' => $omitdata);
foreach($this->backend->getContacts($this->getId(), $options) as $contact) {
foreach ($this->backend->getContacts($this->getId(), $options) as $contact) {
//\OCP\Util::writeLog('contacts', __METHOD__.' id: '.$contact['id'], \OCP\Util::DEBUG);
if(!isset($this->objects[$contact['id']])) {
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)) {
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)) {
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) {
if ($contact->save() === false) {
return false;
}
$id = $contact->getId();
if($this->count() !== null) {
if ($this->count() !== null) {
$this->_count += 1;
}
\OCP\Util::writeLog('contacts', __METHOD__.' id: '.$id, \OCP\Util::DEBUG);
return $id;
}
@ -243,21 +258,26 @@ class Addressbook extends AbstractPIMCollection {
* @throws \Exception on missing permissions
*/
public function deleteChild($id, $options = array()) {
if(!$this->hasPermission(\OCP\PERMISSION_DELETE)) {
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)) {
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])) {
if ($this->backend->deleteContact($this->getId(), $id, $options)) {
if (isset($this->objects[$id])) {
unset($this->objects[$id]);
}
if($this->count() !== null) {
if ($this->count() !== null) {
$this->_count -= 1;
}
return true;
}
return false;
}
@ -269,10 +289,11 @@ class Addressbook extends AbstractPIMCollection {
* @throws \Exception on missing permissions
*/
public function deleteChildren($ids) {
if(!$this->hasPermission(\OCP\PERMISSION_DELETE)) {
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)) {
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);
}
@ -282,9 +303,9 @@ class Addressbook extends AbstractPIMCollection {
array('id' => $ids)
);
foreach($ids as $id) {
foreach ($ids as $id) {
try {
if(!$this->deleteChild($id, array('isBatch' => true))) {
if (!$this->deleteChild($id, array('isBatch' => true))) {
\OCP\Util::writeLog(
'contacts', __METHOD__.' Error deleting contact: '
. $this->getBackend()->name . '::'
@ -318,9 +339,10 @@ class Addressbook extends AbstractPIMCollection {
* @return int|null
*/
public function count() {
if(!isset($this->_count)) {
if (!isset($this->_count)) {
$this->_count = $this->backend->numContacts($this->getId());
}
return $this->_count;
}
@ -332,17 +354,20 @@ class Addressbook extends AbstractPIMCollection {
* @return bool
*/
public function update(array $data) {
if(!$this->hasPermission(\OCP\PERMISSION_UPDATE)) {
if (!$this->hasPermission(\OCP\PERMISSION_UPDATE)) {
throw new \Exception('Access denied');
}
if(!$this->getBackend()->hasContactMethodFor(\OCP\PERMISSION_UPDATE)) {
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) {
if (count($data) === 0) {
return false;
}
foreach($data as $key => $value) {
switch($key) {
foreach ($data as $key => $value) {
switch ($key) {
case 'displayname':
$this->addressBookInfo['displayname'] = $value;
break;
@ -351,6 +376,7 @@ class Addressbook extends AbstractPIMCollection {
break;
}
}
return $this->backend->updateAddressBook($this->getId(), $data);
}
@ -360,9 +386,10 @@ class Addressbook extends AbstractPIMCollection {
* @return bool
*/
public function delete() {
if(!$this->hasPermission(\OCP\PERMISSION_DELETE)) {
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()) {
foreach ($this->getChildren() as $contact) {
if ($event = $contact->getBirthdayEvent()) {
$events[] = $event;
}
}
return $events;
}
}

View File

@ -97,13 +97,13 @@ class App {
* @return AddressBook[]
*/
public function getAddressBooksForUser() {
if(!self::$addressBooks) {
foreach(array_keys(self::$backendClasses) as $backendName) {
if (!self::$addressBooks) {
foreach (array_keys(self::$backendClasses) as $backendName) {
$backend = self::getBackend($backendName, $this->user);
$addressBooks = $backend->getAddressBooksForUser();
if($backendName === 'local' && count($addressBooks) === 0) {
if ($backendName === 'local' && count($addressBooks) === 0) {
$id = $backend->createAddressBook(array('displayname' => self::$l10n->t('Contacts')));
if($id !== false) {
if ($id !== false) {
$addressBook = $backend->getAddressBook($id);
$addressBooks = array($addressBook);
} else {
@ -113,13 +113,18 @@ class App {
\OCP\Util::ERROR
);
}
}
foreach($addressBooks as $addressBook) {
foreach ($addressBooks as $addressBook) {
$addressBook['backend'] = $backendName;
self::$addressBooks[] = new AddressBook($backend, $addressBook);
}
}
}
return self::$addressBooks;
}
@ -132,8 +137,8 @@ class App {
*/
public function getAddressBook($backendName, $addressbookid) {
//\OCP\Util::writeLog('contacts', __METHOD__ . ': '. $backendName . ', ' . $addressbookid, \OCP\Util::DEBUG);
foreach(self::$addressBooks as $addressBook) {
if($addressBook->getBackend()->name === $backendName
foreach (self::$addressBooks as $addressBook) {
if ($addressBook->getBackend()->name === $backendName
&& $addressBook->getId() === $addressbookid
) {
return $addressBook;
@ -142,9 +147,11 @@ class App {
$backend = self::getBackend($backendName, $this->user);
$info = $backend->getAddressBook($addressbookid);
if(!$info) {
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) {