Sharing backends for contacts and addressbooks

This commit is contained in:
Michael Gapczynski 2012-08-06 16:11:03 -04:00
parent 51ddc44b62
commit 3be4f5f2f1
2 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,88 @@
<?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.
*/
class OC_Share_Backend_Addressbook implements OCP\Share_Backend_Collection {
const FORMAT_ADDRESSBOOKS = 1;
/**
* @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
*/
public function isValidSource($item, $uid) {
$addressbook = OC_Contacts_Addressbook::find( $item );
if( $addressbook === false || $addressbook['userid'] != $uid) {
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_#
*/
public function generateTarget($item, $uid, $exclude = null) {
$addressbook = OC_Contacts_Addressbook::find( $item );
$user_addressbooks = array();
foreach(OC_Contacts_Addressbook::all($uid) as $user_addressbook) {
$user_addressbooks[] = $user_addressbook['displayname'];
}
$name = $addressbook['userid']."'s ".$addressbook['displayname'];
$suffix = '';
while (in_array($name.$suffix, $user_addressbooks)) {
$suffix++;
}
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.
*/
public function formatItems($items, $format, $parameters = null) {
$addressbooks = array();
foreach ($items as $item) {
$addressbook = OC_Contacts_Addressbook::find($item['item_source']);
$addressbook['displayname'] = $item['item_target'];
$addressbooks[] = $addressbook;
}
return $addressbooks;
}
public function getChildren($item) {
$query = OCP\DB::prepare('SELECT id FROM *PREFIX*contacts_cards WHERE addressbookid = ?');
$result = $query->execute(array($item));
$sources = array();
while ($contact = $result->fetchRow()) {
$sources[] = $contact['id'];
}
return $sources;
}
}

View File

@ -0,0 +1,53 @@
<?php
/**
* ownCloud
*
* @author Michael Gapczynski
* @copyright 2012 Michael Gapczynski mtgap@owncloud.com
*
* 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/>.
*/
class OC_Share_Backend_Contact implements OCP\Share_Backend {
const FORMAT_CONTACT = 0;
private static $contact;
public function isValidSource($itemSource, $uidOwner) {
self::$contact = OC_Contacts_VCard::find($itemSource);
if (self::$contact) {
return true;
}
return false;
}
public function generateTarget($itemSource, $shareWith, $exclude = null) {
// TODO Get default addressbook and check for conflicts
return self::$contact['fullname'];
}
public function formatItems($items, $format, $parameters = null) {
$contacts = array();
if ($format == self::FORMAT_CONTACT) {
foreach ($items as $item) {
$contact = OC_Contacts_VCard::find($item['item_source']);
$contact['fullname'] = $item['item_target'];
$contacts[] = $contact;
}
}
return $contacts;
}
}