1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-01 13:24:10 +01:00

Contacts: Add generic PIM classes + Contact class.

This commit is contained in:
Thomas Tanghus 2013-03-10 12:40:11 +01:00
parent 275c98cce8
commit c7dfc8fc33
4 changed files with 505 additions and 0 deletions

View File

@ -0,0 +1,144 @@
<?php
/**
* ownCloud - Collection class for PIM object
*
* @author Thomas Tanghus
* @copyright 2012 Thomas Tanghus (thomas@tanghus.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Contacts;
/**
* Subclass this for PIM collections
*/
abstract class PIMCollectionAbstract extends PIMObjectAbstract implements Iterator, Countable, ArrayAccess {
// Iterator properties
protected $objects = array();
protected $counter = 0;
/**
* Returns a specific child node, referenced by its id
*
* This method must throw Sabre_DAV_Exception_NotFound if the node does not
* exist.
*
* @param string $id
* @return IPIMObject
*/
abstract function getChild($id);
/**
* Returns an array with all the child nodes
*
* @return IPIMObject[]
*/
abstract function getChildren($limit = null, $offset = null);
/**
* Checks if a child-node with the specified id exists
*
* @param string $id
* @return bool
*/
function childExists($id);
// Iterator methods
public function rewind() {
$this->counter = 0;
}
public function next() {
$this->counter++;
}
public function valid() {
return array_key_exists($this->counter, $this->objects);
}
public function current() {
return $this->objects[$this->counter];
}
/** Implementations can choose to return the current objects ID/UUID
* to be able to iterate over the collection with ID => Object pairs:
* foreach($collection as $id => $object) {}
*/
public function key() {
return $this->counter;
}
// Countable method.
/**
* For implementations using a backend where fetching all object at once
* would give too much overhead, they can maintain an internal count value
* and fetch objects progressively. Simply watch the diffence between
* $this->counter, the value of count($this->objects) and the internal
* value, and fetch more objects when needed.
*/
public function count() {
return count($this->objects);
}
// ArrayAccess methods
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->objects[] = $value;
} else {
$this->objects[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->objects[$offset]);
}
public function offsetUnset($offset) {
unset($this->objects[$offset]);
}
public function offsetGet($offset) {
return isset($this->objects[$offset]) ? $this->objects[$offset] : null;
}
// Magic property accessors
// TODO: They should go in the implementations.
public function __set($id, $value) {
}
public function __get($id) {
}
public function __isset($id) {
}
public function __unset($id) {
}
}

116
lib/abstractpimobject.php Normal file
View File

@ -0,0 +1,116 @@
<?php
/**
* ownCloud - Interface for PIM object
*
* @author Thomas Tanghus
* @copyright 2012 Thomas Tanghus (thomas@tanghus.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Contacts;
/**
* Subclass this class or implement IPIMObject interface for PIM objects
*/
abstract class PIMObjectAbstract implements IPIMObject {
/**
* This variable holds the ID of this object.
* Depending on the backend, this can be either a string
* or an integer, so we treat them all as strings.
*
* @var string
*/
protected $id;
/**
* This variable holds the owner of this object.
*
* @var string
*/
protected $owner;
/**
* This variable holds the parent of this object if any.
*
* @var string|null
*/
protected $parent;
/**
* This variable holds the permissions of this object.
*
* @var integer
*/
protected $permissions;
/**
* @return string
*/
public function getId() {
return $this->id;
}
/**
* @return string|null
*/
public function getOwner() {
return $this->owner;
}
/**
* If this object is part of a collection return a reference
* to the parent object, otherwise return null.
* @return IPIMObject|null
*/
function getParent() {
return $this->parent;
}
/** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask of
*
* \OCP\PERMISSION_CREATE
* \OCP\PERMISSION_READ
* \OCP\PERMISSION_UPDATE
* \OCP\PERMISSION_DELETE
* \OCP\PERMISSION_SHARE
* or
* \OCP\PERMISSION_ALL
*
* @return integer
*/
function getPermissions() {
return $this->permissions;
}
/**
* @param integer $permission
* @return boolean
*/
function hasPermission($permission) {
return $this->getPermissions() & $permission;
}
/**
* Save the contact data to backend
*
* @return bool
*/
public function save() {
}
}

153
lib/contact.php Normal file
View File

