Small refactoring: use base class methods to read/set common properties; add support for custom properties in all objects

This commit is contained in:
Thomas Bruederli 2013-02-21 16:28:12 +01:00
parent 581724f7c1
commit 97d0c57eee
9 changed files with 87 additions and 108 deletions

View file

@ -381,12 +381,33 @@ abstract class kolab_format
*
* @param array Object data as hash array
*/
abstract public function set(&$object);
public function set(&$object)
{
$this->init();
/**
*
*/
abstract public function is_valid();
if (!empty($object['uid']))
$this->obj->setUid($object['uid']);
// set some automatic values if missing
if (method_exists($this->obj, 'setCreated') && !$this->obj->created()) {
if (empty($object['created']))
$object['created'] = new DateTime('now', self::$timezone);
$this->obj->setCreated(self::get_datetime($object['created']));
}
$object['changed'] = new DateTime('now', self::$timezone);
$this->obj->setLastModified(self::get_datetime($object['changed'], new DateTimeZone('UTC')));
// Save custom properties of the given object
if (!empty($object['x-custom'])) {
$vcustom = new vectorcs;
foreach ($object['x-custom'] as $cp) {
if (is_array($cp))
$vcustom->push(new CustomProperty($cp[0], $cp[1]));
}
$this->obj->setCustomProperties($vcustom);
}
}
/**
* Convert the Kolab object into a hash array data structure
@ -395,7 +416,35 @@ abstract class kolab_format
*
* @return array Kolab object data as hash array
*/
abstract public function to_array($data = array());
public function to_array($data = array())
{
$this->init();
// read object properties into local data object
$object = array(
'uid' => $this->obj->uid(),
'changed' => self::php_datetime($this->obj->lastModified()),
);
// not all container support the created property
if (method_exists($this->obj, 'created')) {
$object['created'] = self::php_datetime($this->obj->created());
}
// read custom properties
$vcustom = $this->obj->customProperties();
for ($i=0; $i < $vcustom->size(); $i++) {
$cp = $vcustom->get($i);
$object['x-custom'][] = array($cp->identifier, $cp->value);
}
return $object;
}
/**
* Object validation method to be implemented by derived classes
*/
abstract public function is_valid();
/**
* Callback for kolab_storage_cache to get object specific tags to cache

View file

@ -85,20 +85,8 @@ class kolab_format_contact extends kolab_format
*/
public function set(&$object)
{
$this->init();
// set some automatic values if missing
if (false && !$this->obj->created()) {
if (!empty($object['created']))
$object['created'] = new DateTime('now', self::$timezone);
$this->obj->setCreated(self::get_datetime($object['created']));
}
if (!empty($object['uid']))
$this->obj->setUid($object['uid']);
$object['changed'] = new DateTime('now', self::$timezone);
$this->obj->setLastModified(self::get_datetime($object['changed'], new DateTimeZone('UTC')));
// set common object properties
parent::set($object);
// do the hard work of setting object values
$nc = new NameComponents;
@ -276,14 +264,10 @@ class kolab_format_contact extends kolab_format
if (!empty($this->data))
return $this->data;
$this->init();
// read common object props into local data object
$object = parent::to_array();
// read object properties into local data object
$object = array(
'uid' => $this->obj->uid(),
'name' => $this->obj->name(),
'changed' => self::php_datetime($this->obj->lastModified()),
);
$object['name'] = $this->obj->name();
$nc = $this->obj->nameComponents();
$object['surname'] = join(' ', self::vector2array($nc->surnames()));

View file

@ -39,14 +39,8 @@ class kolab_format_distributionlist extends kolab_format
*/
public function set(&$object)
{
$this->init();
// set some automatic values if missing
if (!empty($object['uid']))
$this->obj->setUid($object['uid']);
$object['changed'] = new DateTime('now', self::$timezone);
$this->obj->setLastModified(self::get_datetime($object['changed'], new DateTimeZone('UTC')));
// set common object properties
parent::set($object);
$this->obj->setName($object['name']);
@ -93,12 +87,11 @@ class kolab_format_distributionlist extends kolab_format
if (!empty($this->data))
return $this->data;
$this->init();
// read common object props into local data object
$object = parent::to_array();
// read object properties
$object = array(
'uid' => $this->obj->uid(),
'changed' => self::php_datetime($this->obj->lastModified()),
// add object properties
$object += array(
'name' => $this->obj->name(),
'member' => array(),
'_type' => 'distribution-list',

View file

@ -61,8 +61,6 @@ class kolab_format_event extends kolab_format_xcal
*/
public function set(&$object)
{
$this->init();
// set common xcal properties
parent::set($object);
@ -116,8 +114,6 @@ class kolab_format_event extends kolab_format_xcal
if (!empty($this->data))
return $this->data;
$this->init();
// read common xcal props
$object = parent::to_array();

View file

@ -44,14 +44,9 @@ class kolab_format_file extends kolab_format
*/
public function set(&$object)
{
$this->init();
// set common object properties
parent::set($object);
// set some automatic values if missing
if (!empty($object['uid']))
$this->obj->setUid($object['uid']);
$object['changed'] = new DateTime('now', self::$timezone);
$this->obj->setLastModified(self::get_datetime($object['changed'], new DateTimeZone('UTC')));
$this->obj->setClassification($this->sensitivity_map[$object['sensitivity']]);
$this->obj->setCategories(self::array2vector($object['categories']));
@ -106,15 +101,13 @@ class kolab_format_file extends kolab_format
if (!empty($this->data))
return $this->data;
$this->init();
// read common object props into local data object
$object = parent::to_array();
$sensitivity_map = array_flip($this->sensitivity_map);
// read object properties
$object = array(
'uid' => $this->obj->uid(),
'created' => self::php_datetime($this->obj->created()),
'changed' => self::php_datetime($this->obj->lastModified()),
$object += array(
'sensitivity' => $sensitivity_map[$this->obj->classification()],
'categories' => self::vector2array($this->obj->categories()),
'notes' => $this->obj->note(),

View file

@ -39,14 +39,8 @@ class kolab_format_journal extends kolab_format
*/
public function set(&$object)
{
$this->init();
// set some automatic values if missing
if (!empty($object['uid']))
$this->obj->setUid($object['uid']);
$object['changed'] = new DateTime('now', self::$timezone);
$this->obj->setLastModified(self::get_datetime($object['changed'], new DateTimeZone('UTC')));
// set common object properties
parent::set($object);
// TODO: set object propeties
@ -76,14 +70,8 @@ class kolab_format_journal extends kolab_format
if (!empty($this->data))
return $this->data;
$this->init();
// read object properties
$object = array(
'uid' => $this->obj->uid(),
'created' => self::php_datetime($this->obj->created()),
'changed' => self::php_datetime($this->obj->lastModified()),
);
// read common object props into local data object
$object = parent::to_array();
// TODO: read object properties

View file

@ -39,14 +39,8 @@ class kolab_format_note extends kolab_format
*/
public function set(&$object)
{
$this->init();
// set some automatic values if missing
if (!empty($object['uid']))
$this->obj->setUid($object['uid']);
$object['changed'] = new DateTime('now', self::$timezone);
$this->obj->setLastModified(self::get_datetime($object['changed'], new DateTimeZone('UTC')));
// set common object properties
parent::set($object);
// TODO: set object propeties
@ -76,14 +70,8 @@ class kolab_format_note extends kolab_format
if (!empty($this->data))
return $this->data;
$this->init();
// read object properties
$object = array(
'uid' => $this->obj->uid(),
'created' => self::php_datetime($this->obj->created()),
'changed' => self::php_datetime($this->obj->lastModified()),
);
// read common object props into local data object
$object = parent::to_array();
// TODO: read object properties

View file

@ -38,8 +38,6 @@ class kolab_format_task extends kolab_format_xcal
*/
public function set(&$object)
{
$this->init();
// set common xcal properties
parent::set($object);
@ -81,8 +79,6 @@ class kolab_format_task extends kolab_format_xcal
if (!empty($this->data))
return $this->data;
$this->init();
// read common xcal props
$object = parent::to_array();

View file

@ -94,13 +94,13 @@ abstract class kolab_format_xcal extends kolab_format
*/
public function to_array($data = array())
{
// read common object props
$object = parent::to_array();
$status_map = array_flip($this->status_map);
$sensitivity_map = array_flip($this->sensitivity_map);
$object = array(
'uid' => $this->obj->uid(),
'created' => self::php_datetime($this->obj->created()),
'changed' => self::php_datetime($this->obj->lastModified()),
$object += array(
'sequence' => intval($this->obj->sequence()),
'title' => $this->obj->summary(),
'location' => $this->obj->location(),
@ -216,20 +216,12 @@ abstract class kolab_format_xcal extends kolab_format
*/
public function set(&$object)
{
$this->init();
$is_new = !$this->obj->uid();
// set some automatic values if missing
if (!$this->obj->created()) {
if (!empty($object['created']))
$object['created'] = new DateTime('now', self::$timezone);
$this->obj->setCreated(self::get_datetime($object['created']));
}
if (!empty($object['uid']))
$this->obj->setUid($object['uid']);
$object['changed'] = new DateTime('now', self::$timezone);
$this->obj->setLastModified(self::get_datetime($object['changed'], new DateTimeZone('UTC')));
// set common object properties
parent::set($object);
// increment sequence on updates
$object['sequence'] = !$is_new ? $this->obj->sequence()+1 : 0;