⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 document.inc

📁 PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。
💻 INC
📖 第 1 页 / 共 2 页
字号:
    // }}}

    // {{{ fileExists
    /**
     * Static function.
     * Check if a document with a given filename currently exists
     *
     * @param String  File name of document
     * @param int  Primary key of folder to which document is assigned
     *
     * @return boolean true if document exists, false otherwise.
     */
    function fileExists($sFileName, $iFolderID) {
        $sD = KTUtil::getTableName('documents');
        $sDM = KTUtil::getTableName('document_metadata_version');
        $sDC = KTUtil::getTableName('document_content_version');
        $sQuery = "SELECT D.id AS id FROM $sD AS D
            LEFT JOIN $sDM AS DM ON D.metadata_version_id = DM.id
            LEFT JOIN $sDC AS DC ON DM.content_version_id = DC.id
            WHERE DC.filename = ? AND D.folder_id = ? AND D.status_id=1";
        $aParams = array($sFileName, $iFolderID);
        $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
        if (empty($id)) {
            return false;
        }
        return true;
    }
    // }}}

    function &getByFilenameAndFolder($sFileName, $iFolderID) {
        $sD = KTUtil::getTableName('documents');
        $sDM = KTUtil::getTableName('document_metadata_version');
        $sDC = KTUtil::getTableName('document_content_version');
        $sQuery = "SELECT D.id AS id FROM $sD AS D
            LEFT JOIN $sDM AS DM ON D.metadata_version_id = DM.id
            LEFT JOIN $sDC AS DC ON DM.content_version_id = DC.id
            WHERE DC.filename = ? AND D.folder_id = ?";
        $aParams = array($sFileName, $iFolderID);
        $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
        return Document::get($id);
    }

    // {{{ nameExists
    /**
     * Static function.
     * Check if a document with a given filename currently exists
     *
     * @param String  File name of document
     * @param int  Primary key of folder to which document is assigned
     *
     * @return boolean true if document exists, false otherwise.
     */
    function nameExists($sName, $iFolderID) {
        $sD = KTUtil::getTableName('documents');
        $sDM = KTUtil::getTableName('document_metadata_version');
        $sDC = KTUtil::getTableName('document_content_version');
        $sQuery = "SELECT D.id AS id FROM $sD AS D
            LEFT JOIN $sDM AS DM ON D.metadata_version_id = DM.id
            LEFT JOIN $sDC AS DC ON DM.content_version_id = DC.id
            WHERE DM.name = ? AND D.folder_id = ? AND D.status_id=1";
        $aParams = array($sName, $iFolderID);
        $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
        if (empty($id)) {
            return false;
        }
        return true;
    }
    // }}}

    function &getByNameAndFolder($sName, $iFolderID) {
        $sD = KTUtil::getTableName('documents');
        $sDM = KTUtil::getTableName('document_metadata_version');
        $sDC = KTUtil::getTableName('document_content_version');
        $sQuery = "SELECT D.id AS id FROM $sD AS D
            LEFT JOIN $sDM AS DM ON D.metadata_version_id = DM.id
            LEFT JOIN $sDC AS DC ON DM.content_version_id = DC.id
            WHERE DM.name = ? AND D.folder_id = ?";
        $aParams = array($sName, $iFolderID);
        $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
        return Document::get($id);
    }

    // {{{ getDocumentDisplayPath
    /**
     * Static function.
     * Get the path for a document that will be displayed to the user
     *
     * @param integer primary key of document to generate path for
     * @return string full path to document
     */
    function getDocumentDisplayPath($iDocumentID) {
        $oDocument = & Document::get($iDocumentID);
        return $oDocument->getDisplayPath();
    }
    // }}}

    // {{{ cleanupDocumentData
    /**
     * Deletes content from document data tables
     */
    function cleanupDocumentData($iDocumentID) {
        // NEW SEARCH

        return;

        // FIXME this appears to be deprecated, or at least should be
        $sTable = KTUtil::getTableName('document_text');
        $sQuery = "DELETE FROM $sTable WHERE document_id = ?";
        $aParams = array($iDocumentID);
        $res = DBUtil::runQuery(array($sQuery, $aParams));
        return $res;
    }
    // }}}

    // {{{ getByFolderIDAndLookupID
    function &getByFolderIDAndLookupID($iParentID, $iLookupID, $aOptions = null) {
        $aIds = KTEntityUtil::getByDict('KTDocumentCore', array(
            'folder_id' => $iParentID,
            'permission_lookup_id' => $iLookupID,
            'status_id' => LIVE,
        ), array('multi' => true, 'ids' => true));

        $aList = array();
        foreach ($aIds as $iId) {
            $aList[] = Document::get($iId);
        }
    }
    // }}}

    // {{{ getByState
    function &getByState($oState) {
        $iStateId = KTUtil::getId($oState);

        $sDocumentTable = KTUtil::getTableName('documents');
        $sDocumentMetadataTable = KTUtil::getTableName('document_metadata_version');

        $sQuery = sprintf("SELECT D.id AS document_id FROM %s AS D
                           LEFT JOIN %s AS DM ON D.metadata_version_id = DM.id
                           WHERE DM.workflow_state_id = ?", $sDocumentTable, $sDocumentMetadataTable);

        $aParams = array($iStateId);
        $aIds = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'document_id');

        $aList = array();
        foreach ($aIds as $iId) {
            $aList[] = Document::get($iId);
        }
        return $aList;
    }
    // }}}

    // {{{
    function &createFromArray($aOptions) {
        if (KTUtil::arrayGet($aOptions, "size") === null) {
            $aOptions['size'] = 0;
        }
        if (KTUtil::arrayGet($aOptions, "mimetypeid") === null) {
            $aOptions['mimetypeid'] = 0;
        }
        /*
        if (KTUtil::arrayGet($aOptions, "statusid") === null) {
            $aOptions['statusid'] = LIVE;
        }
        */
        $oDocument = new Document();
        $aOptions = array_change_key_case($aOptions);


        $aCoreKeys = array(
            "CreatorId",
            "Created",
            "ModifiedUserId",
            "Modified",
            "FolderId",
            "StatusId",
            "RestoreFolderId",
            "RestoreFolderPath",
        );

        $aCore = array();
        foreach ($aCoreKeys as $sKey) {
            $sKey = strtolower($sKey);
            $sValue = KTUtil::arrayGet($aOptions, $sKey);
            if (!is_null($sValue)) {
                $aCore[$sKey] = $sValue;
            }
        }

        $aMetadataVersionKeys = array(
            "MetadataVersion",
            "ContentVersionId",
            "DocumentTypeId",
            "Name",
            "Description",
            "StatusId",
            "VersionCreated",
            "VersionCreatorId",
        );

        $aMetadataVersion = array();
        foreach ($aMetadataVersionKeys as $sKey) {
            $sKey = strtolower($sKey);
            $sValue = KTUtil::arrayGet($aOptions, $sKey);
            if (!is_null($sValue)) {
                $aMetadataVersion[$sKey] = $sValue;
            }
        }
        $aMetadataVersion['VersionCreatorId'] = $aCore['creatorid'];

        $aContentKeys = array(
            "Filename",
            "Size",
            "MimeId",
            "MajorVersion",
            "MinorVersion",
            "StoragePath",
        );

        $aContentVersion = array();
        foreach ($aContentKeys as $sKey) {
            $sKey = strtolower($sKey);
            $sValue = KTUtil::arrayGet($aOptions, $sKey);
            if (!is_null($sValue)) {
                $aContentVersion[$sKey] = $sValue;
            }
        }

        $oDocument->_oDocumentCore = KTDocumentCore::createFromArray($aCore);
        if (PEAR::isError($oDocument->_oDocumentCore)) {
            return $oDocument->_oDocumentCore;
        }
        $iId = $oDocument->_oDocumentCore->getId();
        $aContentVersion["documentid"] = $iId;
        $oDocument->_oDocumentContentVersion = KTDocumentContentVersion::createFromArray($aContentVersion);
        if (PEAR::isError($oDocument->_oDocumentContentVersion)) { return $oDocument->_oDocumentContentVersion; }
        $aMetadataVersion["documentid"] = $iId;
        $aMetadataVersion["contentversionid"] = $oDocument->_oDocumentContentVersion->getId();
        $oDocument->_oDocumentMetadataVersion = KTDocumentMetadataVersion::createFromArray($aMetadataVersion);
        if (PEAR::isError($oDocument->_oDocumentMetadataVersion)) { return $oDocument->_oDocumentMetadataVersion; }
        $oDocument->_oDocumentCore->setMetadataVersionId($oDocument->_oDocumentMetadataVersion->getId());
        $res = $oDocument->_oDocumentCore->update();
        if (PEAR::isError($res)) {
            return $res;
        }

        // Grab a copy that uses proxies...
        $oDocument =& Document::get($iId);

        KTPermissionUtil::updatePermissionLookup($oDocument);

        return $oDocument;
    }
    // }}}

    // {{{ startNewMetadataVersion
    function startNewMetadataVersion($oUser) {
        $iUserId = KTUtil::getId($oUser);
        $this->_oDocumentMetadataVersion =& $this->_oDocumentMetadataVersion->newCopy();
        if (PEAR::isError($this->_oDocumentMetadataVersion)) {
		    return $this->_oDocumentMetadataVersion;
        }
        $this->_oDocumentMetadataVersion->bumpMetadataVersion();
        $this->_oDocumentMetadataVersion->setVersionCreated(getCurrentDateTime());
        $this->_oDocumentMetadataVersion->setVersionCreatorId($iUserId);
        $this->_oDocumentMetadataVersion->update();
        $this->_oDocumentCore->setMetadataVersion($this->_oDocumentMetadataVersion->getMetadataVersion());
        $this->_oDocumentCore->setMetadataVersionId($this->_oDocumentMetadataVersion->iId);
    }
    // }}}

    // {{{ startNewContentVersion
    function startNewContentVersion($oUser) {
        if ($this->getImmutable()) {
            return PEAR::raiseError(_kt('Cannot create new version of document: Document is immutable'));
        }
        $iUserId = KTUtil::getId($oUser);
        $this->_oDocumentContentVersion =& $this->_oDocumentContentVersion->newCopy();
        $this->_oDocumentMetadataVersion =& $this->_oDocumentMetadataVersion->newCopy();
        if (PEAR::isError($this->_oDocumentContentVersion)) {
		    return $this->_oDocumentContentVersion;
        }
        if (PEAR::isError($this->_oDocumentMetadataVersion)) {
		    return $this->_oDocumentMetadataVersion;
        }
        $this->_oDocumentMetadataVersion->bumpMetadataVersion();
        $this->_oDocumentMetadataVersion->setVersionCreated(getCurrentDateTime());
        $this->_oDocumentMetadataVersion->setVersionCreatorId($iUserId);
        $this->_oDocumentMetadataVersion->setContentVersionId($this->_oDocumentContentVersion->getId());
        $res = $this->_oDocumentMetadataVersion->update();
        if (PEAR::isError($res)) {
            var_dump($res);
            return $res;
        }
        $this->_oDocumentCore->setMetadataVersion($this->_oDocumentMetadataVersion->getMetadataVersion());
        $this->_oDocumentCore->setMetadataVersionId($this->_oDocumentMetadataVersion->getId());
    }
    // }}}

    // {{{ delete
    function delete() {
        $this->_oDocumentCore->setMetadataVersionId(null);
        $this->_oDocumentCore->update();
        $aMetadataVersions = KTDocumentMetadataVersion::getByDocument($this);
        foreach ($aMetadataVersions as $oVersion) {
            $oVersion->delete();
        }
        $aContentVersions = KTDocumentContentVersion::getByDocument($this);
        foreach ($aContentVersions as $oVersion) {
            $oVersion->delete();
        }
        $this->_oDocumentCore->delete();
        return true;
    }
    // }}}

    function clearAllCaches() {

    	$GLOBALS["_OBJECTCACHE"]['Document'] = array();
        KTEntityUtil::clearAllCaches('KTDocumentCore');
        KTEntityUtil::clearAllCaches('KTDocumentContentVersion');
        KTEntityUtil::clearAllCaches('KTDocumentMetadataVersion');

        return KTEntityUtil::clearAllCaches('Document');
    }


    function getLastTransactionComment($sTransactionNamespace) {
        $sDocumentTransactionTable = KTUtil::getTableName('document_transactions');

        $sQuery = sprintf("SELECT comment FROM %s
                           WHERE transaction_namespace = ? AND document_id = ?
                           ORDER BY datetime DESC",
			  $sDocumentTransactionTable, $sDocumentMetadataTable);

	$aParams = array($sTransactionNamespace, $this->getId());
	$sComment = DBUtil::getOneResultKey(array($sQuery, $aParams), 'comment');

	if(PEAR::isError($sComment)) {
	    return false;
	}

	return $sComment;
    }

    function getLastDeletionComment() {
	$sComment = $this->getLastTransactionComment('ktcore.transactions.delete');
	if(!$sComment) {
	    return $sComment;
	}

	$aComment = explode(':', $sComment);
	return trim($aComment[1]);
    }

	static function getStatusString($statusid)
	{
		$statuses = array(
			1=>_kt('Live'),
			2=>_kt('Published'),
			3=>_kt('Deleted'),
			4=>_kt('Archived'),
			5=>_kt('Incomplete'),
			6=>_kt('Version Deleted')
		);

		if (array_key_exists($statusid, $statuses))
		{
			return $statuses[$statusid];
		}

		return _kt('Unknown State');
	}





}
?>

⌨️ 快捷键说明

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