diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2016-02-10 17:06:13 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2016-02-10 17:06:13 +0100 |
commit | c919b4139599a854d60dc057c2f55ba4fa10694a (patch) | |
tree | ce36542d7b89d5b31ae2eaf4d80428c34ac3dce7 /apps/dav/lib/carddav | |
parent | 159a0eb597425d7082aff7cf857d4d042cf8ebd2 (diff) | |
download | nextcloud-server-c919b4139599a854d60dc057c2f55ba4fa10694a.tar.gz nextcloud-server-c919b4139599a854d60dc057c2f55ba4fa10694a.zip |
Adding a custom webdav property which holds the list of contacts groups
Diffstat (limited to 'apps/dav/lib/carddav')
-rw-r--r-- | apps/dav/lib/carddav/addressbook.php | 7 | ||||
-rw-r--r-- | apps/dav/lib/carddav/carddavbackend.php | 20 | ||||
-rw-r--r-- | apps/dav/lib/carddav/plugin.php | 32 | ||||
-rw-r--r-- | apps/dav/lib/carddav/xml/groups.php | 45 |
4 files changed, 102 insertions, 2 deletions
diff --git a/apps/dav/lib/carddav/addressbook.php b/apps/dav/lib/carddav/addressbook.php index ca3f5ba0ef6..be57a2d90a1 100644 --- a/apps/dav/lib/carddav/addressbook.php +++ b/apps/dav/lib/carddav/addressbook.php @@ -161,4 +161,11 @@ class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareable { } parent::delete(); } + + public function getContactsGroups() { + /** @var CardDavBackend $cardDavBackend */ + $cardDavBackend = $this->carddavBackend; + + return $cardDavBackend->collectCardProperties($this->getResourceId(), 'CATEGORIES'); + } } diff --git a/apps/dav/lib/carddav/carddavbackend.php b/apps/dav/lib/carddav/carddavbackend.php index c4f29b39d0d..aa2490ab11a 100644 --- a/apps/dav/lib/carddav/carddavbackend.php +++ b/apps/dav/lib/carddav/carddavbackend.php @@ -29,6 +29,7 @@ use OCP\DB\QueryBuilder\IQueryBuilder; use OCA\DAV\DAV\Sharing\Backend; use OCA\DAV\DAV\Sharing\IShareable; use OCP\IDBConnection; +use PDO; use Sabre\CardDAV\Backend\BackendInterface; use Sabre\CardDAV\Backend\SyncSupport; use Sabre\CardDAV\Plugin; @@ -762,6 +763,25 @@ class CardDavBackend implements BackendInterface, SyncSupport { } /** + * @param int $bookId + * @param string $name + * @return array + */ + public function collectCardProperties($bookId, $name) { + $query = $this->db->getQueryBuilder(); + $result = $query->selectDistinct('value') + ->from($this->dbCardsPropertiesTable) + ->where($query->expr()->eq('name', $query->createNamedParameter($name))) + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) + ->execute(); + + $all = $result->fetchAll(PDO::FETCH_COLUMN); + $result->closeCursor(); + + return $all; + } + + /** * get URI from a given contact * * @param int $id diff --git a/apps/dav/lib/carddav/plugin.php b/apps/dav/lib/carddav/plugin.php index 4acc1037b52..d94dce1db0e 100644 --- a/apps/dav/lib/carddav/plugin.php +++ b/apps/dav/lib/carddav/plugin.php @@ -21,10 +21,19 @@ namespace OCA\DAV\CardDAV; +use OCA\DAV\CardDAV\Xml\Groups; +use Sabre\DAV\INode; +use Sabre\DAV\PropFind; +use Sabre\DAV\Server; use Sabre\HTTP\URLUtil; class Plugin extends \Sabre\CardDAV\Plugin { + function initialize(Server $server) { + $server->on('propFind', [$this, 'propFind']); + parent::initialize($server); + } + /** * Returns the addressbook home for a given principal * @@ -33,15 +42,34 @@ class Plugin extends \Sabre\CardDAV\Plugin { */ protected function getAddressbookHomeForPrincipal($principal) { - if (strrpos($principal, 'principals/users', -strlen($principal)) !== FALSE) { + if (strrpos($principal, 'principals/users', -strlen($principal)) !== false) { list(, $principalId) = URLUtil::splitPath($principal); return self::ADDRESSBOOK_ROOT . '/users/' . $principalId; } - if (strrpos($principal, 'principals/system', -strlen($principal)) !== FALSE) { + if (strrpos($principal, 'principals/system', -strlen($principal)) !== false) { list(, $principalId) = URLUtil::splitPath($principal); return self::ADDRESSBOOK_ROOT . '/system/' . $principalId; } throw new \LogicException('This is not supposed to happen'); } + + /** + * Adds all CardDAV-specific properties + * + * @param PropFind $propFind + * @param INode $node + * @return void + */ + function propFind(PropFind $propFind, INode $node) { + + $ns = '{http://owncloud.org/ns}'; + + if ($node instanceof AddressBook) { + + $propFind->handle($ns . 'groups', function () use ($node) { + return new Groups($node->getContactsGroups()); + }); + } + } } diff --git a/apps/dav/lib/carddav/xml/groups.php b/apps/dav/lib/carddav/xml/groups.php new file mode 100644 index 00000000000..b39615db033 --- /dev/null +++ b/apps/dav/lib/carddav/xml/groups.php @@ -0,0 +1,45 @@ +<?php +/** + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\DAV\CardDAV\Xml; + +use Sabre\Xml\XmlSerializable; +use Sabre\Xml\Element; +use Sabre\Xml\Writer; + +class Groups implements XmlSerializable { + const NS_OWNCLOUD = 'http://owncloud.org/ns'; + + /** @var string[] of TYPE:CHECKSUM */ + private $groups; + + /** + * @param string $groups + */ + public function __construct($groups) { + $this->groups = $groups; + } + + function xmlSerialize(Writer $writer) { + foreach ($this->groups as $group) { + $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); + } + } +} |