folder.inc

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

INC
606
字号
<?php
/**
 * $Id: Folder.inc 8781 2008-07-11 14:25:52Z kevin_fourie $
 *
 * Represents as folder as the per the folders table in the database.
 *
 * 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): ______________________________________
 */

require_once(KT_LIB_DIR . "/foldermanagement/PhysicalFolderManagement.inc");
require_once(KT_LIB_DIR . "/documentmanagement/Document.inc");
require_once(KT_LIB_DIR . "/util/sanitize.inc");

class Folder extends KTEntity {

	/** folder primary key */
	var $iId;
	/** folder name */
	var $sName;
	/** folder description */
	var $sDescription;
	/** folder parent primary key */
	var $iParentID;
	/** primary key of user who created folder */
	var $iCreatorID;
	/** public status of folder */
	var $bIsPublic = false;
	/** comma deliminated string of parent ids */
	var $sParentFolderIDs;
	/** forward slash deliminated path from file system root */
	var $sFullPath;
    /** which permission object I get permissions from */
    var $iPermissionObjectID;
    /** lookup accelerator id */
    var $iPermissionLookupID;
    /** whether to restrict to only certain document types */
    var $bRestrictDocumentTypes = false;
    /** ID of the folder this is a shortcut to(if any) */
    var $iLinkedFolderId;

    // {{{ KTEntity stuff
    var $_aFieldToSelect = array(
        'iId' => 'id',
        'sName' => 'name',
        'sDescription' => 'description',
        'iParentID' => 'parent_id',
        'iCreatorID' => 'creator_id',
        'bIsPublic' => 'is_public',
        'sFullPath' => 'full_path',
        'sParentFolderIDs' => 'parent_folder_ids',
        'iPermissionObjectID' => 'permission_object_id',
        'iPermissionLookupID' => 'permission_lookup_id',
        'bRestrictDocumentTypes' => 'restrict_document_types',
    	'iLinkedFolderId' => 'linked_folder_id',
    );
    // }}}

	function getID() { return $this->iId; }
	function getName() { return ($this->sName); }
	function setName($sNewValue) { $this->sName = ($sNewValue); }
	function getDescription() { return ($this->sDescription); }
	function setDescription($sNewValue) { $this->sDescription = ($sNewValue); }
	function getParentID() { return $this->iParentID; }
	function setParentID($iNewValue) { $this->iParentID = $iNewValue; }
	function getCreatorID() { return $this->iCreatorID; }
	function setCreatorID($iNewValue) { $this->iCreatorID = $iNewValue; }
	function getIsPublic() { return $this->bIsPublic; }
	function setIsPublic($bNewValue) { $this->bIsPublic = $bNewValue; }
	function getFullPath() { return $this->sFullPath; }
	function getParentFolderIDs() { return $this->sParentFolderIDs; }

	function getPermissionObjectID() { return $this->iPermissionObjectID; }
	function setPermissionObjectID($iPermissionObjectID) { $this->iPermissionObjectID = $iPermissionObjectID; }
	function getPermissionLookupID() { return $this->iPermissionLookupID; }
	function setPermissionLookupID($iPermissionLookupID) { $this->iPermissionLookupID = $iPermissionLookupID; }

	function getRestrictDocumentTypes() { return $this->bRestrictDocumentTypes; }
	function setRestrictDocumentTypes($bRestrictDocumentTypes) { $this->bRestrictDocumentTypes = $bRestrictDocumentTypes; }

	function getLinkedFolderId(){ return $this->iLinkedFolderId;}
	function setLinkedFolderId($iNewValue){ $this->iLinkedFolderId = $iNewValue;}



    // {{{ create()
    function create () {
        $oParentFolder =& Folder::get($this->iParentID);
        $this->iPermissionObjectID = $oParentFolder->getPermissionObjectID();
        $res = parent::create();
        if ($res === true) {
            KTPermissionUtil::updatePermissionLookup(Folder::get($this->getId()));
        }
        return $res;
    }
    // }}}

    function loadFromArray ($aOptions) {
    	parent::loadFromArray($aOptions);

    	//now load fields from the folder this folder is linking to, if any.
    	if($this->isSymbolicLink()){
    		$oLinkedFolder = $this->getLinkedFolder();
    		$this->sName = $oLinkedFolder->getName();
    		$this->sDescription = $oLinkedFolder->getDescription();
    	}

    }

