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

Contacts: Adjustments to backends.

This commit is contained in:
Thomas Tanghus 2013-03-12 20:00:22 +01:00
parent 152386946e
commit fccea3347c
6 changed files with 81 additions and 357 deletions

View File

@ -23,7 +23,7 @@ OC::$CLASSPATH['OCA\Contacts\CardDAV\AddressBook'] = 'contacts/lib/carddav/addre
OC::$CLASSPATH['OCA\Contacts\CardDAV\Card'] = 'contacts/lib/carddav/card.php';
OC::$CLASSPATH['OCA\Contacts\SearchProvider'] = 'contacts/lib/search.php';
//require_once __DIR__ . '/../lib/abstractobject.php';
//require_once __DIR__ . '/../lib/contact.php';
//require_once __DIR__ . '/../lib/backend/database.php';
Sabre\VObject\Component::$classMap['VCARD'] = 'OCA\Contacts\Contact';
Sabre\VObject\Property::$classMap['FN'] = 'OCA\Contacts\VObject\StringProperty';
@ -53,6 +53,6 @@ OC_Search::registerProvider('OCA\Contacts\SearchProvider');
OCP\Share::registerBackend('contact', 'OCA\Contacts\Share_Backend_Contact');
OCP\Share::registerBackend('addressbook', 'OCA\Contacts\Share_Backend_Addressbook', 'contact');
foreach(OCA\Contacts\Addressbook::all(OCP\USER::getUser()) as $addressbook) {
/*foreach(OCA\Contacts\Addressbook::all(OCP\USER::getUser()) as $addressbook) {
OCP\Contacts::registerAddressBook(new OCA\Contacts\AddressbookProvider($addressbook['id']));
}
}*/

View File

@ -120,7 +120,10 @@ class Addressbook extends PIMCollectionAbstract {
$contacts = array();
foreach($this->backend->getContacts($this->getId(), $limit, $offset, $omitdata) as $contact) {
$this->objects[$contact['id']] = new Contact($this, $this->backend, $contact);
if(!isset($this->objects[$contact['id']])) {
$this->objects[$contact['id']] = new Contact($this, $this->backend, $contact);
}
$contacts[] = $this->objects[$contact['id']];
}
return $contacts;
}
@ -158,332 +161,6 @@ class Addressbook extends PIMCollectionAbstract {
return $this->backend->lastModifiedAddressBook($this->getId());
}
/**
* @brief Returns the list of addressbooks for a specific user.
* @param string $uid
* @param boolean $active Only return addressbooks with this $active state, default(=false) is don't care
* @param boolean $shared Whether to also return shared addressbook. Defaults to true.
* @return array or false.
*/
public static function all($uid, $active = false, $shared = true) {
$values = array($uid);
$active_where = '';
if ($active) {
$active_where = ' AND `active` = ?';
$values[] = 1;
}
try {
$stmt = \OCP\DB::prepare( 'SELECT * FROM `*PREFIX*contacts_addressbooks` WHERE `userid` = ? ' . $active_where . ' ORDER BY `displayname`' );
$result = $stmt->execute($values);
if (\OC_DB::isError($result)) {
\OCP\Util::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return false;
}
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.' exception: '.$e->getMessage(), \OCP\Util::ERROR);
\OCP\Util::writeLog('contacts', __METHOD__.' uid: '.$uid, \OCP\Util::DEBUG);
return false;
}
$addressbooks = array();
while( $row = $result->fetchRow()) {
$row['permissions'] = \OCP\PERMISSION_ALL;
$addressbooks[] = $row;
}
if($shared === true) {
$addressbooks = array_merge($addressbooks, \OCP\Share::getItemsSharedWith('addressbook', Share_Backend_Addressbook::FORMAT_ADDRESSBOOKS));
}
if(!$active && !count($addressbooks)) {
$id = self::addDefault($uid);
return array(self::find($id),);
}
return $addressbooks;
}
/**
* @brief Get active addressbook IDs for a user.
* @param integer $uid User id. If null current user will be used.
* @return array
*/
public static function activeIds($uid = null) {
if(is_null($uid)) {
$uid = \OCP\USER::getUser();
}
// query all addressbooks to force creation of default if it desn't exist.
$activeaddressbooks = self::all($uid);
$ids = array();
foreach($activeaddressbooks as $addressbook) {
if($addressbook['active']) {
$ids[] = $addressbook['id'];
}
}
return $ids;
}
/**
* @brief Returns the list of active addressbooks for a specific user.
* @param string $uid
* @return array
*/
public static function active($uid) {
return self::all($uid, true);
}
/**
* @brief Gets the data of one address book
* @param integer $id
* @return associative array or false.
*/
public static function find($id) {
try {
$stmt = \OCP\DB::prepare( 'SELECT * FROM `*PREFIX*contacts_addressbooks` WHERE `id` = ?' );
$result = $stmt->execute(array($id));
if (\OC_DB::isError($result)) {
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
return false;
}
} catch(Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.', exception: ' . $e->getMessage(), \OCP\Util::ERROR);
\OCP\Util::writeLog('contacts', __METHOD__.', id: ' . $id, \OCP\Util::DEBUG);
return false;
}
$row = $result->fetchRow();
if($row['userid'] != \OCP\USER::getUser() && !\OC_Group::inGroup(\OCP\User::getUser(), 'admin')) {
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $id);
if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & \OCP\PERMISSION_READ)) {
throw new Exception(
App::$l10n->t(
'You do not have the permissions to read this addressbook.'
)
);
}
$row['permissions'] = $sharedAddressbook['permissions'];
} else {
$row['permissions'] = \OCP\PERMISSION_ALL;
}
return $row;
}
/**
* @brief Adds default address book
* @return $id ID of the newly created addressbook or false on error.
*/
public static function addDefault($uid = null) {
if(is_null($uid)) {
$uid = \OCP\USER::getUser();
}
$id = self::add($uid, 'Contacts', 'Default Address Book');
if($id !== false) {
self::setActive($id, true);
}
return $id;
}
/**
* @brief Creates a new address book
* @param string $userid
* @param string $name
* @param string $description
* @return insertid
*/
public static function add($uid,$name,$description='') {
try {
$stmt = \OCP\DB::prepare( 'SELECT `uri` FROM `*PREFIX*contacts_addressbooks` WHERE `userid` = ? ' );
$result = $stmt->execute(array($uid));
if (\OC_DB::isError($result)) {
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
return false;
}
} catch(Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__ . ' exception: ' . $e->getMessage(), \OCP\Util::ERROR);
\OCP\Util::writeLog('contacts', __METHOD__ . ' uid: ' . $uid, \OCP\Util::DEBUG);
return false;
}
$uris = array();
while($row = $result->fetchRow()) {
$uris[] = $row['uri'];
}
$uri = self::createURI($name, $uris );
try {
$stmt = \OCP\DB::prepare( 'INSERT INTO `*PREFIX*contacts_addressbooks` (`userid`,`displayname`,`uri`,`description`,`ctag`) VALUES(?,?,?,?,?)' );
$result = $stmt->execute(array($uid,$name,$uri,$description,1));
if (\OC_DB::isError($result)) {
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
return false;
}
} catch(Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
\OCP\Util::writeLog('contacts', __METHOD__.', uid: '.$uid, \OCP\Util::DEBUG);
return false;
}
return \OCP\DB::insertid('*PREFIX*contacts_addressbooks');
}
/**
* @brief Edits an addressbook
* @param integer $id
* @param string $name
* @param string $description
* @return boolean
*/
public static function edit($id,$name,$description) {
// Need these ones for checking uri
$addressbook = self::find($id);
if ($addressbook['userid'] != \OCP\User::getUser() && !\OC_Group::inGroup(OCP\User::getUser(), 'admin')) {
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $id);
if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & \OCP\PERMISSION_UPDATE)) {
throw new \Exception(
App::$l10n->t(
'You do not have the permissions to update this addressbook.'
)
);
}
}
if(is_null($name)) {
$name = $addressbook['name'];
}
if(is_null($description)) {
$description = $addressbook['description'];
}
try {
$stmt = \OCP\DB::prepare('UPDATE `*PREFIX*contacts_addressbooks` SET `displayname`=?,`description`=?, `ctag`=`ctag`+1 WHERE `id`=?');
$result = $stmt->execute(array($name,$description,$id));
if (\OC_DB::isError($result)) {
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
throw new Exception(
App::$l10n->t(
'There was an error updating the addressbook.'
)
);
}
} catch(Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__ . ', exception: ' . $e->getMessage(), \OCP\Util::ERROR);
\OCP\Util::writeLog('contacts', __METHOD__ . ', id: ' . $id, \OCP\Util::DEBUG);
throw new Exception(
App::$l10n->t(
'There was an error updating the addressbook.'
)
);
}
return true;
}
/**
* @brief Activates an addressbook
* @param integer $id
* @param boolean $active
* @return boolean
*/
public static function setActive($id,$active) {
$sql = 'UPDATE `*PREFIX*contacts_addressbooks` SET `active` = ? WHERE `id` = ?';
try {
$stmt = \OCP\DB::prepare($sql);
$stmt->execute(array(intval($active), $id));
return true;
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__ . ', exception for ' . $id.': ' . $e->getMessage(), \OCP\Util::ERROR);
return false;
}
}
/**
* @brief Checks if an addressbook is active.
* @param integer $id ID of the address book.
* @return boolean
*/
public static function isActive($id) {
$sql = 'SELECT `active` FROM `*PREFIX*contacts_addressbooks` WHERE `id` = ?';
try {
$stmt = \OCP\DB::prepare( $sql );
$result = $stmt->execute(array($id));
if (\OC_DB::isError($result)) {
\OC_Log::write('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
return false;
}
$row = $result->fetchRow();
return (bool)$row['active'];
} catch(Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.', exception: ' . $e->getMessage(), \OCP\Util::ERROR);
return false;
}
}
/**
* @brief removes an address book
* @param integer $id
* @return boolean true on success, otherwise an exception will be thrown
*/
public function delete() {
$addressbook = self::find($id);
if ($addressbook['userid'] != \OCP\User::getUser() && !\OC_Group::inGroup(\OCP\User::getUser(), 'admin')) {
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $id);
if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & \OCP\PERMISSION_DELETE)) {
throw new Exception(
App::$l10n->t(
'You do not have the permissions to delete this addressbook.'
)
);
}
}
// First delete cards belonging to this addressbook.
$cards = VCard::all($id);
foreach($cards as $card) {
try {
VCard::delete($card['id']);
} catch(Exception $e) {
\OCP\Util::writeLog('contacts',
__METHOD__.', exception deleting vCard '.$card['id'].': '
. $e->getMessage(),
\OCP\Util::ERROR);
}
}
try {
$stmt = \OCP\DB::prepare('DELETE FROM `*PREFIX*contacts_addressbooks` WHERE `id` = ?');
$stmt->execute(array($id));
} catch(\Exception $e) {
\OCP\Util::writeLog('contacts',
__METHOD__.', exception for ' . $id . ': '
. $e->getMessage(),
\OCP\Util::ERROR);
throw new Exception(
App::$l10n->t(
'There was an error deleting this addressbook.'
)
);
}
\OCP\Share::unshareAll('addressbook', $id);
if(count(self::all(\OCP\User::getUser())) == 0) {
self::addDefault();
}
return true;
}
/**
* @brief Updates ctag for addressbook
* @param integer $id
* @return boolean
*/
public static function touch($id) {
$stmt = \OCP\DB::prepare( 'UPDATE `*PREFIX*contacts_addressbooks` SET `ctag` = `ctag` + 1 WHERE `id` = ?' );
$stmt->execute(array($id));
return true;
}
/**
* @brief Creates a URI for Addressbook
* @param string $name name of the addressbook

View File

@ -40,7 +40,7 @@ namespace OCA\Contacts;
/**
* This class manages our addressbooks.
*/
class Addressbook {
class AddressbookLegacy {
/**
* @brief Returns the list of addressbooks for a specific user.
* @param string $uid

View File

@ -143,7 +143,7 @@ abstract class AbstractBackend {
* @param mixed $id
* @return array
*/
public function getContact($addressbookid, $id, $omitdata = false) {
public function getContact($addressbookid, $id) {
}
/**

View File

@ -60,11 +60,14 @@ class Shared extends Database {
* @return mixed
*/
public function getAddressBook($addressbookid) {
return \OCP\Share::getItemSharedWithBySource(
$addressbook = \OCP\Share::getItemSharedWithBySource(
'addressbook',
$addressbookid,
Contacts\Share_Backend_Addressbook::FORMAT_ADDRESSBOOKS
);
// Not sure if I'm doing it wrongly, or if its supposed to return
// the info in an array?
return (isset($addressbook['permissions']) ? $addressbook : $addressbook[0]);
}
/**
@ -79,9 +82,7 @@ class Shared extends Database {
public function getContacts($addressbookid, $limit = null, $offset = null, $omitdata = false) {
//\OCP\Util::writeLog('contacts', __METHOD__.' addressbookid: '
// . $addressbookid, \OCP\Util::DEBUG);
$permissions = 0;
$addressbook = \OCP\Share::getItemSharedWithBySource(
/*$addressbook = \OCP\Share::getItemSharedWithBySource(
'addressbook',
$addressbookid,
Contacts\Share_Backend_Addressbook::FORMAT_ADDRESSBOOKS,
@ -89,25 +90,14 @@ class Shared extends Database {
true // includeCollection
);
\OCP\Util::writeLog('contacts', __METHOD__.' shared: '
. print_r($addressbook, true), \OCP\Util::DEBUG);
. print_r($addressbook, true), \OCP\Util::DEBUG);*/
if(!$this->addressbooks) {
$this->addressbooks = \OCP\Share::getItemsSharedWith(
'addressbook',
Contacts\Share_Backend_Addressbook::FORMAT_ADDRESSBOOKS
);
}
foreach($this->addressbooks as $addressbook) {
if($addressbook['id'] === $addressbookid) {
$permissions = $addressbook['permissions'];
break;
}
}
$addressbook = $this->getAddressBook($addressbookid);
$permissions = $addressbook['permissions'];
$cards = array();
try {
$qfields = $omitdata ? '`id`, `fullname`' : '*';
$qfields = $omitdata ? '`id`, `fullname` AS `displayname`, `lastmodified`' : '*';
$query = 'SELECT ' . $qfields . ' FROM `' . $this->cardsTableName
. '` WHERE `addressbookid` = ? ORDER BY `fullname`';
$stmt = \OCP\DB::prepare($query, $limit, $offset);

View File

@ -44,7 +44,7 @@ class Contact extends VObject\VCard implements IPIMObject {
$this->props['parent'] = $parent;
$this->props['backend'] = $backend;
if(!is_null($properties) {
if(!is_null($properties)) {
foreach($properties as $key => $value) {
switch($key) {
case 'id':
@ -56,6 +56,13 @@ class Contact extends VObject\VCard implements IPIMObject {
case 'uri':
$this->props['uri'] = $value;
break;
case 'carddata':
$this->props['carddata'] = $value;
break;
case 'vcard':
$this->props['vcard'] = $value;
$this->read();
break;
case 'displayname':
case 'fullname':
$this->props['displayname'] = $value;
@ -118,19 +125,59 @@ class Contact extends VObject\VCard implements IPIMObject {
return $this->getPermissions() & $permission;
}
/**
* Save the address book data to backend
* FIXME
*
* @param array $data
* @return bool
*/
public function update(array $data) {
foreach($data as $key => $value) {
switch($key) {
case 'displayname':
$this->addressBookInfo['displayname'] = $value;
break;
case 'description':
$this->addressBookInfo['description'] = $value;
break;
}
}
return $this->props['backend']->updateContact(
$this->getParent()->getId(),
$this->getId(),
$this
);
}
/**
* Delete the data from backend
*
* @return bool
*/
public function delete() {
return $this->props['backend']->deleteContact(
$this->getParent()->getId(),
$this->getId()
);
}
/**
* Save the contact data to backend
*
* @return bool
*/
public function save() {
if($this->id) {
if($this->getId()) {
return $this->props['backend']->updateContact(
$this->props['parent']->getID(), $this->props['id'], $this->serialize()
$this->props['parent']->getId(),
$this->props['id'],
$this->serialize()
);
} else {
$this->props['id'] = $this->props['backend']->createContact(
$this->parent->getID(), $this->serialize()
$this->parent->getId(), $this->serialize()
);
if($this->props['id'] !== false) {
$this->parent->setChildID();
@ -143,10 +190,20 @@ class Contact extends VObject\VCard implements IPIMObject {
// NOTE: Maybe this will mess with
// the magic accessors.
if(!$this->children) {
if(isset($this->props['vcard'])
&& $this->props['vcard'] instanceof VObject\VCard) {
$this->children = $this->props['vcard']->children();
unset($this->props['vcard']);
return true;
}
if(!isset($this->props['carddata']) && is_null($data)) {
$result = $this->props['backend']->getContact($this->parent->getID, $this->id);
$result = $this->props['backend']->getContact(
$this->parent->getId(),
$this->id
);
if($result) {
if(isset($result['vcard']) && $result['vcard'] instanceof Contact) {
if(isset($result['vcard'])
&& $result['vcard'] instanceof VObject\VCard) {
// NOTE: Maybe iterate over $result['vcard']->children
// and add() them
$this->children = $result['vcard']->children;