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())); }
function do_viewComparison() { $document_data = array(); $document_id = 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(); } $document_data['document_id'] = $document_id; $base_version = KTUtil::arrayGet($_REQUEST, 'fBaseVersion'); // try get the document. $oDocument =& Document::get($document_id, $base_version); if (PEAR::isError($oDocument)) { $this->oPage->addError(sprintf(_kt("The base document you attempted to retrieve is invalid. Please <a href=\"%s\">browse</a> for one."), KTBrowseUtil::getBrowseBaseUrl())); return $this->do_error(); } if (!Permission::userHasDocumentReadPermission($oDocument)) { // FIXME inconsistent. $this->oPage->addError(_kt('You are not allowed to view this document')); return $this->permissionDenied(); } $this->oDocument =& $oDocument; $this->oPage->setSecondaryTitle($oDocument->getName()); $aOptions = array('documentaction' => 'viewDocument', 'folderaction' => 'browse'); $this->aBreadcrumbs = kt_array_merge($this->aBreadcrumbs, KTBrowseUtil::breadcrumbsForDocument($oDocument, $aOptions)); $this->oPage->setBreadcrumbDetails(_kt('compare versions')); $comparison_version = KTUtil::arrayGet($_REQUEST, 'fComparisonVersion'); if ($comparison_version === null) { $this->oPage->addError(sprintf(_kt("No comparison version was requested. Please <a href=\"%s\">select a version</a>."), KTUtil::addQueryStringSelf('action=history&fDocumentId=' . $document_id))); return $this->do_error(); } $oComparison =& Document::get($oDocument->getId(), $comparison_version); if (PEAR::isError($oComparison)) { $this->errorRedirectToMain(_kt('Invalid document to compare against.')); } $comparison_data = array(); $comparison_data['document_id'] = $oComparison->getId(); $document_data['document'] = $oDocument; $comparison_data['document'] = $oComparison; $document_data['document_type'] =& DocumentType::get($oDocument->getDocumentTypeID()); $comparison_data['document_type'] =& DocumentType::get($oComparison->getDocumentTypeID()); // follow twice: once for normal, once for comparison. $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::getList(array('metadata_version_id = ?', array($base_version))); $field_values = array(); foreach ($mdlist as $oFieldLink) { $field_values[$oFieldLink->getDocumentFieldID()] = $oFieldLink->getValue(); } $document_data['field_values'] = $field_values; $mdlist =& DocumentFieldLink::getList(array('metadata_version_id = ?', array($comparison_version))); $field_values = array(); foreach ($mdlist as $oFieldLink) { $field_values[$oFieldLink->getDocumentFieldID()] = $oFieldLink->getValue(); } $comparison_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()); // FIXME can we key this on fieldset namespace? or can we have duplicates? // now we get the other fieldsets, IF there is a valid doctype. if ($is_valid_doctype) { // these are the _actual_ fieldsets. $fieldsetDisplayReg =& KTFieldsetDisplayRegistry::getSingleton(); // and the generics $activesets = KTFieldset::getGenericFieldsets(); foreach ($activesets as $oFieldset) { $displayClass = $fieldsetDisplayReg->getHandler($oFieldset->getNamespace()); array_push($fieldsets, new $displayClass($oFieldset)); } $activesets = KTFieldset::getForDocumentType($oDocument->getDocumentTypeID()); foreach ($activesets as $oFieldset) { $displayClass = $fieldsetDisplayReg->getHandler($oFieldset->getNamespace()); array_push($fieldsets, new $displayClass($oFieldset)); } } // FIXME handle ad-hoc fieldsets. $this->addPortlets(); $oTemplate = $this->oValidator->validateTemplate('ktcore/document/compare'); $aTemplateData = array('context' => $this, 'document_id' => $document_id, 'document' => $oDocument, 'document_data' => $document_data, 'comparison_data' => $comparison_data, 'comparison_document' => $oComparison, 'fieldsets' => $fieldsets); //var_dump($aTemplateData['comparison_data']); return $oTemplate->render($aTemplateData); }
function widgetsForFieldset($fieldsetOrType, $sContainerName, $oDocument = null) { // this is likely to be called repeatedly. if (is_null($this->oWF)) { $this->oWF =& KTWidgetFactory::getSingleton(); } // we're going to create one of two things, here: // - conditional fieldset widget // - a "Fieldset" widget // FIXME delegate. $oFieldset =& $fieldsetOrType; $widgets = array(); $fields = $oFieldset->getFields(); if ($oFieldset->getIsConditional()) { $iMasterId = $oFieldset->getMasterFieldId(); $oMasterField = DocumentField::get($iMasterId); $newfields = array(); $newfields[] = $oMasterField; foreach ($fields as $oField) { if ($oField->getId() != $iMasterId) { $newfields[] = $oField; } } foreach ($newfields as $oField) { $fname = 'metadata_' . $oField->getId(); $value = null; if (!is_null($oDocument)) { $oFL = DocumentFieldLink::getByDocumentAndField($oDocument, $oField); if (!is_null($oFL) && !PEAR::isError($oFL)) { $value = $oFL->getValue(); } } $widgets[] = $this->oWF->get('ktcore.widgets.conditionalselection', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription(), 'vocab' => MetaData::getEnabledByDocumentField($oField), 'id_method' => 'getName', 'label_method' => 'getName', 'unselected_label' => _kt("No selection."), 'simple_select' => false, 'master' => $oField->getId() == $iMasterId, 'masterid' => $iMasterId, 'fieldset' => $oFieldset->getId(), 'field' => $oField->getId())); } } else { foreach ($fields as $oField) { $fname = 'metadata_' . $oField->getId(); $value = null; // check if we had an old value if (!is_null($oDocument)) { $oFL = DocumentFieldLink::getByDocumentAndField($oDocument, $oField); if (!is_null($oFL) && !PEAR::isError($oFL)) { $value = $oFL->getValue(); } } // we have to hack in support for the hardcoded types of fields // handled by the "generic" widget. // // we try to make this a little more "sane" $type = ''; if ($oField->getHasLookup()) { if ($oField->getHasLookupTree()) { $type = 'ktcore.fields.tree'; } else { $type = 'ktcore.fields.lookup'; } } else { $type = 'ktcore.fields.string'; } if ($type == 'ktcore.fields.string') { $widgets[] = $this->oWF->get('ktcore.widgets.string', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription())); } else { if ($type == 'ktcore.fields.lookup') { $widgets[] = $this->oWF->get('ktcore.widgets.entityselection', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription(), 'vocab' => MetaData::getEnabledByDocumentField($oField), 'id_method' => 'getName', 'label_method' => 'getName', 'unselected_label' => _kt("No selection."), 'simple_select' => false)); } else { if ($type == 'ktcore.fields.tree') { $widgets[] = $this->oWF->get('ktcore.widgets.treemetadata', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription(), 'vocab' => MetaData::getEnabledByDocumentField($oField), 'field_id' => $oField->getId())); } } } } } return array($this->oWF->get('ktcore.widgets.fieldset', array('label' => $oFieldset->getName(), 'description' => $oFieldset->getDescription(), 'name' => $sContainerName, 'widgets' => $widgets))); }
/** * This returns all metadata for the document. * * <code> * $ktapi = new KTAPI(); * $session = $ktapi->start_system_session(); * $document = $ktapi->get_document_by_id($documentid); * $metadata = $document->get_metadata(); * foreach($metadata as $fieldset){ * echo '<br><br>Fieldset: '.$fieldset['fieldset']; * * foreach($fieldset['fields'] as $field){ * echo '<br>Field name: '.$field['name'] . ' Value: '. $field['value']; * } * } * </code> * * @author KnowledgeTree Team * @access public * @return array An array of metadata fieldsets and fields */ function get_metadata() { $doctypeid = $this->document->getDocumentTypeID(); $fieldsets = (array) KTMetadataUtil::fieldsetsForDocument($this->document, $doctypeid); if (is_null($fieldsets) || PEAR::isError($fieldsets)) { return array(); } $results = array(); foreach ($fieldsets as $fieldset) { if ($fieldset->getIsConditional()) { /* this is not implemented...*/ continue; } $fields = $fieldset->getFields(); $result = array('fieldset' => $fieldset->getName(), 'description' => $fieldset->getDescription()); $fieldsresult = array(); foreach ($fields as $field) { $value = ''; $fieldvalue = DocumentFieldLink::getByDocumentAndField($this->document, $field); if (!is_null($fieldvalue) && !PEAR::isError($fieldvalue)) { $value = $fieldvalue->getValue(); } $controltype = 'string'; if ($field->getHasLookup()) { $controltype = 'lookup'; if ($field->getHasLookupTree()) { $controltype = 'tree'; } } switch ($controltype) { case 'lookup': $selection = KTAPI::get_metadata_lookup($field->getId()); break; case 'tree': $selection = KTAPI::get_metadata_tree($field->getId()); break; default: $selection = array(); } $fieldsresult[] = array('name' => $field->getName(), 'required' => $field->getIsMandatory(), 'value' => $value == '' ? 'n/a' : $value, 'blankvalue' => $value == '' ? '1' : '0', 'description' => $field->getDescription(), 'control_type' => $controltype, 'selection' => $selection); } $result['fields'] = $fieldsresult; $results[] = $result; } return $results; }
function saveMetadata(&$oDocument, $aMetadata, $aOptions = null) { $table = 'document_fields_link'; $bNoValidate = KTUtil::arrayGet($aOptions, 'novalidate', false); if ($bNoValidate !== true) { $res = KTDocumentUtil::validateMetadata($oDocument, $aMetadata); if (PEAR::isError($res)) { return $res; } $aMetadata = empty($res) ? array() : $res; } $iMetadataVersionId = $oDocument->getMetadataVersionId(); $res = DBUtil::runQuery(array("DELETE FROM {$table} WHERE metadata_version_id = ?", array($iMetadataVersionId))); if (PEAR::isError($res)) { return $res; } // XXX: Metadata refactor foreach ($aMetadata as $aInfo) { list($oMetadata, $sValue) = $aInfo; if (is_null($oMetadata)) { continue; } $res = DBUtil::autoInsert($table, array('metadata_version_id' => $iMetadataVersionId, 'document_field_id' => $oMetadata->getID(), 'value' => $sValue)); if (PEAR::isError($res)) { return $res; } } KTDocumentUtil::setComplete($oDocument, 'metadata'); DocumentFieldLink::clearAllCaches(); return true; }