edit.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 405 行 · 第 1/2 页

PHP
405
字号
        $this->oDocument->setModifiedUserId($this->oUser->getId());

        // Update the content version / document version
        global $default;
        if($default->updateContentVersion){
            $this->oDocument->startNewContentVersion($this->oUser);
            $this->oDocument->setMinorVersionNumber($this->oDocument->getMinorVersionNumber()+1);
        }

        $res = $this->oDocument->update();
        if (PEAR::isError($res)) {
            $oForm->handleError(sprintf(_kt("Unexpected failure to update document title: %s"), $res->getMessage()));
        }
        $core_res = KTDocumentUtil::saveMetadata($this->oDocument, $MDPack);
        if (PEAR::isError($core_res)) {
            $oForm->handleError(sprintf(_kt("Unexpected validation failure: %s."), $core_res->getMessage()));
        }

        // post-triggers.
        $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();
        }

        $this->commitTransaction();

        // create the document transaction record
        $oDocumentTransaction = & new DocumentTransaction($this->oDocument, _kt('Document metadata updated'), 'ktcore.transactions.update');
        $oDocumentTransaction->create();

        // 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);
        }

        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();
        }

        $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()));
    }
}
// }}}

?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?