Implement the register function of OC\ContactsManager

This commit is contained in:
Bart Visscher 2014-04-09 21:53:06 +02:00
parent 5b8c7a01e9
commit 511816b878
1 changed files with 48 additions and 12 deletions

View File

@ -34,6 +34,7 @@ namespace OC {
* @return array of contacts which are arrays of key-value-pairs * @return array of contacts which are arrays of key-value-pairs
*/ */
public function search($pattern, $searchProperties = array(), $options = array()) { public function search($pattern, $searchProperties = array(), $options = array()) {
$this->loadAddressBooks();
$result = array(); $result = array();
foreach($this->address_books as $address_book) { foreach($this->address_books as $address_book) {
$r = $address_book->search($pattern, $searchProperties, $options); $r = $address_book->search($pattern, $searchProperties, $options);
@ -51,12 +52,14 @@ namespace OC {
* @return bool successful or not * @return bool successful or not
*/ */
public function delete($id, $address_book_key) { public function delete($id, $address_book_key) {
if (!array_key_exists($address_book_key, $this->address_books)) $address_book = $this->getAddressBook($address_book_key);
if (!$address_book) {
return null; return null;
}
$address_book = $this->address_books[$address_book_key]; if ($address_book->getPermissions() & \OCP\PERMISSION_DELETE) {
if ($address_book->getPermissions() & \OCP\PERMISSION_DELETE)
return null; return null;
}
return $address_book->delete($id); return $address_book->delete($id);
} }
@ -70,13 +73,14 @@ namespace OC {
* @return array representing the contact just created or updated * @return array representing the contact just created or updated
*/ */
public function createOrUpdate($properties, $address_book_key) { public function createOrUpdate($properties, $address_book_key) {
$address_book = $this->getAddressBook($address_book_key);
if (!array_key_exists($address_book_key, $this->address_books)) if (!$address_book) {
return null; return null;
}
$address_book = $this->address_books[$address_book_key]; if ($address_book->getPermissions() & \OCP\PERMISSION_CREATE) {
if ($address_book->getPermissions() & \OCP\PERMISSION_CREATE)
return null; return null;
}
return $address_book->createOrUpdate($properties); return $address_book->createOrUpdate($properties);
} }
@ -87,7 +91,7 @@ namespace OC {
* @return bool true if enabled, false if not * @return bool true if enabled, false if not
*/ */
public function isEnabled() { public function isEnabled() {
return !empty($this->address_books); return !empty($this->address_books) || !empty($this->address_book_loaders);
} }
/** /**
@ -108,6 +112,7 @@ namespace OC {
* @return array * @return array
*/ */
public function getAddressBooks() { public function getAddressBooks() {
$this->loadAddressBooks();
$result = array(); $result = array();
foreach($this->address_books as $address_book) { foreach($this->address_books as $address_book) {
$result[$address_book->getKey()] = $address_book->getDisplayName(); $result[$address_book->getKey()] = $address_book->getDisplayName();
@ -121,6 +126,7 @@ namespace OC {
*/ */
public function clear() { public function clear() {
$this->address_books = array(); $this->address_books = array();
$this->address_book_loaders = array();
} }
/** /**
@ -128,6 +134,11 @@ namespace OC {
*/ */
private $address_books = array(); private $address_books = array();
/**
* @var \Closure[] to call to load/register address books
*/
private $address_book_loaders = array();
/** /**
* In order to improve lazy loading a closure can be registered which will be called in case * In order to improve lazy loading a closure can be registered which will be called in case
* address books are actually requested * address books are actually requested
@ -135,11 +146,36 @@ namespace OC {
* @param string $key * @param string $key
* @param \Closure $callable * @param \Closure $callable
*/ */
function register($key, \Closure $callable) public function register($key, \Closure $callable)
{ {
// $this->address_book_loaders[$key] = $callable;
//TODO: implement me }
//
/**
* Get (and load when needed) the address book for $key
*
* @param string $address_book_key
* @return \OCP\IAddressBook
*/
protected function getAddressBook($address_book_key)
{
$this->loadAddressBooks();
if (!array_key_exists($address_book_key, $this->address_books)) {
return null;
}
return $this->address_books[$address_book_key];
}
/**
* Load all address books registered with 'register'
*/
protected function loadAddressBooks()
{
foreach($this->address_book_loaders as $callable) {
$callable();
}
$this->address_book_loaders = array();
} }
} }
} }