1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-11 22:24:32 +01:00
OwncloudContactsOfficial/lib/backend/shared.php

146 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/**
* ownCloud - Backend for Shared contacts
*
* @author Thomas Tanghus
2014-01-26 00:40:22 +01:00
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Contacts\Backend;
2013-03-12 09:15:40 +01:00
use OCA\Contacts;
/**
2014-04-24 13:39:29 +02:00
* Backend class for shared address books.
*/
class Shared extends Database {
2014-04-24 13:39:29 +02:00
/**
* The name of the backend.
*
* @var string
*/
2013-03-15 17:02:33 +01:00
public $name = 'shared';
2014-04-24 13:39:29 +02:00
/**
* The cached address books.
*
* @var array[]
*/
2014-03-14 20:57:26 +01:00
public $addressBooks = array();
/**
2014-03-16 01:29:24 +01:00
* {@inheritdoc}
*/
public function getAddressBooksForUser(array $options = array()) {
// workaround for https://github.com/owncloud/core/issues/2814
$maybeSharedAddressBook = \OCP\Share::getItemsSharedWith(
'addressbook',
2013-04-25 01:03:05 +02:00
Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS
);
2014-03-08 16:22:51 +01:00
foreach ($maybeSharedAddressBook as $sharedAddressbook) {
if (isset($sharedAddressbook['id'])) {
2014-04-22 15:39:44 +02:00
$this->addressBooks[$sharedAddressbook['id']] = $sharedAddressbook;
$this->addressBooks[$sharedAddressbook['id']]['backend'] = $this->name;
}
2014-03-08 16:22:51 +01:00
}
2014-03-14 20:57:26 +01:00
return $this->addressBooks;
}
2013-03-12 09:15:40 +01:00
/**
2014-03-16 01:29:24 +01:00
* {@inheritdoc}
*/
2014-03-14 20:57:26 +01:00
public function getAddressBook($addressBookId, array $options = array()) {
2014-03-14 20:57:26 +01:00
foreach ($this->addressBooks as $addressBook) {
2014-03-08 16:22:51 +01:00
2014-03-14 20:57:26 +01:00
if ($addressBook['id'] === $addressBookId) {
return $addressBook;
}
2014-03-08 16:22:51 +01:00
}
2014-03-08 16:22:51 +01:00
$addressBook = \OCP\Share::getItemSharedWithBySource(
2013-03-12 09:15:40 +01:00
'addressbook',
2014-03-14 20:57:26 +01:00
$addressBookId,
2013-04-25 01:03:05 +02:00
Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS
2013-03-12 09:15:40 +01:00
);
2014-03-08 16:22:51 +01:00
2013-03-12 20:00:22 +01:00
// Not sure if I'm doing it wrongly, or if its supposed to return
// the info in an array?
$addressBook = (isset($addressBook['permissions']) ? $addressBook : $addressBook[0]);
if(!isset($addressBook['permissions'])) {
return null;
}
$addressBook['backend'] = $this->name;
2014-03-14 20:57:26 +01:00
$this->addressBooks[] = $addressBook;
return $addressBook;
2013-03-12 09:15:40 +01:00
}
/**
2014-03-16 01:29:24 +01:00
* {@inheritdoc}
*/
2014-03-14 20:57:26 +01:00
public function getContacts($addressBookId, array $options = array()) {
2014-03-14 20:57:26 +01:00
$addressBook = $this->getAddressBook($addressBookId);
2014-03-08 16:22:51 +01:00
if (!$addressBook) {
2014-03-14 20:57:26 +01:00
throw new \Exception('Shared Address Book not found: ' . $addressBookId, 404);
}
2014-03-08 16:22:51 +01:00
$permissions = $addressBook['permissions'];
2014-03-14 20:57:26 +01:00
$cards = parent::getContacts($addressBookId, $options);
2014-03-08 16:22:51 +01:00
foreach ($cards as &$card) {
$card['permissions'] = $permissions;
}
return $cards;
}
2013-03-12 09:15:40 +01:00
/**
2014-03-16 01:29:24 +01:00
* {@inheritdoc}
*/
2014-03-14 20:57:26 +01:00
public function getContact($addressBookId, $id, array $options = array()) {
2014-03-08 16:22:51 +01:00
2014-03-14 20:57:26 +01:00
$addressBook = $this->getAddressBook($addressBookId);
2014-03-08 16:22:51 +01:00
if (!$addressBook) {
2014-03-14 20:57:26 +01:00
throw new \Exception('Shared Address Book not found: ' . $addressBookId, 404);
}
2014-03-08 16:22:51 +01:00
$permissions = $addressBook['permissions'];
2014-03-14 20:57:26 +01:00
$card = parent::getContact($addressBookId, $id, $options);
2014-03-08 16:22:51 +01:00
if (!$card) {
2014-01-22 22:22:58 +01:00
throw new \Exception('Shared Contact not found: ' . implode(',', $id), 404);
}
2014-03-08 16:22:51 +01:00
$card['permissions'] = $permissions;
return $card;
}
}