Commit c34e89a6cf4cb28423453003af64b2bf7806b439

Authored by Tohir Solomons
1 parent 3cbd93df

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

Committed By: Tohir Solomons
Reviewed By: Megan Watson	 

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@9694 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 47 additions and 1 deletions
ktapi/KTAPIDocument.inc.php
... ... @@ -739,7 +739,18 @@ class KTAPI_Document extends KTAPI_FolderItem
739 739  
740 740 if ($this->document->getDocumentTypeId() != $doctypeid)
741 741 {
  742 + // Get the current document type, fieldsets and metadata
  743 + $iOldDocTypeID = $this->document->getDocumentTypeID();
  744 + $fieldsets = KTMetadataUtil::fieldsetsForDocument($this->document, $iOldDocTypeID);
  745 + $mdlist = DocumentFieldLink::getByDocument($this->document);
  746 +
  747 + $field_values = array();
  748 + foreach ($mdlist as $oFieldLink) {
  749 + $field_values[$oFieldLink->getDocumentFieldID()] = $oFieldLink->getValue();
  750 + }
  751 +
742 752 DBUtil::startTransaction();
  753 + $this->document->startNewMetadataVersion($user);
743 754 $this->document->setDocumentTypeId($doctypeid);
744 755 $res = $this->document->update();
745 756  
... ... @@ -750,7 +761,42 @@ class KTAPI_Document extends KTAPI_FolderItem
750 761 }
751 762  
752 763  
753   - $metadata = $this->get_packed_metadata();
  764 + // Ensure all values for fieldsets common to both document types are retained
  765 + $fs_ids = array();
  766 +
  767 + $doctype_fieldsets = KTFieldSet::getForDocumentType($doctypeid);
  768 + foreach($doctype_fieldsets as $fieldset)
  769 + {
  770 + $fs_ids[] = $fieldset->getId();
  771 + }
  772 +
  773 + $MDPack = array();
  774 + foreach ($fieldsets as $oFieldset)
  775 + {
  776 + if ($oFieldset->getIsGeneric() || in_array($oFieldset->getId(), $fs_ids))
  777 + {
  778 + $fields = $oFieldset->getFields();
  779 +
  780 + foreach ($fields as $oField)
  781 + {
  782 + $val = isset($field_values[$oField->getId()]) ? $field_values[$oField->getId()] : '';
  783 +
  784 + if (!empty($val))
  785 + {
  786 + $MDPack[] = array($oField, $val);
  787 + }
  788 + }
  789 + }
  790 + }
  791 +
  792 + $core_res = KTDocumentUtil::saveMetadata($this->document, $MDPack, array('novalidate' => true));
  793 +
  794 + if (PEAR::isError($core_res)) {
  795 + DBUtil::rollback();
  796 + return $core_res;
  797 + }
  798 +
  799 +
754 800  
755 801 $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
756 802 $aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate');
... ...