        /**
         * Returns a comma delimited string containing the parent folder ids, strips leading /
         *
         * @return String	comma delimited string containing the parent folder ids
         */
        function generateFolderIDs($iFolderId) {
            if (empty($iFolderId)) {
                    return;
            }
            $oFolder =& Folder::get($iFolderId);
            if (PEAR::isError($oFolder)) {
                return $oFolder;
            }
            $iParentId = $oFolder->getParentId();
            if (empty($iParentId)) {
                return $oFolder->getId();
            }
            $oParentFolder =& Folder::get($iParentId);
            if (PEAR::isError($oParentFolder)) {
                return $oParentFolder;
            }
            $sParentFolderParentFolderIds = $oParentFolder->getParentFolderIDs();
            if (empty($sParentFolderParentFolderIds)) {
                return sprintf('%s,%s', $iParentId, $oFolder->getId());;
            }
            return sprintf('%s,%s,%s', $sParentFolderParentFolderIds, $iParentId, $oFolder->getId());
        }

	/**
	 * Recursively generates forward slash deliminated string giving full path of document
	 * from file system root url
	 */
	function generateFullFolderPath($iFolderId) {
		//if the folder is not the root folder
		if ($iFolderId == 0) {
            return;
        }
		if ($iFolderId == 1) {
            $oFolder =& Folder::get(1);
            return $oFolder->getName();
        }

        $oFolder =& Folder::get($iFolderId);
		if (PEAR::isError($oFolder)) {
            global $default;
            $default->log->error("Invalid folder passed to generateFullFolderPath: %s", print_r($oFolder, true));
            return $oFolder;
        }
        $iParentId = $oFolder->getParentId();
        if (empty($iParentId)) {
            return $oFolder->getName();
        }
        $res = Folder::generateFullFolderPath($iParentId);
        if (PEAR::isError($res)) {
            return $res;
        }
        return sprintf('%s/%s', $res, $oFolder->getName());
	}

	/**
	 * Returns a forward slash deliminated string giving full path of document, strips leading /
	 */
	function generateFolderPath($iFolderID) {
		$sPath = Folder::generateFullFolderPath($iFolderID);
		return $sPath;
	}

    function _fieldValues () {
    	if ($this->getId() == 1)
    	{
			$this->sFullPath = null;
			$this->sParentFolderIDs = null;
    	}
    	else
    	{
    		$parent = Folder::get($this->iParentID);
        	$this->sFullPath = $parent->getFullPath();
        	if (!empty($this->sFullPath)) $this->sFullPath .= '/';
        	$this->sFullPath .= $this->sName;
        	$this->sParentFolderIDs = $parent->getParentFolderIDs();
			if (!empty($this->sParentFolderIDs)) $this->sParentFolderIDs .= ',';
        	$this->sParentFolderIDs .= $this->iParentID;
    	}
        return parent::_fieldValues();
        return array(
            'name' => $this->sName,
            'description' => $this->sDescription,
            'parent_id' => $this->iParentID,
            'creator_id' => $this->iCreatorID,
            'is_public' => KTUtil::anyToBool($this->bIsPublic),
            'full_path' => $this->sFullPath,
            'parent_folder_ids' => $this->sParentFolderIDs,
            'permission_object_id' => $this->iPermissionObjectID,
            'permission_lookup_id' => $this->iPermissionLookupID,
        );
    }

    function _table () {
        global $default;
        return $default->folders_table;
    }

	/**
	* Update the current folder values in the database
	*
	* @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"]
	*/
	function update($bPathChange = false) {
        $res = parent::update();
        if ($res === true) {
            if ($bPathChange) {
                // XXX: TransactionCheckPoint
                $this->updateChildPaths($this->iId);
                $this->updateDocumentPaths($this->iId);
            }
        }
        return $res;
	}

	function renameFolder($sOldPath) {
		PhysicalFolderManagement::renameFolder($sOldPath, $default->documentRoot . "/" . $this->sFullPath . "/" . $this->sName);
	}

	/**
	* When a folder is renamed, we must update
	* the paths of the children in the database
	*
	*/
	function updateChildPaths($iId) {
		global $default;
		//get the direct children
        $sql = $default->db;
		$aFolders =& Folder::getByParentId($iId);
		foreach ($aFolders as $oFolder) {
			$oFolder->update(true);
		}
		return;
	}

    /**
     * When a folder's path changes, we must update the paths in the
     * documents in that folder.  Sub-folders are handled elsewhere in
     * update().
     */

    function updateDocumentPaths($iId) {
        $aDocuments = Document::getList(array('folder_id = ?', $iId));
        if (PEAR::isError($aDocuments)) {
            return $aDocuments;
        }
        foreach ($aDocuments as $oDocument) {
            // Document->update() automatically adjusts the path.
            $oDocument->update();
            // XXX: Should handle failure here somehow, but rather get
            // most working than just the first two.  Must find a sane
            // way to handle transactions.
            // TransactionCheckPoint
        }
        return true;
    }

    /**
     * Returns the documents in this folder
     */
    function getDocumentIDs($iFolderID) {

⌨️ 快捷键说明

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