diff --git a/lib/metadata/metadatautil.inc.php b/lib/metadata/metadatautil.inc.php index cdfd161..f48748f 100644 --- a/lib/metadata/metadatautil.inc.php +++ b/lib/metadata/metadatautil.inc.php @@ -671,11 +671,14 @@ class KTMetadataUtil { // }}} // {{{ fieldsetsForDocument - function fieldsetsForDocument($oDocument) { + function fieldsetsForDocument($oDocument, $iTypeOverride = null) { global $default; $oDocument = KTUtil::getObject('Document', $oDocument); $iMetadataVersionId = $oDocument->getMetadataVersionId(); $iDocumentTypeId = $oDocument->getDocumentTypeId(); + if (!is_null($iTypeOverride)) { + $iDocumentTypeId = $iTypeOverride; + } $sQuery = "SELECT DISTINCT F.id AS fieldset_id " . "FROM $default->document_metadata_version_table AS DM INNER JOIN document_fields_link AS DFL ON DM.id = DFL.metadata_version_id " . diff --git a/plugins/ktcore/document/edit.php b/plugins/ktcore/document/edit.php index 7622c35..9d5c6d3 100644 --- a/plugins/ktcore/document/edit.php +++ b/plugins/ktcore/document/edit.php @@ -58,6 +58,10 @@ class KTDocumentEditAction extends KTDocumentAction { return _kt('Edit Metadata'); } + function predispatch() { + $this->persistParams(array('new_type')); + } + function form_edit() { $oForm = new KTForm; $oForm->setOptions(array( @@ -74,6 +78,12 @@ class KTDocumentEditAction extends KTDocumentAction { $oFReg =& KTFieldsetRegistry::getSingleton(); $doctypeid = $this->oDocument->getDocumentTypeID(); + if ($_REQUEST['new_type']) { + $oTestType = DocumentType::get($_REQUEST['new_type']); + if (!PEAR::isError($oTestType)) { + $doctypeid = $oTestType->getId(); + } + } $widgets = array( array('ktcore.widgets.string', array( @@ -90,7 +100,7 @@ class KTDocumentEditAction extends KTDocumentAction { 'output' => 'document_title', )), ); - $fieldsets = (array) KTMetadataUtil::fieldsetsForDocument($this->oDocument); + $fieldsets = (array) KTMetadataUtil::fieldsetsForDocument($this->oDocument, $doctypeid); foreach ($fieldsets as $oFieldset) { $widgets = kt_array_merge($widgets, $oFReg->widgetsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument)); @@ -103,19 +113,22 @@ class KTDocumentEditAction extends KTDocumentAction { return $oForm; } - function do_main() { - $this->addErrorMessage("Doctype changing regressed."); - + function do_main() { $this->oPage->setBreadcrumbDetails("Edit Metadata"); $oTemplate = $this->oValidator->validateTemplate('ktcore/document/edit'); - + + $doctypeid = $this->oDocument->getDocumentTypeID(); + $type = DocumentType::get($doctypeid); + + $oForm = $this->form_edit(); $oTemplate->setData(array( 'context' => $this, 'form' => $oForm, 'document' => $this->oDocument, + 'type_name' => $type->getName(), )); return $oTemplate->render(); } @@ -140,8 +153,16 @@ class KTDocumentEditAction extends KTDocumentAction { // ); // // we do this the "easy" way. + $doctypeid = $this->oDocument->getDocumentTypeId(); + if ($_REQUEST['new_type']) { + $oTestType = DocumentType::get($_REQUEST['new_type']); + if (!PEAR::isError($oTestType)) { + $doctypeid = $oTestType->getId(); + } + } + - $fieldsets = KTMetadataUtil::fieldsetsForDocument($this->oDocument); + $fieldsets = KTMetadataUtil::fieldsetsForDocument($this->oDocument, $doctypeid); $MDPack = array(); foreach ($fieldsets as $oFieldset) { @@ -162,6 +183,9 @@ class KTDocumentEditAction extends KTDocumentAction { } $this->startTransaction(); + if ($this->oDocument->getDocumentTypeId() != $doctypeid) { + $this->oDocument->setDocumentTypeId($doctypeid); + } $this->oDocument->setName($data['document_title']); $res = $this->oDocument->update(); if (PEAR::isError($res)) { @@ -190,9 +214,66 @@ class KTDocumentEditAction extends KTDocumentAction { redirect(KTBrowseUtil::getUrlForDocument($this->oDocument->getId())); exit(0); - } + function form_changetype() { + $oForm = new KTForm; + $oForm->setOptions(array( + 'label' => _kt("Change Document Type"), + 'description' => _kt("Changing the document type will allow different metadata to be associated with it."), + 'identifier' => 'ktcore.doc.edit.typechange', + 'submit_label' => _kt("Update Document"), + 'context' => $this, + 'cancel_action' => 'main', + 'action' => 'trytype', + )); + + $type = DocumentType::get($this->oDocument->getDocumentTypeId()); + $current_type_name = $type->getName(); + $oFolder = Folder::get($this->oDocument->getFolderID()); + + $oForm->setWidgets(array( + array('ktcore.widgets.entityselection',array( + 'label' => _kt("New Document Type"), + 'description' => _kt("Please select the new type for this document."), + 'important_description' => sprintf(_kt("The document is currently of type \"%s\"."), $current_type_name), + 'value' => $type->getId(), + 'label_method' => 'getName', + 'vocab' => DocumentType::getListForUserAndFolder($this->oUser, $oFolder), + 'simple_select' => false, + 'required' => true, + 'name' => 'type' + )), + )); + + $oForm->setValidators(array( + array('ktcore.validators.entity', array( + 'test' => 'type', + 'output' => 'type', + 'class' => 'DocumentType', + )), + )); + + return $oForm; + } + + function do_selecttype() { + $oForm = $this->form_changetype(); + return $oForm->renderPage(_kt("Change Document Type")); + } + + function do_trytype() { + $oForm = $this->form_changetype(); + $res = $oForm->validate(); + $data = $res['results']; + $errors = $res['errors']; + + if (!empty($errors)) { + $oForm->handleError(); + } + + $this->successRedirectToMain(sprintf(_kt("You have selected a new document type: %s. Please note that this change has not yet been saved - please update the metadata below as necessary, and then save the document to make it a permanent change."), $data['type']->getName()), array('new_type' => $data['type']->getId())); + } } // }}} diff --git a/templates/ktcore/document/edit.smarty b/templates/ktcore/document/edit.smarty index 39229eb..5904675 100644 --- a/templates/ktcore/document/edit.smarty +++ b/templates/ktcore/document/edit.smarty @@ -14,7 +14,7 @@

{i18n}Editing{/i18n}: {$document->getName()}

{capture assign=link}{addQS}action=selectType&fDocumentId={$document->getId()}{/addQS}{/capture} -

{i18n arg_link=$link}Change the document type{/i18n}

+

{i18n arg_link=$link arg_name=$type_name}Change the document type. The current type is "#name#"{/i18n}

{i18n}The following document metadata is available for editing.{/i18n}