1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-30 19:52:17 +01:00

Contacts: Updates on PIM classes.

This commit is contained in:
Thomas Tanghus 2013-03-17 21:32:53 +01:00
parent f8b0387fdd
commit 6fc05fa4cc
4 changed files with 99 additions and 43 deletions

View File

@ -34,6 +34,15 @@ abstract class PIMCollectionAbstract extends PIMObjectAbstract implements \Itera
protected $counter = 0;
/**
* Add a child to the collection
*
* It's up to the implementations to "keep track" of the children.
*
* @param mixed $data
*/
abstract public function add($data);
/**
* This is a collection so return null.
* @return null
@ -44,6 +53,7 @@ abstract class PIMCollectionAbstract extends PIMObjectAbstract implements \Itera
/**
* Returns a specific child node, referenced by its id
* TODO: Maybe implement it here?
*
* @param string $id
* @return IPIMObject
@ -127,22 +137,22 @@ abstract class PIMCollectionAbstract extends PIMObjectAbstract implements \Itera
}
// Magic property accessors
// TODO: They should go in the implementations.
// NOTE: They should go in the implementations(?)
public function __set($id, $value) {
$this->objects[$id] = $value;
}
public function __get($id) {
return $this->objects[$id];
}
public function __isset($id) {
return isset($this->objects[$id]);
}
public function __unset($id) {
unset($this->objects[$id]);
}

View File

@ -54,10 +54,12 @@ class Addressbook extends PIMCollectionAbstract {
}
/**
* @return string
* @return string|null
*/
public function getId() {
return $this->addressBookInfo['id'];
return isset($this->addressBookInfo['id'])
? $this->addressBookInfo['id']
: null;
}
/**
@ -101,7 +103,7 @@ class Addressbook extends PIMCollectionAbstract {
* Returns a specific child node, referenced by its id
*
* @param string $id
* @return IPIMObject
* @return Contact|null
*/
function getChild($id) {
if(!isset($this->objects[$id])) {
@ -110,7 +112,11 @@ class Addressbook extends PIMCollectionAbstract {
$this->objects[$id] = new Contact($this, $this->backend, $contact);
}
}
return isset($this->objects[$id]) ? $this->objects[$id] : null;
// When requesting a single contact we preparse it
if(isset($this->objects[$id])) {
$this->objects[$id]->retrieve();
return $this->objects[$id];
}
}
/**
@ -143,7 +149,20 @@ class Addressbook extends PIMCollectionAbstract {
}
/**
* Save the address book data to backend
* Add a contact to the address book
* FIXME: This should take an array or a VCard|Contact and return
* the ID or false (null?).
*
* @param array|VObject\VCard $data
*/
public function add($data) {
if($data instanceof VObject\VCard) {
} else {
}
}
/**
* Update and save the address book data to backend
*
* @param array $data
* @return bool
@ -163,6 +182,15 @@ class Addressbook extends PIMCollectionAbstract {
$this->backend->updateAddressBook($this->getId(), $data);
}
/**
* Save the address book data to backend
* NOTE: @see IPIMObject::update for consistency considerations.
*
* @return bool
*/
public function save() {
}
/**
* @brief Get the last modification time for the object.
*

View File

@ -39,14 +39,26 @@ class Contact extends VObject\VCard implements IPIMObject {
protected $props = array();
public function __construct($parent, $backend, $properties = null) {
//\OCP\Util::writeLog('contacts', __METHOD__ . ' ' . print_r($properties, true), \OCP\Util::DEBUG);
/**
* Create a new Contact object
*
* @param AddressBook $parent
* @param AbstractBackend $backend
* @param mixed $data
*/
public function __construct($parent, $backend, $data = null) {
//\OCP\Util::writeLog('contacts', __METHOD__ . ' ' . print_r($data, true), \OCP\Util::DEBUG);
//\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG);
$this->props['parent'] = $parent;
$this->props['backend'] = $backend;
if(!is_null($properties)) {
foreach($properties as $key => $value) {
if(!is_null($data)) {
if($data instanceof VObject\VCard) {
foreach($obj->children as $child) {
$this->add($child);
}
} elseif(is_array($data)) {
foreach($data as $key => $value) {
switch($key) {
case 'id':
$this->props['id'] = $value;
@ -70,6 +82,11 @@ class Contact extends VObject\VCard implements IPIMObject {
break;
}
}
} else {
throw new Exception(
__METHOD__ . ' 3rd argument must either be an array or a subclass of \VObject\VCard'
);
}
}
}
@ -192,18 +209,18 @@ class Contact extends VObject\VCard implements IPIMObject {
public function save() {
if($this->getId()) {
return $this->props['backend']->updateContact(
$this->props['parent']->getId(),
$this->props['id'],
$this->getParent()->getId(),
$this->getId(),
$this->serialize()
);
} else {
$this->props['id'] = $this->props['backend']->createContact(
$this->parent->getId(), $this->serialize()
$this->getParent()->getId(), $this->serialize()
);
if($this->props['id'] !== false) {
$this->parent->setChildID();
$this->getParent()->setChildID($this);
}
return $this->props['id'] !== false;
return $this->getId() !== false;
}
}

View File

@ -106,7 +106,8 @@ interface IPIMObject {
/**
* Save the contact data to backend
* FIXME: This isn't consistent.
* FIXME: This isn't consistent. We need a proper interface
* for creating new instances and saving to storage.
*
* @param array $data
* @return bool