From c34e89a6cf4cb28423453003af64b2bf7806b439 Mon Sep 17 00:00:00 2001 From: Tohir Solomons Date: Mon, 22 Dec 2008 09:57:00 +0000 Subject: [PATCH] KOA-372 "Changing the document type of a document from any type to default does not change the metadata fields to the default metadata fields" Fixed. change_document_type() function needed to be expanded --- ktapi/KTAPIDocument.inc.php | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/ktapi/KTAPIDocument.inc.php b/ktapi/KTAPIDocument.inc.php index 65a1a28..ad9c81a 100644 --- a/ktapi/KTAPIDocument.inc.php +++ b/ktapi/KTAPIDocument.inc.php @@ -739,7 +739,18 @@ class KTAPI_Document extends KTAPI_FolderItem if ($this->document->getDocumentTypeId() != $doctypeid) { + // Get the current document type, fieldsets and metadata + $iOldDocTypeID = $this->document->getDocumentTypeID(); + $fieldsets = KTMetadataUtil::fieldsetsForDocument($this->document, $iOldDocTypeID); + $mdlist = DocumentFieldLink::getByDocument($this->document); + + $field_values = array(); + foreach ($mdlist as $oFieldLink) { + $field_values[$oFieldLink->getDocumentFieldID()] = $oFieldLink->getValue(); + } + DBUtil::startTransaction(); + $this->document->startNewMetadataVersion($user); $this->document->setDocumentTypeId($doctypeid); $res = $this->document->update(); @@ -750,7 +761,42 @@ class KTAPI_Document extends KTAPI_FolderItem } - $metadata = $this->get_packed_metadata(); + // Ensure all values for fieldsets common to both document types are retained + $fs_ids = array(); + + $doctype_fieldsets = KTFieldSet::getForDocumentType($doctypeid); + foreach($doctype_fieldsets as $fieldset) + { + $fs_ids[] = $fieldset->getId(); + } + + $MDPack = array(); + foreach ($fieldsets as $oFieldset) + { + if ($oFieldset->getIsGeneric() || in_array($oFieldset->getId(), $fs_ids)) + { + $fields = $oFieldset->getFields(); + + foreach ($fields as $oField) + { + $val = isset($field_values[$oField->getId()]) ? $field_values[$oField->getId()] : ''; + + if (!empty($val)) + { + $MDPack[] = array($oField, $val); + } + } + } + } + + $core_res = KTDocumentUtil::saveMetadata($this->document, $MDPack, array('novalidate' => true)); + + if (PEAR::isError($core_res)) { + DBUtil::rollback(); + return $core_res; + } + + $oKTTriggerRegistry = KTTriggerRegistry::getSingleton(); $aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate'); -- libgit2 0.21.4