return $addressBooks;
}
- public function getAddressBooksByUri($addressBookUri) {
+ /**
+ * @param int $addressBookId
+ */
+ public function getAddressBookById($addressBookId) {
+ $query = $this->db->getQueryBuilder();
+ $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken'])
+ ->from('addressbooks')
+ ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId)))
+ ->execute();
+
+ $row = $result->fetch();
+ $result->closeCursor();
+ if ($row === false) {
+ return null;
+ }
+
+ return [
+ 'id' => $row['id'],
+ 'uri' => $row['uri'],
+ 'principaluri' => $row['principaluri'],
+ '{DAV:}displayname' => $row['displayname'],
+ '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'],
+ '{http://calendarserver.org/ns/}getctag' => $row['synctoken'],
+ '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
+ ];
+ }
+
+ /**
+ * @param $addressBookUri
+ * @return array|null
+ */
+ public function getAddressBooksByUri($principal, $addressBookUri) {
$query = $this->db->getQueryBuilder();
$result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken'])
->from('addressbooks')
->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri)))
+ ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal)))
->setMaxResults(1)
->execute();
* @param string $principalUri
* @param string $url Just the 'basename' of the url.
* @param array $properties
+ * @return int
* @throws BadRequest
*/
function createAddressBook($principalUri, $url, array $properties) {
])
->setParameters($values)
->execute();
+
+ return $query->getLastInsertId();
}
/**
}
/**
- * @param string $path
+ * @param AddressBook $book
* @param string[] $add
* @param string[] $remove
*/
- public function updateShares($path, $add, $remove) {
+ public function updateShares($book, $add, $remove) {
foreach($add as $element) {
- $this->shareWith($path, $element);
+ $this->shareWith($book, $element);
}
foreach($remove as $element) {
- $this->unshare($path, $element);
+ $this->unshare($book->getBookId(), $element);
}
}
/**
- * @param string $addressBookUri
+ * @param AddressBook $addressBook
* @param string $element
*/
- private function shareWith($addressBookUri, $element) {
+ private function shareWith($addressBook, $element) {
$user = $element['href'];
$parts = explode(':', $user, 2);
if ($parts[0] !== 'principal') {
return;
}
- $addressBook = $this->getAddressBooksByUri($addressBookUri);
- if (is_null($addressBook)) {
- return;
- }
-
// remove the share if it already exists
- $this->unshare($addressBookUri, $element['href']);
+ $this->unshare($addressBook->getBookId(), $element['href']);
$access = self::ACCESS_READ;
if (isset($element['readOnly'])) {
$access = $element['readOnly'] ? self::ACCESS_READ : self::ACCESS_READ_WRITE;
}
- $newUri = sha1($addressBookUri . $addressBook['principaluri']);
+ $newUri = sha1($addressBook->getName() . $addressBook->getOwner());
+// $newUri = $addressBook->getName() . '-' . $addressBook->getOwner();
$query = $this->db->getQueryBuilder();
$query->insert('dav_shares')
->values([
'uri' => $query->createNamedParameter($newUri),
'type' => $query->createNamedParameter('addressbook'),
'access' => $query->createNamedParameter($access),
- 'resourceid' => $query->createNamedParameter($addressBook['id'])
+ 'resourceid' => $query->createNamedParameter($addressBook->getBookId())
]);
$query->execute();
}
/**
- * @param string $addressBookUri
+ * @param int $addressBookId
* @param string $element
*/
- private function unshare($addressBookUri, $element) {
+ private function unshare($addressBookId, $element) {
$parts = explode(':', $element, 2);
if ($parts[0] !== 'principal') {
return;
return;
}
- $addressBook = $this->getAddressBooksByUri($addressBookUri);
- if (is_null($addressBook)) {
- return;
- }
-
$query = $this->db->getQueryBuilder();
$query->delete('dav_shares')
- ->where($query->expr()->eq('resourceid', $query->createNamedParameter($addressBook['id'])))
+ ->where($query->expr()->eq('resourceid', $query->createNamedParameter($addressBookId)))
->andWhere($query->expr()->eq('type', $query->createNamedParameter('addressbook')))
->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($parts[1])))
;