@ -0,0 +1,153 @@
<?php
/**
* ownCloud - Contact object
*
* @author Thomas Tanghus
* @copyright 2012 Thomas Tanghus (thomas@tanghus.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Contacts;
/**
* Subclass this class or implement IPIMObject interface for PIM objects
*/
class Contact extends VObject\VCard implements IPIMObject {
/**
* The name of the object type in this case VCARD.
*
* This is used when serializing the object.
*
* @var string
*/
public $name = 'VCARD';
protected $props = array();
public function __construct($parent, $backend) {
//\OCP\Util::writeLog('contacts', __METHOD__, \OCP\Util::DEBUG);
$this->props['parent'] = $parent;
$this->props['backend'] = $backend;
}
/**
* @return string|null
*/
public function getOwner() {
return $this->props['owner'];
}
/**
* @return string|null
*/
public function getId() {
return $this->props['id'];
}
/**
* If this object is part of a collection return a reference
* to the parent object, otherwise return null.
* @return IPIMObject|null
*/
function getParent() {
return $this->props['parent'];
}
/** CRUDS permissions (Create, Read, Update, Delete, Share)
*
* @return integer
*/
function getPermissions() {
return $this->props['permissions'];
}
/**
* @param integer $permission
* @return bool
*/
function hasPermission($permission) {
return $this->getPermissions() & $permission;
}
/**
* Save the contact data to backend
*
* @return bool
*/
public function save() {
if($this->id) {
return $this->props['backend']->updateContact(
$this->props['parent']->getID(), $this->props['id'], $this->serialize()
);
} else {
$this->props['id'] = $this->props['backend']->createContact(
$this->parent->getID(), $this->serialize()
);
if($this->props['id'] !== false) {
$this->parent->setChildID();
}
return $this->props['id'] !== false;
}
}
public function read($data = null) {
// NOTE: Maybe this will mess with
// the magic accessors.
if(!$this->children) {
if(!isset($this->props['carddata']) && is_null($data)) {
$result = $this->props['backend']->getContact($this->parent->getID, $this->id);
if($result) {
if(isset($result['vcard']) && $result['vcard'] instanceof Contact) {
$this->children = $result['vcard']->children;
return true;
} elseif(isset($result['carddata'])) {
// Save internal values
$data = $result['carddata'];
$this->props['carddata'] = $result['carddata'];
$this->props['lastmodified'] = $result['lastmodified'];
$this->props['displayname'] = $result['displayname'];
$this->props['permissions'] = $result['permissions'];
} else {
return false;
}
}
}
try {
$obj = \Sabre\VObject\Reader::read(
$data,
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES);
$this->children = $obj->children;
} catch (\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__ .
'Error parsing carddata: ' . $e->getMessage(),
\OCP\Util::ERROR);
return false;
}
}
return true;
}
public function lastModified() {
if(!isset($this->props['lastmodified'])) {
$this->read();
}
return isset($this->props['lastmodified'])
? isset($this->props['lastmodified'])
: null;
}
}

92
lib/ipimobject.php Normal file
View File

@ -0,0 +1,92 @@
<?php
/**
* ownCloud - Interface for PIM object
*
* @author Thomas Tanghus
* @copyright 2012 Thomas Tanghus (thomas@tanghus.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Contacts;
/**
* Implement this interface for PIM objects
*/
interface IPIMObject {
/**
* If this object is part of a collection return a reference
* to the parent object, otherwise return null.
* @return IPIMObject|null
*/
//function getParent();
/**
* @return string
*/
public function getId();
/**
* @return string|null
*/
function getOwner();
/**
* If this object is part of a collection return a reference
* to the parent object, otherwise return null.
* @return IPIMObject|null
*/
function getParent();
/** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask of
*
* \OCP\PERMISSION_CREATE
* \OCP\PERMISSION_READ
* \OCP\PERMISSION_UPDATE
* \OCP\PERMISSION_DELETE
* \OCP\PERMISSION_SHARE
* or
* \OCP\PERMISSION_ALL
*
* @return integer
*/
function getPermissions();
/**
* @param integer $permission
* @return boolean
*/
function hasPermission($permission);
/**
* Save the contact data to backend
*
* @return bool
*/
public function save();
/**
* @brief Get the last modification time for the object.
*
* Must return a UNIX time stamp or null if the backend
* doesn't support it.
*
* @returns int | null
*/
public function lastModified();
}