2012-08-06 22:11:03 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2013-04-25 01:03:05 +02:00
|
|
|
namespace OCA\Contacts\Share;
|
|
|
|
use OCA\Contacts\Backend\Database;
|
2012-10-25 03:34:12 +02:00
|
|
|
|
2013-04-25 01:03:05 +02:00
|
|
|
class Addressbook implements \OCP\Share_Backend_Collection {
|
2012-08-06 22:11:03 +02:00
|
|
|
const FORMAT_ADDRESSBOOKS = 1;
|
2013-03-22 14:59:24 +01:00
|
|
|
const FORMAT_COLLECTION = 2;
|
2012-08-10 14:32:54 +02:00
|
|
|
|
2013-03-12 09:15:40 +01:00
|
|
|
public $backend;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
// Currently only share
|
2013-04-25 01:03:05 +02:00
|
|
|
$this->backend = new Database();
|
2013-03-12 09:15:40 +01:00
|
|
|
}
|
|
|
|
|
2012-08-06 22:11:03 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the source of the item to be stored in the database
|
|
|
|
* @param string Item
|
|
|
|
* @param string Owner of the item
|
|
|
|
* @return mixed|array|false Source
|
|
|
|
*
|
|
|
|
* Return an array if the item is file dependent, the array needs two keys: 'item' and 'file'
|
|
|
|
* Return false if the item does not exist for the user
|
|
|
|
*
|
|
|
|
* The formatItems() function will translate the source returned back into the item
|
|
|
|
*/
|
2012-08-10 14:32:54 +02:00
|
|
|
public function isValidSource($itemSource, $uidOwner) {
|
2013-03-12 09:15:40 +01:00
|
|
|
$addressbook = $this->backend->getAddressBook($itemSource);
|
2013-04-19 09:59:30 +02:00
|
|
|
if(!$addressbook || $addressbook['owner'] !== $uidOwner) {
|
2012-08-06 22:11:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get a unique name of the item for the specified user
|
|
|
|
* @param string Item
|
|
|
|
* @param string|false User the item is being shared with
|
|
|
|
* @param array|null List of similar item names already existing as shared items
|
|
|
|
* @return string Target name
|
|
|
|
*
|
|
|
|
* This function needs to verify that the user does not already have an item with this name.
|
|
|
|
* If it does generate a new name e.g. name_#
|
|
|
|
*/
|
2012-08-10 14:32:54 +02:00
|
|
|
public function generateTarget($itemSource, $shareWith, $exclude = null) {
|
2013-03-12 09:15:40 +01:00
|
|
|
$addressbook = $this->backend->getAddressBook($itemSource);
|
|
|
|
|
2012-08-06 22:11:03 +02:00
|
|
|
$user_addressbooks = array();
|
2013-03-12 09:15:40 +01:00
|
|
|
|
2013-10-23 19:12:31 +02:00
|
|
|
//foreach($this->backend->getAddressBooksForUser($shareWith) as $user_addressbook) {
|
|
|
|
// FIXME: The backend used is for the current user, not the right one
|
|
|
|
foreach($this->backend->getAddressBooksForUser() as $user_addressbook) {
|
2012-08-06 22:11:03 +02:00
|
|
|
$user_addressbooks[] = $user_addressbook['displayname'];
|
|
|
|
}
|
2013-03-10 12:38:27 +01:00
|
|
|
$name = $addressbook['displayname'] . '(' . $addressbook['userid'] . ')';
|
2012-08-06 22:11:03 +02:00
|
|
|
$suffix = '';
|
|
|
|
while (in_array($name.$suffix, $user_addressbooks)) {
|
|
|
|
$suffix++;
|
|
|
|
}
|
2012-08-23 14:45:19 +02:00
|
|
|
|
2013-03-10 12:38:27 +01:00
|
|
|
$suffix = $suffix ? ' ' . $suffix : '';
|
2012-08-06 22:11:03 +02:00
|
|
|
return $name.$suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Converts the shared item sources back into the item in the specified format
|
|
|
|
* @param array Shared items
|
|
|
|
* @param int Format
|
|
|
|
* @return ?
|
|
|
|
*
|
|
|
|
* The items array is a 3-dimensional array with the item_source as the first key and the share id as the second key to an array with the share info.
|
|
|
|
* The key/value pairs included in the share info depend on the function originally called:
|
|
|
|
* If called by getItem(s)Shared: id, item_type, item, item_source, share_type, share_with, permissions, stime, file_source
|
|
|
|
* If called by getItem(s)SharedWith: id, item_type, item, item_source, item_target, share_type, share_with, permissions, stime, file_source, file_target
|
|
|
|
* This function allows the backend to control the output of shared items with custom formats.
|
|
|
|
* It is only called through calls to the public getItem(s)Shared(With) functions.
|
|
|
|
*/
|
2013-03-12 09:15:40 +01:00
|
|
|
public function formatItems($items, $format, $parameters = null, $include = false) {
|
2013-03-16 15:59:23 +01:00
|
|
|
//\OCP\Util::writeLog('contacts', __METHOD__
|
|
|
|
// . ' ' . $include . ' ' . print_r($items, true), \OCP\Util::DEBUG);
|
2012-08-06 22:11:03 +02:00
|
|
|
$addressbooks = array();
|
2013-03-22 14:59:24 +01:00
|
|
|
if ($format === self::FORMAT_ADDRESSBOOKS) {
|
2012-08-10 14:33:20 +02:00
|
|
|
foreach ($items as $item) {
|
2013-03-12 09:15:40 +01:00
|
|
|
//\OCP\Util::writeLog('contacts', __METHOD__.' item_source: ' . $item['item_source'] . ' include: '
|
|
|
|
// . (int)$include, \OCP\Util::DEBUG);
|
|
|
|
$addressbook = $this->backend->getAddressBook($item['item_source']);
|
2012-08-10 14:33:20 +02:00
|
|
|
if ($addressbook) {
|
2013-04-07 21:49:53 +02:00
|
|
|
$addressbook['displayname'] = $addressbook['displayname'] . ' (' . $addressbook['owner'] . ')';
|
2012-08-23 22:02:38 +02:00
|
|
|
$addressbook['permissions'] = $item['permissions'];
|
2012-08-10 14:33:20 +02:00
|
|
|
$addressbooks[] = $addressbook;
|
|
|
|
}
|
2012-08-09 01:12:59 +02:00
|
|
|
}
|
2013-03-22 14:59:24 +01:00
|
|
|
} elseif ($format === self::FORMAT_COLLECTION) {
|
2013-03-13 08:52:00 +01:00
|
|
|
foreach ($items as $item) {
|
|
|
|
}
|
2012-08-06 22:11:03 +02:00
|
|
|
}
|
|
|
|
return $addressbooks;
|
|
|
|
}
|
|
|
|
|
2012-08-10 14:32:54 +02:00
|
|
|
public function getChildren($itemSource) {
|
2013-03-12 09:15:40 +01:00
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.' item_source: ' . $itemSource, \OCP\Util::DEBUG);
|
|
|
|
$contacts = $this->backend->getContacts($itemSource, null, null, true);
|
2012-09-09 05:11:40 +02:00
|
|
|
$children = array();
|
2013-03-12 09:15:40 +01:00
|
|
|
foreach($contacts as $contact) {
|
2012-09-09 05:11:40 +02:00
|
|
|
$children[] = array('source' => $contact['id'], 'target' => $contact['fullname']);
|
2012-08-06 22:11:03 +02:00
|
|
|
}
|
2012-09-09 05:11:40 +02:00
|
|
|
return $children;
|
2012-08-06 22:11:03 +02:00
|
|
|
}
|
|
|
|
|
2012-08-10 14:33:20 +02:00
|
|
|
}
|