function do_trytype() { $oForm = $this->form_changetype(); $res = $oForm->validate(); $data = $res['results']; $errors = $res['errors']; if (!empty($errors)) { $oForm->handleError(); } $document_type = $data['type']; $doctypeid = $document_type->getId(); // Get the current document type, fieldsets and metadata $iOldDocTypeID = $this->oDocument->getDocumentTypeID(); $fieldsets = KTMetadataUtil::fieldsetsForDocument($this->oDocument, $iOldDocTypeID); $mdlist = DocumentFieldLink::getByDocument($this->oDocument); $field_values = array(); foreach ($mdlist as $oFieldLink) { $field_values[$oFieldLink->getDocumentFieldID()] = $oFieldLink->getValue(); } DBUtil::startTransaction(); // Update the document with the new document type id $this->oDocument->startNewMetadataVersion($this->oUser); $this->oDocument->setDocumentTypeId($doctypeid); $res = $this->oDocument->update(); if (PEAR::isError($res)) { DBUtil::rollback(); return $res; } // 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->oDocument, $MDPack, array('novalidate' => true)); if (PEAR::isError($core_res)) { DBUtil::rollback(); return $core_res; } DBUtil::commit(); $oKTTriggerRegistry = KTTriggerRegistry::getSingleton(); $aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate'); foreach ($aTriggers as $aTrigger) { $sTrigger = $aTrigger[0]; $oTrigger = new $sTrigger(); $aInfo = array("document" => $this->oDocument, "aOptions" => $MDPack); $oTrigger->setInfo($aInfo); $ret = $oTrigger->postValidate(); } // Check if there are any dynamic conditions / permissions that need to be updated on the document // If there are dynamic conditions then update the permissions on the document // The dynamic condition test fails unless the changes exists in the DB therefore update permissions after committing the transaction. $iPermissionObjectId = $this->oDocument->getPermissionObjectID(); $dynamicCondition = KTPermissionDynamicCondition::getByPermissionObjectId($iPermissionObjectId); if (!PEAR::isError($dynamicCondition) && !empty($dynamicCondition)) { $res = KTPermissionUtil::updatePermissionLookup($this->oDocument); } $this->successRedirectToMain(sprintf(_kt("You have selected a new document type: %s. "), $data['type']->getName())); }
<?php require_once "../../config/dmsDefaults.php"; require_once KT_LIB_DIR . '/documentmanagement/documentutil.inc.php'; require_once KT_LIB_DIR . '/filelike/fsfilelike.inc.php'; $oDocument =& Document::get(207); if (PEAR::isError($oDocument)) { print "FAILURE\n"; var_dump($oDocument); } $res = KTDocumentUtil::saveMetadata($oDocument, array()); if (PEAR::isError($res)) { print "FAILURE\n"; var_dump($res); exit(0); } // saveMetadata can update status id $oDocument->update(); if (file_exists($sFilename)) { unlink($sFilename); }
/** * This updates the metadata on the document. This includes the 'title'. * * <code> * $ktapi = new KTAPI(); * $session = $ktapi->start_system_session(); * $document = $ktapi->get_document_by_id($documentid); * $metadata = $document->get_metadata(); * foreach($metadata as $key => $fieldset){ * if($fieldset['fieldset'] == 'XYZ'){ * * foreach($fieldset['fields'] as $k => $field){ * if($field['name'] == 'ABC'){ * $metadata[$key][fields][$k]['value'] = 'new value'; * } * } * } * } * * $res = $document->update_metadata($metadata); * </code> * * @author KnowledgeTree Team * @access public * @param array This is an array containing the metadata to be associated with the document. * @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure */ function update_metadata($metadata) { global $default; if (empty($metadata)) { return; } $packed = $this->get_packed_metadata($metadata); DBUtil::startTransaction(); $user = $this->ktapi->get_user(); $this->document->setLastModifiedDate(getCurrentDateTime()); $this->document->setModifiedUserId($user->getId()); // Update the content version / document version if ($default->updateContentVersion) { $this->document->startNewContentVersion($user); $this->document->setMinorVersionNumber($this->document->getMinorVersionNumber() + 1); } else { $this->document->startNewMetadataVersion($user); } $res = $this->document->update(); if (PEAR::isError($res)) { DBUtil::rollback(); return new KTAPI_Error('Unexpected failure updating document', $res); } $result = KTDocumentUtil::saveMetadata($this->document, $packed, array('novalidate' => true)); if (is_null($result)) { DBUtil::rollback(); return new PEAR_Error(KTAPI_ERROR_INTERNAL_ERROR . ': Null result returned but not expected.'); } if (PEAR::isError($result)) { DBUtil::rollback(); return new KTAPI_Error('Unexpected validation failure', $result); } DBUtil::commit(); $oKTTriggerRegistry = KTTriggerRegistry::getSingleton(); $aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate'); foreach ($aTriggers as $aTrigger) { $sTrigger = $aTrigger[0]; $oTrigger = new $sTrigger(); $aInfo = array("document" => $this->document, "aOptions" => $packed); $oTrigger->setInfo($aInfo); $ret = $oTrigger->postValidate(); } }
function &_add($oFolder, $sFilename, $oUser, $aOptions) { global $default; //$oContents = KTUtil::arrayGet($aOptions, 'contents'); $aMetadata = KTUtil::arrayGet($aOptions, 'metadata', null, false); $oDocumentType = KTUtil::arrayGet($aOptions, 'documenttype'); $sDescription = KTUtil::arrayGet($aOptions, 'description', ''); if (empty($sDescription)) { // If no document name is provided use the filename minus the extension $aFile = pathinfo($sFilename); $sDescription = isset($aFile['filename']) && !empty($aFile['filename']) ? $aFile['filename'] : $sFilename; } $oUploadChannel =& KTUploadChannel::getSingleton(); if ($oDocumentType) { $iDocumentTypeId = KTUtil::getId($oDocumentType); } else { $iDocumentTypeId = 1; } $oUploadChannel->sendMessage(new KTUploadGenericMessage(_kt('Creating database entry'))); $oDocument =& Document::createFromArray(array('name' => $sDescription, 'description' => $sDescription, 'filename' => $sFilename, 'folderid' => $oFolder->getID(), 'creatorid' => $oUser->getID(), 'documenttypeid' => $iDocumentTypeId)); $oUploadChannel->sendMessage(new KTUploadGenericMessage(_kt('Storing contents'))); $res = KTDocumentUtil::storeContents($oDocument, '', $aOptions); if (PEAR::isError($res)) { if (!PEAR::isError($oDocument)) { $oDocument->delete(); } return $res; } if (is_null($aMetadata)) { $res = KTDocumentUtil::setIncomplete($oDocument, 'metadata'); if (PEAR::isError($res)) { $oDocument->delete(); return $res; } } else { $oUploadChannel->sendMessage(new KTUploadGenericMessage(_kt('Saving metadata'))); $res = KTDocumentUtil::saveMetadata($oDocument, $aMetadata, $aOptions); if (PEAR::isError($res)) { $oDocument->delete(); return $res; } } // setIncomplete and storeContents may change the document's status or // storage_path, so now is the time to update $oDocument->update(); return $oDocument; }