documentutil.inc.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,474 行 · 第 1/4 页
PHP
1,474 行
$oCore = KTDocumentCore::createFromArray(array(
'iCreatorId'=>$user->getId(),
'iFolderId'=>$targetFolder->getId(),
'iLinkedDocumentId'=>$sourceDocument->getId(),
'sFullPath'=> $targetFolder->getFullPath() . '/' .
$sourceDocument->getName(),
'iPermissionObjectId'=>$targetFolder->getPermissionObjectID(),
'iPermissionLookupId'=>$targetFolder->getPermissionLookupID(),
'iStatusId'=>1,
'iMetadataVersionId'=>$sourceDocument->getMetadataVersionId(),
));
$document = Document::get($oCore->getId());
return $document;
}
/**
* Deletes a document symbolic link
*
* @param Document $document the symbolic link document
* @param User $user the user deleting the link
* @return unknown
*/
static function deleteSymbolicLink($document, $user = null) // added/
{
//validate input
if (is_numeric($document))
{
$document = Document::get($document);
}
if (!$document instanceof Document)
{
return PEAR::raiseError(_kt('Document not specified'));
}
if (!$document->isSymbolicLink())
{
return PEAR::raiseError(_kt('Document must be a symbolic link entity'));
}
if (is_null($user))
{
$user = $_SESSION['userID'];
}
if (is_numeric($user))
{
$user = User::get($user);
}
//check permissions
$oPerm = KTPermission::getByName('ktcore.permissions.delete');
if (!KTBrowseUtil::inAdminMode($user, $document->getParentID())) {
if(!KTPermissionUtil::userHasPermissionOnItem($user, $oPerm, $document)){
return PEAR::raiseError(_kt('You\'re not authorized to delete this shortcut'));
}
}
// we only need to delete the document entry for the link
$sql = "DELETE FROM documents WHERE id=?";
DBUtil::runQuery(array($sql, array($document->getId())));
}
// Overwrite the document
function overwrite($oDocument, $sFilename, $sTempFileName, $oUser, $aOptions) {
//$oDocument, $sFilename, $sCheckInComment, $oUser, $aOptions = false
$oStorage =& KTStorageManagerUtil::getSingleton();
$iFileSize = filesize($sTempFileName);
// Check that document is not checked out
if($oDocument->getIsCheckedOut()) {
return PEAR::raiseError(_kt('Document is checkout and cannot be overwritten'));
}
if (!$oStorage->upload($oDocument, $sTempFileName)) {
return PEAR::raiseError(_kt('An error occurred while storing the new file'));
}
$oDocument->setLastModifiedDate(getCurrentDateTime());
$oDocument->setModifiedUserId($oUser->getId());
$oDocument->setFileSize($iFileSize);
$sOriginalFilename = $oDocument->getFileName();
if($sOriginalFilename != $sFilename){
if(strlen($sFilename)) {
global $default;
$oDocument->setFileName($sFilename);
$default->log->info('renamed document ' . $oDocument->getId() . ' to ' . $sFilename);
}
$oDocument->setMinorVersionNumber($oDocument->getMinorVersionNumber()+1);
}
$sType = KTMime::getMimeTypeFromFile($sFilename);
$iMimeTypeId = KTMime::getMimeTypeID($sType, $oDocument->getFileName());
$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;
}
}
// NEW SEARCH
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;
}
// {{{ validateMetadata
function validateMetadata(&$oDocument, $aMetadata) {
$aFieldsets =& KTFieldset::getGenericFieldsets();
$aFieldsets =& kt_array_merge($aFieldsets,
KTFieldset::getForDocumentType($oDocument->getDocumentTypeId()));
$aSimpleMetadata = array();
foreach ($aMetadata as $aSingleMetadatum) {
list($oField, $sValue) = $aSingleMetadatum;
if (is_null($oField)) {
continue;
}
$aSimpleMetadata[$oField->getId()] = $sValue;
}
$aFailed = array();
foreach ($aFieldsets as $oFieldset) {
$aFields =& $oFieldset->getFields();
$aFieldValues = array();
$isRealConditional = ($oFieldset->getIsConditional() && KTMetadataUtil::validateCompleteness($oFieldset));
foreach ($aFields as $oField) {
$v = KTUtil::arrayGet($aSimpleMetadata, $oField->getId());
if ($oField->getIsMandatory() && !$isRealConditional) {
if (empty($v)) {
// XXX: What I'd do for a setdefault...
$aFailed['field'][$oField->getId()] = 1;
}
}
if (!empty($v)) {
$aFieldValues[$oField->getId()] = $v;
}
}
if ($isRealConditional) {
$res = KTMetadataUtil::getNext($oFieldset, $aFieldValues);
if ($res) {
foreach ($res as $aMDSet) {
if ($aMDSet['field']->getIsMandatory()) {
$aFailed['fieldset'][$oFieldset->getId()] = 1;
}
}
}
}
}
if (!empty($aFailed)) {
return new KTMetadataValidationError($aFailed);
}
return $aMetadata;
}
// }}}
// {{{ saveMetadata
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;
}
// }}}
function copyMetadata($oDocument, $iPreviousMetadataVersionId) {
$iNewMetadataVersion = $oDocument->getMetadataVersionId();
$sTable = KTUtil::getTableName('document_fields_link');
$aFields = DBUtil::getResultArray(array("SELECT * FROM $sTable WHERE metadata_version_id = ?", array($iPreviousMetadataVersionId)));
foreach ($aFields as $aRow) {
unset($aRow['id']);
$aRow['metadata_version_id'] = $iNewMetadataVersion;
DBUtil::autoInsert($sTable, $aRow);
}
}
// {{{ setIncomplete
function setIncomplete(&$oDocument, $reason) {
$oDocument->setStatusID(STATUS_INCOMPLETE);
$table = 'document_incomplete';
$iId = $oDocument->getId();
$aIncomplete = DBUtil::getOneResult(array("SELECT * FROM $table WHERE id = ?", array($iId)));
if (PEAR::isError($aIncomplete)) {
return $aIncomplete;
}
if (is_null($aIncomplete)) {
$aIncomplete = array('id' => $iId);
}
$aIncomplete[$reason] = true;
$res = DBUtil::autoDelete($table, $iId);
if (PEAR::isError($res)) {
return $res;
}
$res = DBUtil::autoInsert($table, $aIncomplete);
if (PEAR::isError($res)) {
return $res;
}
return true;
}
// }}}
// {{{ setComplete
function setComplete(&$oDocument, $reason) {
$table = 'document_incomplete';
$iId = $oDocument->getID();
$aIncomplete = DBUtil::getOneResult(array("SELECT * FROM $table WHERE id = ?", array($iId)));
if (PEAR::isError($aIncomplete)) {
return $aIncomplete;
}
if (is_null($aIncomplete)) {
$oDocument->setStatusID(LIVE);
return true;
}
$aIncomplete[$reason] = false;
$bIncomplete = false;
foreach ($aIncomplete as $k => $v) {
if ($k === 'id') { continue; }
if ($v) {
$bIncomplete = true;
}
}
if ($bIncomplete === false) {
DBUtil::autoDelete($table, $iId);
$oDocument->setStatusID(LIVE);
return true;
}
$res = DBUtil::autoDelete($table, $iId);
if (PEAR::isError($res)) {
return $res;
}
$res = DBUtil::autoInsert($table, $aIncomplete);
if (PEAR::isError($res)) {
return $res;
}
}
// }}}
// {{{ add
function &add($oFolder, $sFilename, $oUser, $aOptions) {
$GLOBALS['_IN_ADD'] = true;
$ret = KTDocumentUtil::_in_add($oFolder, $sFilename, $oUser, $aOptions);
unset($GLOBALS['_IN_ADD']);
return $ret;
}
// }}}
function getUniqueFilename($oFolder, $sFilename) {
// this is just a quick refactoring. We should look at a more optimal way of doing this as there are
// quite a lot of queries.
$iFolderId = $oFolder->getId();
while (KTDocumentUtil::fileExists($oFolder, $sFilename)) {
$oDoc = Document::getByFilenameAndFolder($sFilename, $iFolderId);
$sFilename = KTDocumentUtil::generateNewDocumentFilename($oDoc->getFileName());
}
return $sFilename;
}
function getUniqueDocumentName($oFolder, $sFilename)
{
// this is just a quick refactoring. We should look at a more optimal way of doing this as there are
// quite a lot of queries.
$iFolderId = $oFolder->getId();
while(KTDocumentUtil::nameExists($oFolder, $sFilename)) {
$oDoc = Document::getByNameAndFolder($sFilename, $iFolderId);
$sFilename = KTDocumentUtil::generateNewDocumentName($oDoc->getName());
}
return $sFilename;
}
// {{{ _in_add
function &_in_add($oFolder, $sFilename, $oUser, $aOptions) {
$aOrigOptions = $aOptions;
$sFilename = KTDocumentUtil::getUniqueFilename($oFolder, $sFilename);
$sName = KTUtil::arrayGet($aOptions, 'description', $sFilename);
$sName = KTDocumentUtil::getUniqueDocumentName($oFolder, $sName);
$aOptions['description'] = $sName;
$oUploadChannel =& KTUploadChannel::getSingleton();
$oUploadChannel->sendMessage(new KTUploadNewFile($sFilename));
DBUtil::startTransaction();
$oDocument =& KTDocumentUtil::_add($oFolder, $sFilename, $oUser, $aOptions);
$oUploadChannel->sendMessage(new KTUploadGenericMessage(_kt('Document created')));
if (PEAR::isError($oDocument)) {
return $oDocument;
}
$oUploadChannel->sendMessage(new KTUploadGenericMessage(_kt('Scanning file')));
$oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
$aTriggers = $oKTTriggerRegistry->getTriggers('content', 'scan');
$iTrigger = 0;
foreach ($aTriggers as $aTrigger) {
$sTrigger = $aTrigger[0];
$oTrigger = new $sTrigger;
$oTrigger->setDocument($oDocument);
// $oUploadChannel->sendMessage(new KTUploadGenericMessage(sprintf(_kt(" (trigger %s)"), $sTrigger)));
$ret = $oTrigger->scan();
if (PEAR::isError($ret)) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?