1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-30 19:52:17 +01:00
This commit is contained in:
LEDfan 2014-04-01 20:48:00 +02:00
parent a234865f71
commit a6393f9695

View File

@ -7,10 +7,12 @@ use OCA\Contacts\Contact,
OCA\Contacts\Utils\Properties, OCA\Contacts\Utils\Properties,
Sabre\VObject\Reader, Sabre\VObject\Reader,
OCA\Contacts\Addressbook; OCA\Contacts\Addressbook;
/** /**
* Contact backend for storing all the ownCloud users in this installation. * Contact backend for storing all the ownCloud users in this installation.
* Every user has *1* personal addressbook. The id of this addresbook is the * Every user has *1* personal addressbook. The id of this addresbook is the
* userid of the owner. * userid of the owner.
*/ */
class OwnCloudUsers extends AbstractBackend { class OwnCloudUsers extends AbstractBackend {
@ -33,6 +35,9 @@ class OwnCloudUsers extends AbstractBackend {
$this->userid = $userid ? $userid : \OCP\User::getUser(); $this->userid = $userid ? $userid : \OCP\User::getUser();
} }
/**
* {@inheritdoc}
*/
public function getAddressBooksForUser(array $options = array()) { public function getAddressBooksForUser(array $options = array()) {
// Only 1 addressbook for every user // Only 1 addressbook for every user
$sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?';
@ -40,17 +45,17 @@ class OwnCloudUsers extends AbstractBackend {
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
$result = $query->execute($args); $result = $query->execute($args);
$row = $result->fetchRow(); $row = $result->fetchRow();
// Check if there are no results TODO?
if(!$row){ if(!$row){ // TODO -> better way?
// Create new addressbook // Create new addressbook
$sql = 'INSERT INTO ' . $this->addressBooksTableName $sql = 'INSERT INTO ' . $this->addressBooksTableName
. ' ( ' . ' ( '
. 'id, ' . 'id, '
. 'displayname, ' . 'displayname, '
//. 'uri, ' TODO //. 'uri, ' TODO
. 'description, ' . 'description, '
// . 'ctag, ' //. 'ctag, '
. 'active ' . 'active '
. ') VALUES ( ' . ') VALUES ( '
. '?, ' . '?, '
. '?, ' . '?, '
@ -73,6 +78,10 @@ class OwnCloudUsers extends AbstractBackend {
} }
} }
/**
* {@inheritdoc}
* Only 1 addressbook for every user
*/
public function getAddressBook($addressBookId, array $options = array()) { public function getAddressBook($addressBookId, array $options = array()) {
$sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?';
@ -86,6 +95,10 @@ class OwnCloudUsers extends AbstractBackend {
return array($row); return array($row);
} }
/**
* {@inheritdoc}
* There are as many contacts in this addressbook as in this ownCloud installation
*/
public function getContacts($addressbookid, array $options = array()){ public function getContacts($addressbookid, array $options = array()){
$contacts = array(); $contacts = array();
@ -125,6 +138,13 @@ class OwnCloudUsers extends AbstractBackend {
} }
} }
/**
* {@inheritdoc}
* If your username is "admin" and you want to retrieve your own contact
* the params would be: $addressbookid = 'admin'; $id = 'admin';
* If your username is 'foo' and you want to retrieve the contact with
* ownCloud username 'bar' the params would be: $addressbookid = 'foo'; $id = 'bar';
*/
public function getContact($addressbookid, $id, array $options = array()){ public function getContact($addressbookid, $id, array $options = array()){
$sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?';
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
@ -135,10 +155,18 @@ class OwnCloudUsers extends AbstractBackend {
return $row; return $row;
} }
// Not needed since there is only one addressbook for every user
public function createAddressBook(array $properties) { public function createAddressBook(array $properties) {
} }
/**
* Help function to add contacts to an addressbook.
* This only happens when an admin creates new users
* @param array $contacts array with userid of ownCloud users
* @param string $addressBookId
* @return bool
*/
private function addContacts($contacts, $addressbookid){ private function addContacts($contacts, $addressbookid){
foreach($contacts as $user){ foreach($contacts as $user){
$sql = 'INSERT INTO ' . $this->cardsTableName . ' (' $sql = 'INSERT INTO ' . $this->cardsTableName . ' ('
@ -173,19 +201,32 @@ class OwnCloudUsers extends AbstractBackend {
); );
$carddata = $this->generateCardData($contact); $carddata = $this->generateCardData($contact);
$result = $query->execute(array($user, $this->userid, $addressbookid, \OCP\User::getDisplayName($user), $carddata->serialize(), 'test', time())); $result = $query->execute(array($user, $this->userid, $addressbookid, \OCP\User::getDisplayName($user), $carddata->serialize(), 'test', time()));
// TODO Check if $result succeeded
} }
} }
/**
* Help function to remove contacts from an addressbook.
* This only happens when an admin remove an ownCloud user
* @param array $contacts array with userid of ownCloud users
* @param string $addressBookId
* @return bool
*/
private function removeContacts($contacts, $addressbookid){ private function removeContacts($contacts, $addressbookid){
foreach($contacts as $user){ foreach($contacts as $user){
$sql = 'DELETE FROM ' . $this->cardsTableName . ' WHERE owner = ? AND id = ?'; $sql = 'DELETE FROM ' . $this->cardsTableName . ' WHERE owner = ? AND id = ?';
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
$result = $query->execute(array($this->userid, $user)); $result = $query->execute(array($this->userid, $user));
// TODO Check if $result succeeded
} }
} }
/**
* Help function to generate the carddate which than can be stored in the db
* @param string|VCard $data
* @return Vcard
*/
private function generateCardData($data){ private function generateCardData($data){
if (!$data instanceof VCard) { if (!$data instanceof VCard) {
try { try {
@ -215,49 +256,50 @@ class OwnCloudUsers extends AbstractBackend {
return $data; return $data;
} }
/**
* @inheritdoc
*/
public function updateContact($addressBookId, $id, $contact, array $options = array()) { public function updateContact($addressBookId, $id, $contact, array $options = array()) {
$updateRevision = true; $updateRevision = true;
$isCardDAV = false; $isCardDAV = false;
if (!$contact instanceof VCard) { if (!$contact instanceof VCard) {
try { try {
$contact = Reader::read($contact); $contact = Reader::read($contact);
} catch(\Exception $e) { } catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR);
return false; return false;
} }
} }
if (is_array($id)) { if (is_array($id)) {
if (isset($id['id'])) {
$id = $id['id'];
} elseif (isset($id['uri'])) {
$updateRevision = false;
$isCardDAV = true;
$id = $this->getIdFromUri($id['uri']);
if (isset($id['id'])) { if (is_null($id)) {
$id = $id['id']; \OCP\Util::writeLog('contacts', __METHOD__ . ' Couldn\'t find contact', \OCP\Util::ERROR);
} elseif (isset($id['uri'])) { return false;
$updateRevision = false;
$isCardDAV = true;
$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 {
throw new \Exception(
__METHOD__ . ' If second argument is an array, either \'id\' or \'uri\' has to be set.'
);
}
} }
if ($updateRevision || !isset($contact->REV)) { if ($updateRevision || !isset($contact->REV)) {
$now = new \DateTime; $now = new \DateTime;
$contact->REV = $now->format(\DateTime::W3C); $contact->REV = $now->format(\DateTime::W3C);
} }
$data = $contact->serialize(); $data = $contact->serialize();
$sql = 'UPDATE ' . $this->cardsTableName $sql = 'UPDATE ' . $this->cardsTableName
. ' SET ' . ' SET '
. '`addressbookid` = ?, ' . '`addressbookid` = ?, '