Skip to content
Snippets Groups Projects
Commit a51e9a5c authored by Bart Visscher's avatar Bart Visscher
Browse files

Merge pull request #8136 from owncloud/contactsmanager-register

Implement the register function of OC\ContactsManager
parents 339554ae 8749442f
Branches
No related tags found
No related merge requests found
......@@ -34,12 +34,13 @@ namespace OC {
* @return array an array of contacts which are arrays of key-value-pairs
*/
public function search($pattern, $searchProperties = array(), $options = array()) {
$this->loadAddressBooks();
$result = array();
foreach($this->address_books as $address_book) {
$r = $address_book->search($pattern, $searchProperties, $options);
foreach($this->addressBooks as $addressBook) {
$r = $addressBook->search($pattern, $searchProperties, $options);
$contacts = array();
foreach($r as $c){
$c['addressbook-key'] = $address_book->getKey();
$c['addressbook-key'] = $addressBook->getKey();
$contacts[] = $c;
}
$result = array_merge($result, $contacts);
......@@ -52,18 +53,20 @@ namespace OC {
* This function can be used to delete the contact identified by the given id
*
* @param object $id the unique identifier to a contact
* @param string $address_book_key identifier of the address book in which the contact shall be deleted
* @param string $addressBookKey identifier of the address book in which the contact shall be deleted
* @return bool successful or not
*/
public function delete($id, $address_book_key) {
if (!array_key_exists($address_book_key, $this->address_books))
public function delete($id, $addressBookKey) {
$addressBook = $this->getAddressBook($addressBookKey);
if (!$addressBook) {
return null;
}
$address_book = $this->address_books[$address_book_key];
if ($address_book->getPermissions() & \OCP\PERMISSION_DELETE)
if ($addressBook->getPermissions() & \OCP\PERMISSION_DELETE) {
return null;
}
return $address_book->delete($id);
return $addressBook->delete($id);
}
/**
......@@ -71,19 +74,20 @@ namespace OC {
* Otherwise the contact will be updated by replacing the entire data set.
*
* @param array $properties this array if key-value-pairs defines a contact
* @param string $address_book_key identifier of the address book in which the contact shall be created or updated
* @return array an array representing the contact just created or updated
* @param string $addressBookKey identifier of the address book in which the contact shall be created or updated
* @return array representing the contact just created or updated
*/
public function createOrUpdate($properties, $address_book_key) {
if (!array_key_exists($address_book_key, $this->address_books))
public function createOrUpdate($properties, $addressBookKey) {
$addressBook = $this->getAddressBook($addressBookKey);
if (!$addressBook) {
return null;
}
$address_book = $this->address_books[$address_book_key];
if ($address_book->getPermissions() & \OCP\PERMISSION_CREATE)
if ($addressBook->getPermissions() & \OCP\PERMISSION_CREATE) {
return null;
}
return $address_book->createOrUpdate($properties);
return $addressBook->createOrUpdate($properties);
}
/**
......@@ -92,30 +96,31 @@ namespace OC {
* @return bool true if enabled, false if not
*/
public function isEnabled() {
return !empty($this->address_books);
return !empty($this->addressBooks) || !empty($this->addressBookLoaders);
}
/**
* @param \OCP\IAddressBook $address_book
* @param \OCP\IAddressBook $addressBook
*/
public function registerAddressBook(\OCP\IAddressBook $address_book) {
$this->address_books[$address_book->getKey()] = $address_book;
public function registerAddressBook(\OCP\IAddressBook $addressBook) {
$this->addressBooks[$addressBook->getKey()] = $addressBook;
}
/**
* @param \OCP\IAddressBook $address_book
* @param \OCP\IAddressBook $addressBook
*/
public function unregisterAddressBook(\OCP\IAddressBook $address_book) {
unset($this->address_books[$address_book->getKey()]);
public function unregisterAddressBook(\OCP\IAddressBook $addressBook) {
unset($this->addressBooks[$addressBook->getKey()]);
}
/**
* @return array
*/
public function getAddressBooks() {
$this->loadAddressBooks();
$result = array();
foreach($this->address_books as $address_book) {
$result[$address_book->getKey()] = $address_book->getDisplayName();
foreach($this->addressBooks as $addressBook) {
$result[$addressBook->getKey()] = $addressBook->getDisplayName();
}
return $result;
......@@ -125,26 +130,56 @@ namespace OC {
* removes all registered address book instances
*/
public function clear() {
$this->address_books = array();
$this->addressBooks = array();
$this->addressBookLoaders = array();
}
/**
* @var \OCP\IAddressBook[] which holds all registered address books
*/
private $address_books = array();
private $addressBooks = array();
/**
* @var \Closure[] to call to load/register address books
*/
private $addressBookLoaders = array();
/**
* In order to improve lazy loading a closure can be registered which will be called in case
* address books are actually requested
*
* @param string $key
* @param \Closure $callable
*/
function register($key, \Closure $callable)
public function register(\Closure $callable)
{
//
//TODO: implement me
//
$this->addressBookLoaders[] = $callable;
}
/**
* Get (and load when needed) the address book for $key
*
* @param string $addressBookKey
* @return \OCP\IAddressBook
*/
protected function getAddressBook($addressBookKey)
{
$this->loadAddressBooks();
if (!array_key_exists($addressBookKey, $this->addressBooks)) {
return null;
}
return $this->addressBooks[$addressBookKey];
}
/**
* Load all address books registered with 'register'
*/
protected function loadAddressBooks()
{
foreach($this->addressBookLoaders as $callable) {
$callable($this);
}
$this->addressBookLoaders = array();
}
}
}
......@@ -134,7 +134,7 @@ namespace OCP {
*/
public static function registerAddressBook(\OCP\IAddressBook $address_book) {
$cm = \OC::$server->getContactsManager();
return $cm->registerAddressBook($address_book);
$cm->registerAddressBook($address_book);
}
/**
......@@ -142,7 +142,7 @@ namespace OCP {
*/
public static function unregisterAddressBook(\OCP\IAddressBook $address_book) {
$cm = \OC::$server->getContactsManager();
return $cm->unregisterAddressBook($address_book);
$cm->unregisterAddressBook($address_book);
}
/**
......
......@@ -138,11 +138,10 @@ namespace OCP\Contacts {
* In order to improve lazy loading a closure can be registered which will be called in case
* address books are actually requested
*
* @param string $key
* @param \Closure $callable
* @return void
*/
function register($key, \Closure $callable);
function register(\Closure $callable);
/**
* @return array
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment