Blame view

sources/apps/contacts/lib/share/addressbook.php 4.4 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
  <?php
  /**
6d9380f96   Cédric Dupont   Update sources OC...
3
4
5
6
   * @author Bart Visscher
   * @copyright 2012 Bart Visscher <bartv@thisnet.nl>
   * @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
   *
d1bafeea1   Kload   [fix] Upgrade to ...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
  
  namespace OCA\Contacts\Share;
  use OCA\Contacts\App;
  
  class Addressbook implements \OCP\Share_Backend_Collection {
  	const FORMAT_ADDRESSBOOKS = 1;
  	const FORMAT_COLLECTION = 2;
  
  	/**
  	 * @var \OCA\Contacts\App;
  	 */
  	public $app;
  
  	public function __construct() {
  		$this->app = new App(\OCP\User::getUser());
  	}
  
  	/**
  	* @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($itemSource, $uidOwner) {
  		$app = new App($uidOwner);
  
  		try {
  			$app->getAddressBook('local', $itemSource);
  		} catch(\Exception $e) {
  			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($itemSource, $shareWith, $exclude = null) {
  		// Get app for the sharee
  		$app = new App($shareWith);
  		$backend = $app->getBackend('local');
  
  		// Get address book for the owner
  		$addressBook = $this->app->getBackend('local')->getAddressBook($itemSource);
  
  		$userAddressBooks = array();
  
  		foreach($backend->getAddressBooksForUser() as $userAddressBook) {
  			$userAddressBooks[] = $userAddressBook['displayname'];
  		}
  		$name = $addressBook['displayname'] . '(' . $addressBook['owner'] . ')';
  		$suffix = '';
  		while (in_array($name.$suffix, $userAddressBooks)) {
  			$suffix++;
  		}
  
  		$suffix = $suffix ? ' ' . $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, $include = false) {
  		//\OCP\Util::writeLog('contacts', __METHOD__
  		//	. ' ' . $include . ' ' . print_r($items, true), \OCP\Util::DEBUG);
  		$addressBooks = array();
  		$backend = $this->app->getBackend('local');
  
  		if ($format === self::FORMAT_ADDRESSBOOKS) {
  			foreach ($items as $item) {
6d9380f96   Cédric Dupont   Update sources OC...
104
105
  				//\OCP\Util::writeLog('contacts', __METHOD__.' item_source: ' . $item['item_source'] . ' include: '
  				//	. (int)$include, \OCP\Util::DEBUG);
d1bafeea1   Kload   [fix] Upgrade to ...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  				$addressBook = $backend->getAddressBook($item['item_source'], array('shared_by' => $item['uid_owner']));
  				if ($addressBook) {
  					$addressBook['displayname'] = $addressBook['displayname'] . ' (' . $addressBook['owner'] . ')';
  					$addressBook['permissions'] = $item['permissions'];
  					$addressBooks[] = $addressBook;
  				}
  			}
  		} elseif ($format === self::FORMAT_COLLECTION) {
  			foreach ($items as $item) {
  			}
  		}
  		return $addressBooks;
  	}
  
  	public function getChildren($itemSource) {
  		\OCP\Util::writeLog('contacts', __METHOD__.' item_source: ' . $itemSource, \OCP\Util::DEBUG);
  		$contacts = $this->app->getBackend('local')->getContacts($itemSource);
  		$children = array();
  		foreach($contacts as $contact) {
  			$children[] = array('source' => $contact['id'], 'target' => $contact['displayname']);
  		}
  		return $children;
  	}
  
  }