1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-02-07 01:54:16 +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; 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. * This is a collection so return null.
* @return null * @return null
@ -44,6 +53,7 @@ abstract class PIMCollectionAbstract extends PIMObjectAbstract implements \Itera
/** /**
* Returns a specific child node, referenced by its id * Returns a specific child node, referenced by its id
* TODO: Maybe implement it here?
* *
* @param string $id * @param string $id
* @return IPIMObject * @return IPIMObject
@ -127,23 +137,23 @@ abstract class PIMCollectionAbstract extends PIMObjectAbstract implements \Itera
} }
// Magic property accessors // Magic property accessors
// TODO: They should go in the implementations. // NOTE: They should go in the implementations(?)
public function __set($id, $value) { public function __set($id, $value) {
$this->objects[$id] = $value;
} }
public function __get($id) { public function __get($id) {
return $this->objects[$id];
} }
public function __isset($id) { public function __isset($id) {
return isset($this->objects[$id]);
} }
public function __unset($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() { 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 * Returns a specific child node, referenced by its id
* *
* @param string $id * @param string $id
* @return IPIMObject * @return Contact|null
*/ */
function getChild($id) { function getChild($id) {
if(!isset($this->objects[$id])) { if(!isset($this->objects[$id])) {
@ -110,7 +112,11 @@ class Addressbook extends PIMCollectionAbstract {
$this->objects[$id] = new Contact($this, $this->backend, $contact); $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 * @param array $data
* @return bool * @return bool
@ -163,6 +182,15 @@ class Addressbook extends PIMCollectionAbstract {
$this->backend->updateAddressBook($this->getId(), $data); $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. * @brief Get the last modification time for the object.
* *

View File

@ -39,36 +39,53 @@ class Contact extends VObject\VCard implements IPIMObject {
protected $props = array(); 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); //\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG);
$this->props['parent'] = $parent; $this->props['parent'] = $parent;
$this->props['backend'] = $backend; $this->props['backend'] = $backend;
if(!is_null($properties)) { if(!is_null($data)) {
foreach($properties as $key => $value) { if($data instanceof VObject\VCard) {
switch($key) { foreach($obj->children as $child) {
case 'id': $this->add($child);
$this->props['id'] = $value;
break;
case 'lastmodified':
$this->props['lastmodified'] = $value;
break;
case 'uri':
$this->props['uri'] = $value;
break;
case 'carddata':
$this->props['carddata'] = $value;
break;
case 'vcard':
$this->props['vcard'] = $value;
$this->retrieve();
break;
case 'displayname':
case 'fullname':
$this->props['displayname'] = $value;
break;
} }
} elseif(is_array($data)) {
foreach($data as $key => $value) {
switch($key) {
case 'id':
$this->props['id'] = $value;
break;
case 'lastmodified':
$this->props['lastmodified'] = $value;
break;
case 'uri':
$this->props['uri'] = $value;
break;
case 'carddata':
$this->props['carddata'] = $value;
break;
case 'vcard':
$this->props['vcard'] = $value;
$this->retrieve();
break;
case 'displayname':
case 'fullname':
$this->props['displayname'] = $value;
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() { public function save() {
if($this->getId()) { if($this->getId()) {
return $this->props['backend']->updateContact( return $this->props['backend']->updateContact(
$this->props['parent']->getId(), $this->getParent()->getId(),
$this->props['id'], $this->getId(),
$this->serialize() $this->serialize()
); );
} else { } else {
$this->props['id'] = $this->props['backend']->createContact( $this->props['id'] = $this->props['backend']->createContact(
$this->parent->getId(), $this->serialize() $this->getParent()->getId(), $this->serialize()
); );
if($this->props['id'] !== false) { 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 * 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 * @param array $data
* @return bool * @return bool
@ -130,4 +131,4 @@ interface IPIMObject {
*/ */
public function delete(); public function delete();
} }