From 59f8744a4317dbe4f7ce7cdecd209ea0f720e1f2 Mon Sep 17 00:00:00 2001 From: Nigel Banks Date: Thu, 23 May 2013 05:03:23 +0200 Subject: [PATCH] Added new function islandora_copy_object() can copy existing/new fedora objects. It's a bit leaky in that it makes assumptions about the members that will exist on AbstractObjects, but we can't make cloning work at the moment as it doesn't really work well with inheritance, every decendent would have to define __clone functions properly and I figured a wrapper function like this would be easier to support in the long term, until we make the movement to decorated objects. --- islandora.module | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/islandora.module b/islandora.module index 79672625..df17e65f 100644 --- a/islandora.module +++ b/islandora.module @@ -951,6 +951,63 @@ function islandora_add_object(AbstractObject &$object) { return $object->repository->ingestObject($object); } +/** + * Creates a new object with the same properties as the old. + * + * @todo Make Tuque objects support cloneing. + * + * @param AbstractObject $object + * An existing or new Fedora Object. + * + * @return AbstractObject + * The new Fedora Object with properties identical to the object given. This + * returned object is not automatically ingested. + */ +function islandora_copy_object(AbstractObject $object) { + $clone = $object->repository->constructObject($object->id); + $object_properties = array( + 'state', + 'createdDate', + 'lastModifiedDate', + 'label', + 'owner', + 'logMessage', + ); + // Copy Properties. + foreach ($object_properties as $property) { + if (isset($object->$property)) { + $clone->$property = $object->$property; + } + } + $datastream_properties = array( + 'label', + 'versionable', + 'state', + 'mimetype', + 'format', + 'size', + 'checksum', + 'checksumType', + 'createdDate', + 'content', + 'url', + 'location', + ); + // Copy Datastreams. + foreach ($object as $dsid => $datastream) { + if (empty($clone[$dsid])) { + $ds = $clone->constructDatastream($dsid, $datastream->controlGroup); + $clone->ingestDatastream($ds); + } + foreach ($datastream_properties as $property) { + if (isset($object[$dsid]->$property)) { + $clone[$dsid]->$property = $object[$dsid]->$property; + } + } + } + return $clone; +} + /** * Delete's or purges the given object. *