documentutil.inc.php.tmp

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

TMP
1,528
字号
    function copy($oDocument, $oDestinationFolder, $sReason = null, $sDestinationDocName = null) {        // 1. generate a new triad of content, metadata and core objects.        // 2. update the storage path.		//print '--------------------------------- BEFORE';        //print_r($oDocument);        // grab the "source "data        $sTable = KTUtil::getTableName('documents');        $sQuery = 'SELECT * FROM ' . $sTable . ' WHERE id = ?';        $aParams = array($oDocument->getId());        $aCoreRow = DBUtil::getOneResult(array($sQuery, $aParams));        unset($aCoreRow['id']);        $aCoreRow['folder_id'] = $oDestinationFolder->getId(); // new location.        $id = DBUtil::autoInsert($sTable, $aCoreRow);        if (PEAR::isError($id)) { return $id; }        // we still have a bogus md_version, but integrity holds, so fix it now.        $oCore = KTDocumentCore::get($id);        // Get the metadata version for the source document        $sTable = KTUtil::getTableName('document_metadata_version');        $sQuery = 'SELECT * FROM ' . $sTable . ' WHERE id = ?';        $aParams = array($oDocument->getMetadataVersionId());        $aMDRow = DBUtil::getOneResult(array($sQuery, $aParams));        unset($aMDRow['id']);        // Copy the source metadata into the destination document        $aMDRow['document_id'] = $oCore->getId();        if(!empty($sDestinationDocName)){            $aMDRow['name'] = $sDestinationDocName;            $aMDRow['description'] = $sDestinationDocName;        }        $id = DBUtil::autoInsert($sTable, $aMDRow);        if (PEAR::isError($id)) { return $id; }        $oCore->setMetadataVersionId($id);        $oMDV = KTDocumentMetadataVersion::get($id);        // Get the content version for the source document        $sTable = KTUtil::getTableName('document_content_version');        $sQuery = 'SELECT * FROM ' . $sTable . ' WHERE id = ?';        $aParams = array($oDocument->_oDocumentContentVersion->getId());        $aContentRow = DBUtil::getOneResult(array($sQuery, $aParams));        unset($aContentRow['id']);        // Copy the source content into the destination document        $aContentRow['document_id'] = $oCore->getId();        if(!empty($sDestinationDocName)){            $aContentRow['filename'] = $sDestinationDocName;        }        $id = DBUtil::autoInsert($sTable, $aContentRow);        if (PEAR::isError($id)) { return $id; }        $oMDV->setContentVersionId($id);        $res = $oCore->update();        if (PEAR::isError($res)) { return $res; }        $res = $oMDV->update();        if (PEAR::isError($res)) { return $res; }        // now, we have a semi-sane document object. get it.        $oNewDocument = Document::get($oCore->getId());        //print '--------------------------------- AFTER';        //print_r($oDocument);		//print '======';        //print_r($oNewDocument);        // copy the metadata from old to new.        $res = KTDocumentUtil::copyMetadata($oNewDocument, $oDocument->getMetadataVersionId());        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; }        // NEW SEARCH        /*        $sTable = KTUtil::getTableName('document_text');        $aQuery = array("SELECT document_text FROM $sTable WHERE document_id = ?", array($oDocument->getId()));        $sData = DBUtil::getOneResultKey($aQuery, 'document_text');        $aInsertValues = array(            'document_id' => $oNewDocument->getId(),            'document_text' => $contents,        );        DBUtil::autoInsert($sTable, $aInsertValues, array('noid' => true));        */        KTDocumentUtil::updateSearchableText($oNewDocument);        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();        // 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());        $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)) {            DBUtil::rollback();            return PEAR::raiseError(_kt('Invalid document content version object.'));        }        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 + -
显示快捷键?