documentutil.inc.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,474 行 · 第 1/4 页
PHP
1,474 行
<?php
/**
* $Id: documentutil.inc.php 9533 2008-10-09 14:07:04Z kevin_fourie $
*
* Document-handling utility functions
*
* Simplifies and canonicalises operations such as adding, updating, and
* deleting documents from the repository.
*
* KnowledgeTree Community Edition
* Document Management Made Simple
* Copyright (C) 2008 KnowledgeTree Inc.
* Portions copyright The Jam Warehouse Software (Pty) Limited
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
* California 94120-7775, or email info@knowledgetree.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* KnowledgeTree" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by KnowledgeTree" and retain the original
* copyright notice.
* Contributor( s): ______________________________________
*/
// LEGACY PATHS
require_once(KT_LIB_DIR . '/documentmanagement/DocumentFieldLink.inc');
require_once(KT_LIB_DIR . '/documentmanagement/DocumentTransaction.inc');
require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php');
// NEW PATHS
require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php');
require_once(KT_LIB_DIR . '/filelike/filelikeutil.inc.php');
require_once(KT_LIB_DIR . '/metadata/metadatautil.inc.php');
require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
require_once(KT_LIB_DIR . '/subscriptions/subscriptions.inc.php');
require_once(KT_LIB_DIR . '/triggers/triggerregistry.inc.php');
require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
require_once(KT_LIB_DIR . '/alert/EmailTemplate.inc.php');
require_once(KT_LIB_DIR . '/browse/browseutil.inc.php');
// WORKFLOW
require_once(KT_LIB_DIR . '/workflow/workflowutil.inc.php');
class KTDocumentUtil {
function checkin($oDocument, $sFilename, $sCheckInComment, $oUser, $aOptions = false) {
$oStorage =& KTStorageManagerUtil::getSingleton();
$iFileSize = filesize($sFilename);
$iPreviousMetadataVersion = $oDocument->getMetadataVersionId();
$bSuccess = $oDocument->startNewContentVersion($oUser);
if (PEAR::isError($bSuccess)) {
return $bSuccess;
}
KTDocumentUtil::copyMetadata($oDocument, $iPreviousMetadataVersion);
$aOptions['temp_file'] = $sFilename;
$res = KTDocumentUtil::storeContents($oDocument, '', $aOptions);
if (PEAR::isError($res)) {
return $res;
}
$oDocument->setLastModifiedDate(getCurrentDateTime());
$oDocument->setModifiedUserId($oUser->getId());
$oDocument->setIsCheckedOut(false);
$oDocument->setCheckedOutUserID(-1);
if ($aOptions['major_update']) {
$oDocument->setMajorVersionNumber($oDocument->getMajorVersionNumber()+1);
$oDocument->setMinorVersionNumber('0');
} else {
$oDocument->setMinorVersionNumber($oDocument->getMinorVersionNumber()+1);
}
$oDocument->setFileSize($iFileSize);
if(is_array($aOptions)) {
$sFilename = KTUtil::arrayGet($aOptions, 'newfilename', '');
if(!empty($sFilename)) {
global $default;
$oDocument->setFileName($sFilename);
$default->log->info('renamed document ' . $oDocument->getId() . ' to ' . $sFilename);
// detection of mime types needs to be refactored. this stuff is damn messy!
// If the filename has changed then update the mime type
$iMimeTypeId = KTMime::getMimeTypeID('', $sFilename);
$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, $sCheckInComment, 'ktcore.transactions.check_in');
$oDocumentTransaction->create();
$oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
$aTriggers = $oKTTriggerRegistry->getTriggers('content', 'scan');
foreach ($aTriggers as $aTrigger) {
$sTrigger = $aTrigger[0];
$oTrigger = new $sTrigger;
$oTrigger->setDocument($oDocument);
$ret = $oTrigger->scan();
if (PEAR::isError($ret)) {
$oDocument->delete();
return $ret;
}
}
Indexer::index($oDocument);
// fire subscription alerts for the checked in document
$oSubscriptionEvent = new SubscriptionEvent();
$oFolder = Folder::get($oDocument->getFolderID());
$oSubscriptionEvent->CheckinDocument($oDocument, $oFolder);
return true;
}
function checkout($oDocument, $sCheckoutComment, $oUser) {
//automatically check out the linked document if this is a shortcut
if($oDocument->isSymbolicLink()){
$oDocument->switchToLinkedCore();
}
if ($oDocument->getIsCheckedOut()) {
return PEAR::raiseError(_kt('Already checked out.'));
}
// FIXME at the moment errors this _does not_ rollback.
$oDocument->setIsCheckedOut(true);
$oDocument->setCheckedOutUserID($oUser->getId());
if (!$oDocument->update()) { return PEAR::raiseError(_kt('There was a problem checking out the document.')); }
$oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
$aTriggers = $oKTTriggerRegistry->getTriggers('checkout', '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;
}
}
$oDocumentTransaction = new DocumentTransaction($oDocument, $sCheckoutComment, 'ktcore.transactions.check_out');
$oDocumentTransaction->create();
// fire subscription alerts for the downloaded document
$oSubscriptionEvent = new SubscriptionEvent();
$oFolder = Folder::get($oDocument->getFolderID());
$oSubscriptionEvent->CheckOutDocument($oDocument, $oFolder);
return true;
}
function archive($oDocument, $sReason) {
$this->startTransaction();
$oDocument->setStatusID(ARCHIVED);
$res = $oDocument->update();
if (PEAR::isError($res) || ($res === false)) {
return PEAR::raiseError(_kt('There was a database error while trying to archive this file'));
}
// Ensure the action is not blocked
if(!KTWorkflowUtil::actionEnabledForDocument($oDocument, 'ktcore.actions.document.archive')){
return PEAR::raiseError(_kt('Document cannot be archived as it is restricted by the workflow.'));
}
//delete all shortcuts linking to this document
$aSymlinks = $oDocument->getSymbolicLinks();
foreach($aSymlinks as $aSymlink){
$oShortcutDocument = Document::get($aSymlink['id']);
$oOwnerUser = User::get($oShortcutDocument->getOwnerID());
KTDocumentUtil::deleteSymbolicLink($aSymlink['id']);
//send an email to the owner of the shortcut
if($oOwnerUser->getEmail()!=null && $oOwnerUser->getEmailNotification() == true){
$emailTemplate = new EmailTemplate("kt3/notifications/notification.SymbolicLinkArchived",array('user_name'=>$this->oUser->getName(),
'url'=>KTUtil::ktLink(KTBrowseUtil::getUrlForDocument($oShortcutDocument)),
'title' =>$oShortcutDocument->getName()));
$email = new EmailAlert($oOwnerUser->getEmail(),_kt("KnowledgeTree Notification"),$emailTemplate->getBody());
$email->send();
}
}
$oDocumentTransaction = & new DocumentTransaction($oDocument, sprintf(_kt('Document archived: %s'), $sReason), 'ktcore.transactions.update');
$oDocumentTransaction->create();
$this->commitTransaction();
$oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
$aTriggers = $oKTTriggerRegistry->getTriggers('archive', '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)) {
$oDocument->delete();
return $ret;
}
}
// fire subscription alerts for the archived document
$oSubscriptionEvent = new SubscriptionEvent();
$oFolder = Folder::get($oDocument->getFolderID());
$oSubscriptionEvent->ArchivedDocument($oDocument, $oFolder);
return true;
}
function &_add($oFolder, $sFilename, $oUser, $aOptions) {
global $default;
//$oContents = KTUtil::arrayGet($aOptions, 'contents');
$aMetadata = KTUtil::arrayGet($aOptions, 'metadata', null, false);
$oDocumentType = KTUtil::arrayGet($aOptions, 'documenttype');
$sDescription = KTUtil::arrayGet($aOptions, 'description', '');
if(empty($sDescription)){
// If no document name is provided use the filename minus the extension
$aFile = pathinfo($sFilename);
$sDescription = (isset($aFile['filename']) && !empty($aFile['filename'])) ? $aFile['filename'] : $sFilename;
}
$oUploadChannel =& KTUploadChannel::getSingleton();
if ($oDocumentType) {
$iDocumentTypeId = KTUtil::getId($oDocumentType);
} else {
$iDocumentTypeId = 1;
}
$oUploadChannel->sendMessage(new KTUploadGenericMessage(_kt('Creating database entry')));
$oDocument =& Document::createFromArray(array(
'name' => $sDescription,
'description' => $sDescription,
'filename' => $sFilename,
'folderid' => $oFolder->getID(),
'creatorid' => $oUser->getID(),
'documenttypeid' => $iDocumentTypeId,
));
$oUploadChannel->sendMessage(new KTUploadGenericMessage(_kt('Storing contents')));
$res = KTDocumentUtil::storeContents($oDocument, '', $aOptions);
if (PEAR::isError($res)) {
if (!PEAR::isError($oDocument)) {
$oDocument->delete();
}
return $res;
}
if (is_null($aMetadata)) {
$res = KTDocumentUtil::setIncomplete($oDocument, 'metadata');
if (PEAR::isError($res)) {
$oDocument->delete();
return $res;
}
} else {
$oUploadChannel->sendMessage(new KTUploadGenericMessage(_kt('Saving metadata')));
$res = KTDocumentUtil::saveMetadata($oDocument, $aMetadata, $aOptions);
if (PEAR::isError($res)) {
$oDocument->delete();
return $res;
}
}
// setIncomplete and storeContents may change the document's status or
// storage_path, so now is the time to update
$oDocument->update();
return $oDocument;
}
/**
* Create a symbolic link in the target folder
*
* @param Document $sourceDocument the document to create a link to
* @param Folder $targetFolder the folder to place the link in
* @param User $user current user
*/
static function createSymbolicLink($sourceDocument, $targetFolder, $user = null) // added/
{
//validate input
if (is_numeric($sourceDocument))
{
$sourceDocument = Document::get($sourceDocument);
}
if (!$sourceDocument instanceof Document)
{
return PEAR::raiseError(_kt('Source document not specified'));
}
if (is_numeric($targetFolder))
{
$targetFolder = Folder::get($targetFolder);
}
if (!$targetFolder instanceof Folder)
{
return PEAR::raiseError(_kt('Target folder not specified'));
}
if (is_null($user))
{
$user = $_SESSION['userID'];
}
if (is_numeric($user))
{
$user = User::get($user);
}
//check for permissions
$oPermission =& KTPermission::getByName("ktcore.permissions.write");
$oReadPermission =& KTPermission::getByName("ktcore.permissions.read");
if (KTBrowseUtil::inAdminMode($user, $targetFolder)) {
if(!KTPermissionUtil::userHasPermissionOnItem($user, $oPermission, $targetFolder)){
return PEAR::raiseError(_kt('You\'re not authorized to create shortcuts'));
}
}
if (!KTBrowseUtil::inAdminMode($user, $sourceDocument->getParentID())) {
if(!KTPermissionUtil::userHasPermissionOnItem($user, $oReadPermission, $sourceDocument)){
return PEAR::raiseError(_kt('You\'re not authorized to create a shortcut to this document'));
}
}
//check if the shortcut doesn't already exists in the target folder
$aSymlinks = $sourceDocument->getSymbolicLinks();
foreach($aSymlinks as $iSymlink){
$oSymlink = Document::get($iSymlink['id']);
$oSymlink->switchToRealCore();
if($oSymlink->getFolderID() == $targetFolder->getID()){
return PEAR::raiseError(_kt('There already is a shortcut to this document in the target folder.'));
}
}
//create the actual shortcut
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?