function do_main() { // fix legacy, broken items. if (KTUtil::arrayGet($_REQUEST, 'fDocumentID', true) !== true) { $_REQUEST['fDocumentId'] = sanitizeForSQL(KTUtil::arrayGet($_REQUEST, 'fDocumentID')); unset($_REQUEST['fDocumentID']); } $document_data = array(); $document_id = sanitizeForSQL(KTUtil::arrayGet($_REQUEST, 'fDocumentId')); if ($document_id === null) { $this->oPage->addError(sprintf(_kt("No document was requested. Please <a href=\"%s\">browse</a> for one."), KTBrowseUtil::getBrowseBaseUrl())); return $this->do_error(); } // try get the document. $oDocument =& Document::get($document_id); if (PEAR::isError($oDocument)) { $this->oPage->addError(sprintf(_kt("The document you attempted to retrieve is invalid. Please <a href=\"%s\">browse</a> for one."), KTBrowseUtil::getBrowseBaseUrl())); $this->oPage->booleanLink = true; return $this->do_error(); } $document_id = $oDocument->getId(); $document_data['document_id'] = $oDocument->getId(); if (!KTBrowseUtil::inAdminMode($this->oUser, $oDocument->getFolderId())) { if ($oDocument->getStatusID() == ARCHIVED) { $this->oPage->addError(_kt('This document has been archived. Please contact the system administrator to have it restored if it is still needed.')); return $this->do_request($oDocument); } else { if ($oDocument->getStatusID() == DELETED) { $this->oPage->addError(_kt('This document has been deleted. Please contact the system administrator to have it restored if it is still needed.')); return $this->do_error(); } else { if (!Permission::userHasDocumentReadPermission($oDocument)) { $this->oPage->addError(_kt('You are not allowed to view this document')); return $this->permissionDenied(); } } } } if ($oDocument->getStatusID() == ARCHIVED) { $this->oPage->addError(_kt('This document has been archived.')); } else { if ($oDocument->getStatusID() == DELETED) { $this->oPage->addError(_kt('This document has been deleted.')); } } $this->oPage->setSecondaryTitle($oDocument->getName()); $aOptions = array('documentaction' => 'viewDocument', 'folderaction' => 'browse'); $this->oDocument =& $oDocument; //Figure out if we came here by navigating trough a shortcut. //If we came here from a shortcut, the breadcrumbspath should be relative //to the shortcut folder. $iSymLinkFolderId = KTUtil::arrayGet($_REQUEST, 'fShortcutFolder', null); if (is_numeric($iSymLinkFolderId)) { $oBreadcrumbsFolder = Folder::get($iSymLinkFolderId); $aOptions['final'] = false; $this->aBreadcrumbs = kt_array_merge($this->aBreadcrumbs, KTBrowseUtil::breadcrumbsForFolder($oBreadcrumbsFolder, $aOptions)); $this->aBreadcrumbs[] = array('name' => $this->oDocument->getName()); } else { $this->aBreadcrumbs = kt_array_merge($this->aBreadcrumbs, KTBrowseUtil::breadcrumbsForDocument($oDocument, $aOptions, $iSymLinkFolderId)); } $this->oPage->setBreadcrumbDetails(_kt('document details')); $this->addPortlets('Document Details'); $document_data['document'] = $oDocument; $document_data['document_type'] =& DocumentType::get($oDocument->getDocumentTypeID()); $is_valid_doctype = true; if (PEAR::isError($document_data['document_type'])) { $this->oPage->addError(_kt('The document you requested has an invalid <strong>document type</strong>. Unfortunately, this means that we cannot effectively display it.')); $is_valid_doctype = false; } // we want to grab all the md for this doc, since its faster that way. $mdlist =& DocumentFieldLink::getByDocument($oDocument); $field_values = array(); foreach ($mdlist as $oFieldLink) { $field_values[$oFieldLink->getDocumentFieldID()] = $oFieldLink->getValue(); } //var_dump($field_values); $document_data['field_values'] = $field_values; // Fieldset generation. // // we need to create a set of FieldsetDisplay objects // that adapt the Fieldsets associated with this lot // to the view (i.e. ZX3). Unfortunately, we don't have // any of the plumbing to do it, so we handle this here. $fieldsets = array(); // we always have a generic. array_push($fieldsets, new GenericFieldsetDisplay()); $fieldsetDisplayReg =& KTFieldsetDisplayRegistry::getSingleton(); $aDocFieldsets = KTMetadataUtil::fieldsetsForDocument($oDocument); foreach ($aDocFieldsets as $oFieldset) { $displayClass = $fieldsetDisplayReg->getHandler($oFieldset->getNamespace()); array_push($fieldsets, new $displayClass($oFieldset)); } $checkout_user = '******'; if ($oDocument->getIsCheckedOut() == 1) { $oCOU = User::get($oDocument->getCheckedOutUserId()); if (!(PEAR::isError($oCOU) || $oCOU == false)) { $checkout_user = $oCOU->getName(); } } // is the checkout action active? $bCanCheckin = false; foreach ($this->actions as $oDocAction) { $sActName = $oDocAction->sName; if ($sActName == 'ktcore.actions.document.cancelcheckout') { if ($oDocAction->getInfo()) { $bCanCheckin = true; } } } // viewlets. $aViewlets = array(); $aViewletActions = KTDocumentActionUtil::getDocumentActionsForDocument($this->oDocument, $this->oUser, 'documentviewlet'); foreach ($aViewletActions as $oAction) { $aInfo = $oAction->getInfo(); if ($aInfo !== null) { $aViewlets[] = $oAction->display_viewlet(); // use the action, since we display_viewlet() later. } } $viewlet_data = implode(' ', $aViewlets); $viewlet_data = trim($viewlet_data); $content_class = 'view'; if (!empty($viewlet_data)) { $content_class = 'view withviewlets'; } $this->oPage->setContentClass($content_class); $oTemplating =& KTTemplating::getSingleton(); $oTemplate = $oTemplating->loadTemplate('ktcore/document/view'); $aTemplateData = array('context' => $this, 'sCheckoutUser' => $checkout_user, 'isCheckoutUser' => $this->oUser->getId() == $oDocument->getCheckedOutUserId(), 'canCheckin' => $bCanCheckin, 'document_id' => $document_id, 'document' => $oDocument, 'documentName' => $oDocument->getName(), 'document_data' => $document_data, 'fieldsets' => $fieldsets, 'viewlet_data' => $viewlet_data); //return '<pre>' . print_r($aTemplateData, true) . '</pre>'; return $oTemplate->render($aTemplateData); }
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())); }
/** * Changes the document type of the document. * * @author KnowledgeTree Team * @access public * @param string $documenttype The new document type * @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure */ function change_document_type($documenttype) { $user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_WRITE); if (PEAR::isError($user)) { return $user; } $doctypeid = KTAPI::get_documenttypeid($documenttype); if (PEAR::isError($doctypeid)) { return $doctypeid; } 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(); if (PEAR::isError($res)) { DBUtil::rollback(); return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $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->document, $MDPack, array('novalidate' => true)); if (PEAR::isError($core_res)) { DBUtil::rollback(); return $core_res; } $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(); } DBUtil::commit(); } }