Addressbook::isValidSource()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * @author Bart Visscher
4
 * @copyright 2012 Bart Visscher <[email protected]>
5
 * @copyright 2013-2014 Thomas Tanghus ([email protected])
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later.
9
 * See the COPYING-README file.
10
 */
11
12
namespace OCA\Contacts\Share;
13
use OCA\Contacts\App;
14
15
class Addressbook implements \OCP\Share_Backend_Collection {
16
	const FORMAT_ADDRESSBOOKS = 1;
17
	const FORMAT_COLLECTION = 2;
18
19
	/**
20
	 * @var \OCA\Contacts\App;
21
	 */
22
	public $app;
23
24
	public function __construct() {
25
		$this->app = new App(\OC::$server->getUserSession()->getUser()->getUId());
26
	}
27
28
	/**
29
	* @brief Get the source of the item to be stored in the database
30
	* @param string Item
31
	* @param string Owner of the item
32
	* @return boolean Source
33
	*
34
	* Return an array if the item is file dependent, the array needs two keys: 'item' and 'file'
35
	* Return false if the item does not exist for the user
36
	*
37
	* The formatItems() function will translate the source returned back into the item
38
	*/
39
	public function isValidSource($itemSource, $uidOwner) {
40
		$app = new App($uidOwner);
41
42
		try {
43
			$app->getAddressBook('local', $itemSource);
44
		} catch(\Exception $e) {
45
			return false;
46
		}
47
		return true;
48
	}
49
50
	/**
51
	* @brief Get a unique name of the item for the specified user
52
	* @param string Item
53
	* @param string|false User the item is being shared with
54
	* @param array|null List of similar item names already existing as shared items
55
	* @return string Target name
56
	*
57
	* This function needs to verify that the user does not already have an item with this name.
58
	* If it does generate a new name e.g. name_#
59
	*/
60
	public function generateTarget($itemSource, $shareWith, $exclude = null) {
61
		// Get app for the sharee
62
		$app = new App($shareWith);
63
		$backend = $app->getBackend('local');
64
65
		// Get address book for the owner
66
		$addressBook = $this->app->getBackend('local')->getAddressBook($itemSource);
67
68
		$userAddressBooks = array();
69
70
		foreach($backend->getAddressBooksForUser() as $userAddressBook) {
71
			$userAddressBooks[] = $userAddressBook['displayname'];
72
		}
73
		$name = $addressBook['displayname'] . '(' . $addressBook['owner'] . ')';
74
		$suffix = '';
75
		while (in_array($name.$suffix, $userAddressBooks)) {
76
			$suffix++;
77
		}
78
79
		$suffix = $suffix ? ' ' . $suffix : '';
80
		return $name.$suffix;
81
	}
82
83
	/**
84
	* @brief Converts the shared item sources back into the item in the specified format
85
	* @param array Shared items
86
	* @param int Format
87
	* @return ?
0 ignored issues
show
Documentation introduced by
The doc-type ? could not be parsed: Unknown type name "?" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
88
	*
89
	* 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.
90
	* The key/value pairs included in the share info depend on the function originally called:
91
	* If called by getItem(s)Shared: id, item_type, item, item_source, share_type, share_with, permissions, stime, file_source
92
	* If called by getItem(s)SharedWith: id, item_type, item, item_source, item_target, share_type, share_with, permissions, stime, file_source, file_target
93
	* This function allows the backend to control the output of shared items with custom formats.
94
	* It is only called through calls to the public getItem(s)Shared(With) functions.
95
	*/
96
	public function formatItems($items, $format, $parameters = null, $include = false) {
97
		//\OCP\Util::writeLog('contacts', __METHOD__
98
		//	. ' ' . $include . ' ' . print_r($items, true), \OCP\Util::DEBUG);
99
		$addressBooks = array();
100
		$backend = $this->app->getBackend('local');
101
102
		if ($format === self::FORMAT_ADDRESSBOOKS) {
103
			foreach ($items as $item) {
104
				//\OCP\Util::writeLog('contacts', __METHOD__.' item_source: ' . $item['item_source'] . ' include: '
105
				//	. (int)$include, \OCP\Util::DEBUG);
106
				$addressBook = $backend->getAddressBook($item['item_source'], array('shared_by' => $item['uid_owner']));
107
				if ($addressBook) {
108
					$addressBook['displayname'] = $addressBook['displayname'] . ' (' . $addressBook['owner'] . ')';
109
					$addressBook['permissions'] = $item['permissions'];
110
					$addressBooks[] = $addressBook;
111
				}
112
			}
113
		} elseif ($format === self::FORMAT_COLLECTION) {
114
			foreach ($items as $item) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
115
			}
116
		}
117
		return $addressBooks;
118
	}
119
120
	public function getChildren($itemSource) {
121
		\OCP\Util::writeLog('contacts', __METHOD__.' item_source: ' . $itemSource, \OCP\Util::DEBUG);
122
		$contacts = $this->app->getBackend('local')->getContacts($itemSource);
123
		$children = array();
124
		foreach($contacts as $contact) {
125
			$children[] = array('source' => $contact['id'], 'target' => $contact['displayname']);
126
		}
127
		return $children;
128
	}
129
130
	public function isShareTypeAllowed($shareType) {
131
		return true;
132
	}
133
134
}
135