diff --git a/plugins/libkolab/lib/kolab_format.php b/plugins/libkolab/lib/kolab_format.php index 601d3660..809fb29c 100644 --- a/plugins/libkolab/lib/kolab_format.php +++ b/plugins/libkolab/lib/kolab_format.php @@ -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 diff --git a/plugins/libkolab/lib/kolab_format_contact.php b/plugins/libkolab/lib/kolab_format_contact.php index 7994b97d..e816294a 100644 --- a/plugins/libkolab/lib/kolab_format_contact.php +++ b/plugins/libkolab/lib/kolab_format_contact.php @@ -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())); diff --git a/plugins/libkolab/lib/kolab_format_distributionlist.php b/plugins/libkolab/lib/kolab_format_distributionlist.php index f91f6978..d25bd473 100644 --- a/plugins/libkolab/lib/kolab_format_distributionlist.php +++ b/plugins/libkolab/lib/kolab_format_distributionlist.php @@ -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', diff --git a/plugins/libkolab/lib/kolab_format_event.php b/plugins/libkolab/lib/kolab_format_event.php index d750f8e0..6b1db886 100644 --- a/plugins/libkolab/lib/kolab_format_event.php +++ b/plugins/libkolab/lib/kolab_format_event.php @@ -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(); diff --git a/plugins/libkolab/lib/kolab_format_file.php b/plugins/libkolab/lib/kolab_format_file.php index 22d1e7c5..191c7fe8 100644 --- a/plugins/libkolab/lib/kolab_format_file.php +++ b/plugins/libkolab/lib/kolab_format_file.php @@ -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(), diff --git a/plugins/libkolab/lib/kolab_format_journal.php b/plugins/libkolab/lib/kolab_format_journal.php index 01cdc3db..3528d162 100644 --- a/plugins/libkolab/lib/kolab_format_journal.php +++ b/plugins/libkolab/lib/kolab_format_journal.php @@ -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 diff --git a/plugins/libkolab/lib/kolab_format_note.php b/plugins/libkolab/lib/kolab_format_note.php index 5a9c0bc5..cee6345b 100644 --- a/plugins/libkolab/lib/kolab_format_note.php +++ b/plugins/libkolab/lib/kolab_format_note.php @@ -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 diff --git a/plugins/libkolab/lib/kolab_format_task.php b/plugins/libkolab/lib/kolab_format_task.php index d66cafd7..0fa28065 100644 --- a/plugins/libkolab/lib/kolab_format_task.php +++ b/plugins/libkolab/lib/kolab_format_task.php @@ -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(); diff --git a/plugins/libkolab/lib/kolab_format_xcal.php b/plugins/libkolab/lib/kolab_format_xcal.php index a6450afb..b3d3c275 100644 --- a/plugins/libkolab/lib/kolab_format_xcal.php +++ b/plugins/libkolab/lib/kolab_format_xcal.php @@ -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;