1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-18 07:52:21 +01:00

Fix CardDAV updating/deleting

This commit is contained in:
Thomas Tanghus 2013-10-03 18:09:19 +02:00
parent 95620bd9d3
commit 4c2fe05bd4
3 changed files with 52 additions and 29 deletions

View File

@ -496,6 +496,7 @@ class Database extends AbstractBackend {
*/
public function createContact($addressbookid, $contact, array $options = array()) {
$qname = 'createcontact';
$uri = isset($options['uri']) ? $options['uri'] : null;
if(!$contact instanceof VCard) {
@ -525,13 +526,13 @@ class Database extends AbstractBackend {
$contact->PRODID = $prodid;
$data = $contact->serialize();
if(!isset(self::$preparedQueries['createcontact'])) {
self::$preparedQueries['createcontact'] = \OCP\DB::prepare('INSERT INTO `'
if(!isset(self::$preparedQueries[$qname])) {
self::$preparedQueries[$qname] = \OCP\DB::prepare('INSERT INTO `'
. $this->cardsTableName
. '` (`addressbookid`,`fullname`,`carddata`,`uri`,`lastmodified`) VALUES(?,?,?,?,?)' );
}
try {
$result = self::$preparedQueries['createcontact']
$result = self::$preparedQueries[$qname]
->execute(
array(
$addressbookid,
@ -569,9 +570,9 @@ class Database extends AbstractBackend {
* @return bool
*/
public function updateContact($addressbookid, $id, $contact, array $options = array()) {
$noCollection = isset($options['noCollection']) ? $options['noCollection'] : false;
$isBatch = isset($options['isBatch']) ? $options['isBatch'] : false;
$qname = 'updatecontact';
$updateRevision = true;
$isCardDAV = false;
@ -583,25 +584,24 @@ class Database extends AbstractBackend {
return false;
}
}
$where_query = '`id` = ?';
if(is_array($id)) {
$where_query = '';
if(isset($id['id'])) {
$id = $id['id'];
$qname = 'createcontactbyid';
} elseif(isset($id['uri'])) {
$updateRevision = false;
$isCardDAV = true;
$where_query = '`id` = ?';
$id = $id['uri'];
$qname = 'createcontactbyuri';
$id = $this->getIdFromUri($id['uri']);
if(is_null($id)) {
\OCP\Util::writeLog('contacts', __METHOD__ . ' Couldn\'t find contact', \OCP\Util::ERROR);
return false;
}
} else {
throw new \Exception(
__METHOD__ . ' If second argument is an array, either \'id\' or \'uri\' has to be set.'
);
}
} else {
$qname = 'createcontactbyid';
}
if($updateRevision || !isset($contact->REV)) {
@ -611,19 +611,15 @@ class Database extends AbstractBackend {
$data = $contact->serialize();
$updates = array($contact->FN, $data, time(), $id);
if($noCollection) {
$me = $this->getContact(null, $id, $options);
$addressbookid = $me['parent'];
}
//if(!$noCollection) {
$where_query .= ' AND `addressbookid` = ?';
$updates[] = $addressbookid;
//}
$updates = array($contact->FN, $data, time(), $id, $addressbookid);
$query = 'UPDATE `' . $this->cardsTableName
. '` SET `fullname` = ?,`carddata` = ?, `lastmodified` = ? WHERE ' . $where_query;
. '` SET `fullname` = ?,`carddata` = ?, `lastmodified` = ? WHERE `id` = ? AND `addressbookid` = ?';
if(!isset(self::$preparedQueries[$qname])) {
self::$preparedQueries[$qname] = \OCP\DB::prepare($query);
}
@ -661,25 +657,24 @@ class Database extends AbstractBackend {
public function deleteContact($addressbookid, $id, array $options = array()) {
// TODO: pass the uri in $options instead.
$qname = 'deletecontact';
$noCollection = isset($options['noCollection']) ? $options['noCollection'] : false;
$isBatch = isset($options['isBatch']) ? $options['isBatch'] : false;
$where_query = '`id` = ?';
if(is_array($id)) {
$where_query = '';
if(isset($id['id'])) {
$id = $id['id'];
$qname = 'deletecontactsbyid';
} elseif(isset($id['uri'])) {
$where_query = '`id` = ?';
$id = $id['uri'];
$qname = 'deletecontactsbyuri';
$id = $this->getIdFromUri($id['uri']);
if(is_null($id)) {
\OCP\Util::writeLog('contacts', __METHOD__ . ' Couldn\'t find contact', \OCP\Util::ERROR);
return false;
}
} else {
throw new Exception(
__METHOD__ . ' If second argument is an array, either \'id\' or \'uri\' has to be set.'
);
}
} else {
$qname = 'deletecontactsbyid';
}
if(!$isBatch) {
@ -687,11 +682,18 @@ class Database extends AbstractBackend {
array('id' => $id)
);
}
if($noCollection) {
$me = $this->getContact(null, $id, $options);
$addressbookid = $me['parent'];
}
if(!isset(self::$preparedQueries[$qname])) {
self::$preparedQueries[$qname] = \OCP\DB::prepare('DELETE FROM `'
. $this->cardsTableName
. '` WHERE ' . $where_query . ' AND `addressbookid` = ?');
. '` WHERE `id` = ? AND `addressbookid` = ?');
}
\OCP\Util::writeLog('contacts', __METHOD__ . ' updates: ' . $id . '/' . $addressbookid, \OCP\Util::DEBUG);
try {
$result = self::$preparedQueries[$qname]->execute(array($id, $addressbookid));
if (\OCP\DB::isError($result)) {
@ -725,6 +727,27 @@ class Database extends AbstractBackend {
return ($contact ? $contact['lastmodified'] : null);
}
/**
* @brief Get the contact id from the uri.
*
* @param mixed $id
* @returns int | null
*/
public function getIdFromUri($uri) {
$query = 'SELECT `id` FROM `'. $this->cardsTableName . '` WHERE `uri` = ?';
$stmt = \OCP\DB::prepare($query);
$result = $stmt->execute(array($uri));
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return null;
}
if((int)$result->numRows() === 0) {
\OCP\Util::writeLog('contacts', __METHOD__.', Not found, uri: '. $uri, \OCP\Util::DEBUG);
return null;
}
return $result->fetchOne();
}
private function createAddressBookURI($displayname, $userid = null) {
$userid = $userid ? $userid : \OCP\User::getUser();
$name = str_replace(' ', '_', strtolower($displayname));

View File

@ -242,7 +242,7 @@ class Backend extends \Sabre_CardDAV_Backend_Abstract {
*/
public function deleteCard($addressbookid, $carduri) {
list($id, $backend) = $this->getBackendForAddressBook($addressbookid);
return $backend->deleteContact($addressbookid, array('uri' => $carduri));
return $backend->deleteContact($id, array('uri' => $carduri));
}
/**

View File

@ -43,7 +43,6 @@ class Plugin extends \Sabre_CardDAV_Plugin {
* @return void
*/
protected function validateVCard(&$data) {
\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG);
// If it's a stream, we convert it to a string first.
if (is_resource($data)) {
@ -52,6 +51,7 @@ class Plugin extends \Sabre_CardDAV_Plugin {
// Converting the data to unicode, if needed.
$data = \Sabre_DAV_StringUtil::ensureUTF8($data);
//\OCP\Util::writeLog('contacts', __METHOD__ . "\n".$data, \OCP\Util::DEBUG);
try {
$vobj = VObject\Reader::read($data, VObject\Reader::OPTION_IGNORE_INVALID_LINES);