documentutil.inc.php

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

PHP
1,474
字号
        // create the new document record
        $aCoreRow['modified'] = date('Y-m-d H:i:s');
        $aCoreRow['folder_id'] = $oDestinationFolder->getId(); // new location.
        $id = DBUtil::autoInsert($sDocumentTable, $aCoreRow);
        if (PEAR::isError($id)) { return $id; }
        $iNewDocumentId = $id;

        // create the new metadata record
        $aMDRow['document_id'] = $iNewDocumentId;
        $aMDRow['description'] = $aMDRow['name'];
        $id = DBUtil::autoInsert($sMetadataTable, $aMDRow);
        if (PEAR::isError($id)) { return $id; }
        $iNewMetadataId = $id;

        // the document metadata version is still pointing to the original
        $aCoreUpdate = array();
        $aCoreUpdate['metadata_version_id'] = $iNewMetadataId;
        $aCoreUpdate['metadata_version'] = 0;

        // create the new content version
        $aContentRow['document_id'] = $iNewDocumentId;
        $id = DBUtil::autoInsert($sContentTable, $aContentRow);
        if (PEAR::isError($id)) { return $id; }
        $iNewContentId = $id;

        // the metadata content version is still pointing to the original
        $aMetadataUpdate = array();
        $aMetadataUpdate['content_version_id'] = $iNewContentId;
        $aMetadataUpdate['metadata_version'] = 0;

        // apply the updates to the document and metadata records
        $res = DBUtil::autoUpdate($sDocumentTable, $aCoreUpdate, $iNewDocumentId);
        if (PEAR::isError($res)) { return $res; }

        $res = DBUtil::autoUpdate($sMetadataTable, $aMetadataUpdate, $iNewMetadataId);
        if (PEAR::isError($res)) { return $res; }

        // now, we have a semi-sane document object. get it.
        $oNewDocument = Document::get($iNewDocumentId);

        //print '--------------------------------- AFTER';
        //print_r($oDocument);
		//print '======';
        //print_r($oNewDocument);

        // copy the metadata from old to new.
        $res = KTDocumentUtil::copyMetadata($oNewDocument, $iOldMetadataId);
        if (PEAR::isError($res)) { return $res; }

        // Ensure the copied document is not checked out
        $oNewDocument->setIsCheckedOut(false);
        $oNewDocument->setCheckedOutUserID(-1);

        // finally, copy the actual file.
        $oStorage =& KTStorageManagerUtil::getSingleton();
        $res = $oStorage->copy($oDocument, $oNewDocument);

        $oOriginalFolder = Folder::get($oDocument->getFolderId());
        $iOriginalFolderPermissionObjectId = $oOriginalFolder->getPermissionObjectId();
        $iDocumentPermissionObjectId = $oDocument->getPermissionObjectId();

        if ($iDocumentPermissionObjectId === $iOriginalFolderPermissionObjectId) {
            $oNewDocument->setPermissionObjectId($oDestinationFolder->getPermissionObjectId());
        }

        $res = $oNewDocument->update();
        if (PEAR::isError($res)) { return $res; }

        KTPermissionUtil::updatePermissionLookup($oNewDocument);

        if (is_null($sReason)) {
            $sReason = '';
        }

        $oDocumentTransaction = new DocumentTransaction($oDocument, sprintf(_kt("Copied to folder \"%s\". %s"), $oDestinationFolder->getName(), $sReason), 'ktcore.transactions.copy');
        $oDocumentTransaction->create();

        $oSrcFolder = Folder::get($oDocument->getFolderID());
        $oDocumentTransaction = new DocumentTransaction($oNewDocument, sprintf(_kt("Copied from original in folder \"%s\". %s"), $oSrcFolder->getName(), $sReason), 'ktcore.transactions.copy');
        $oDocumentTransaction->create();


        $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
        $aTriggers = $oKTTriggerRegistry->getTriggers('copyDocument', 'postValidate');
        foreach ($aTriggers as $aTrigger) {
            $sTrigger = $aTrigger[0];
            $oTrigger = new $sTrigger;
            $aInfo = array(
                'document' => $oNewDocument,
                'old_folder' => $oSrcFolder,
                'new_folder' => $oDestinationFolder,
            );
            $oTrigger->setInfo($aInfo);
            $ret = $oTrigger->postValidate();
            if (PEAR::isError($ret)) {
                return $ret;
            }
        }

        // fire subscription alerts for the copied document
        $oSubscriptionEvent = new SubscriptionEvent();
        $oFolder = Folder::get($oDocument->getFolderID());
        $oSubscriptionEvent->MoveDocument($oDocument, $oDestinationFolder, $oSrcFolder, 'CopiedDocument');

        return $oNewDocument;
    }

    function rename($oDocument, $sNewFilename, $oUser) {
        $oStorage =& KTStorageManagerUtil::getSingleton();

        $iPreviousMetadataVersion = $oDocument->getMetadataVersionId();
        $oOldContentVersion = $oDocument->_oDocumentContentVersion;
        $bSuccess = $oDocument->startNewContentVersion($oUser);
        if (PEAR::isError($bSuccess)) {
            return $bSuccess;
        }
        KTDocumentUtil::copyMetadata($oDocument, $iPreviousMetadataVersion);
        $res = $oStorage->renameDocument($oDocument, $oOldContentVersion, $sNewFilename);

        if (!$res) {
            return PEAR::raiseError(_kt('An error occurred while storing the new file'));
        }

        $oDocument->setLastModifiedDate(getCurrentDateTime());
        $oDocument->setModifiedUserId($oUser->getId());
        $oDocument->setMinorVersionNumber($oDocument->getMinorVersionNumber()+1);
		$oDocument->_oDocumentContentVersion->setFilename($sNewFilename);

		$sType = KTMime::getMimeTypeFromFile($sNewFilename);
		$iMimeTypeId = KTMime::getMimeTypeID($sType, $sNewFilename);
        $oDocument->setMimeTypeId($iMimeTypeId);

        $bSuccess = $oDocument->update();
        if ($bSuccess !== true) {
            if (PEAR::isError($bSuccess)) {
                return $bSuccess;
            }
            return PEAR::raiseError(_kt('An error occurred while storing this document in the database'));
        }

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

        $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
        $aTriggers = $oKTTriggerRegistry->getTriggers('renameDocument', 'postValidate');
        foreach ($aTriggers as $aTrigger) {
            $sTrigger = $aTrigger[0];
            $oTrigger = new $sTrigger;
            $aInfo = array(
                'document' => $oDocument
            );
            $oTrigger->setInfo($aInfo);
            $ret = $oTrigger->postValidate();
            if (PEAR::isError($ret)) {
                return $ret;
            }
        }

        // fire subscription alerts for the checked in document
        $oSubscriptionEvent = new SubscriptionEvent();
        $oFolder = Folder::get($oDocument->getFolderID());
        $oSubscriptionEvent->ModifyDocument($oDocument, $oFolder);

        return true;
    }

    function move($oDocument, $oToFolder, $oUser = null, $sReason = null) {
    	//make sure we move the symlink, and the document it's linking to
		if($oDocument->isSymbolicLink()){
    		$oDocument->switchToRealCore();
    	}else{
    		$oDocument->switchToLinkedCore();
    	}
        $oFolder = $oToFolder; // alias.

        $oOriginalFolder = Folder::get($oDocument->getFolderId());
        $iOriginalFolderPermissionObjectId = $oOriginalFolder->getPermissionObjectId();
        $iDocumentPermissionObjectId = $oDocument->getPermissionObjectId();

        if ($iDocumentPermissionObjectId === $iOriginalFolderPermissionObjectId) {
            $oDocument->setPermissionObjectId($oFolder->getPermissionObjectId());
        }

        //put the document in the new folder
        $oDocument->setFolderID($oFolder->getId());
        $sName = $oDocument->getName();
        $sFilename = $oDocument->getFileName();

        $oDocument->setFileName(KTDocumentUtil::getUniqueFilename($oToFolder, $sFilename));
        $oDocument->setName(KTDocumentUtil::getUniqueDocumentName($oToFolder, $sName));

        $res = $oDocument->update();
        if (PEAR::isError($res)) {
            return $res;
        }

        //move the document on the file system(not if it's a symlink)
        if(!$oDocument->isSymbolicLink()){
	        $oStorage =& KTStorageManagerUtil::getSingleton();
	        $res = $oStorage->moveDocument($oDocument, $oFolder, $oOriginalFolder);
	        if (PEAR::isError($res) || ($res === false)) {
	            $oDocument->setFolderID($oOriginalFolder->getId());
	            $res = $oDocument->update();
	            if (PEAR::isError($res)) {
	                return $res;
	            }
	            return $res; // we failed, bail.
	        }
        }

        $sMoveMessage = sprintf(_kt("Moved from %s/%s to %s/%s. %s"),
            $oOriginalFolder->getFullPath(),
            $oOriginalFolder->getName(),
            $oFolder->getFullPath(),
            $oFolder->getName(),
            $sReason);

        // create the document transaction record

        $oDocumentTransaction = new DocumentTransaction($oDocument, $sMoveMessage, 'ktcore.transactions.move');
        $oDocumentTransaction->create();


        $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
        $aTriggers = $oKTTriggerRegistry->getTriggers('moveDocument', 'postValidate');
        foreach ($aTriggers as $aTrigger) {
            $sTrigger = $aTrigger[0];
            $oTrigger = new $sTrigger;
            $aInfo = array(
                'document' => $oDocument,
                'old_folder' => $oOriginalFolder,
                'new_folder' => $oFolder,
            );
            $oTrigger->setInfo($aInfo);
            $ret = $oTrigger->postValidate();
            if (PEAR::isError($ret)) {
                return $ret;
            }
        }

        // fire subscription alerts for the moved document
        $oSubscriptionEvent = new SubscriptionEvent();
        $oSubscriptionEvent->MoveDocument($oDocument, $oFolder, $oOriginalFolder);

        return KTPermissionUtil::updatePermissionLookup($oDocument);
    }

    /**
    * Delete a selected version of the document.
    */
    function deleteVersion($oDocument, $iVersionID, $sReason){

        $oDocument =& KTUtil::getObject('Document', $oDocument);
        $oVersion =& KTDocumentMetadataVersion::get($iVersionID);

        $oStorageManager =& KTStorageManagerUtil::getSingleton();

        global $default;

        if (empty($sReason)) {
            return PEAR::raiseError(_kt('Deletion requires a reason'));
        }

        if (PEAR::isError($oDocument) || ($oDocument == false)) {
            return PEAR::raiseError(_kt('Invalid document object.'));
        }

        if (PEAR::isError($oVersion) || ($oVersion == false)) {
            return PEAR::raiseError(_kt('Invalid document version object.'));
        }

        $iContentId = $oVersion->getContentVersionId();
        $oContentVersion = KTDocumentContentVersion::get($iContentId);

        if (PEAR::isError($oContentVersion) || ($oContentVersion == false)) {
            return PEAR::raiseError(_kt('Invalid document content version object.'));
        }

        // Check that the document content is not the same as the current content version
        $sDocStoragePath = $oDocument->getStoragePath();
        $sVersionStoragePath = $oContentVersion->getStoragePath();

        if($sDocStoragePath == $sVersionStoragePath){
            return PEAR::raiseError(_kt("Can't delete version: content is the same as the current document content."));
        }

        DBUtil::startTransaction();

        // now delete the document version
        $res = $oStorageManager->deleteVersion($oVersion);
        if (PEAR::isError($res) || ($res == false)) {
            //could not delete the document version from the file system
            $default->log->error('Deletion: Filesystem error deleting the metadata version ' .
                $oVersion->getMetadataVersion() . ' of the document ' .
                $oDocument->getFileName() . ' from folder ' .
                Folder::getFolderPath($oDocument->getFolderID()) .
                ' id=' . $oDocument->getFolderID());

            // we use a _real_ transaction here ...

            DBUtil::rollback();

            return PEAR::raiseError(_kt('There was a problem deleting the document from storage.'));
        }

        // change status for the metadata version
        $oVersion->setStatusId(VERSION_DELETED);
        $oVersion->update();

        // set the storage path to empty
//        $oContentVersion->setStoragePath('');

        DBUtil::commit();
    }
}

class KTMetadataValidationError extends PEAR_Error {
    function KTMetadataValidationError ($aFailed) {
        $this->aFailed = $aFailed;
        $message = _kt('Please be sure to enter information for all the Required fields below');
        parent::PEAR_Error($message);
    }
}

class KTUploadChannel {
    var $observers = array();

    function &getSingleton() {
        if (!KTUtil::arrayGet($GLOBALS, 'KT_UploadChannel')) {
            $GLOBALS['KT_UploadChannel'] = new KTUploadChannel;
        }
        return $GLOBALS['KT_UploadChannel'];
    }

    function sendMessage(&$msg) {
        foreach ($this->observers as $oObserver) {
            $oObserver->receiveMessage($msg);
        }
    }

    function addObserver(&$obs) {
        array_push($this->observers, $obs);
    }
}

class KTUploadGenericMessage {
    function KTUploadGenericMessage($sMessage) {
        $this->sMessage = $sMessage;
    }

    function getString() {
        return $this->sMessage;
    }
}

class KTUploadNewFile {
    function KTUploadNewFile($sFilename) {
        $this->sFilename = $sFilename;
    }

    function getString() {
        return $this->sFilename;
    }
}

?>

⌨️ 快捷键说明

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