mdtree.inc

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

INC
394
字号
<?php
/**
 * $Id: MDTree.inc 8444 2008-05-09 07:43:24Z jonathan_byrne $
 *
 * 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 . "/ktentity.inc");
//require_once("../../../../../config/dmsDefaults.php"); // gak.
require_once(KT_LIB_DIR . "/documentmanagement/DocumentField.inc");
require_once(KT_LIB_DIR . "/documentmanagement/MetaData.inc");
require_once(KT_LIB_DIR . "/util/sanitize.inc");

class MDTreeNode extends KTEntity {
    /** boilerplate DB code. */
    /** primary key */
    var $iId = -1;
    var $iFieldId;
    var $sName;
    var $iParentNode;

    var $_aFieldToSelect = array(
        "iId" => "id",
        "iFieldId" => "document_field_id",
        "sName" => "name",
        "iParentNode" => "metadata_lookup_tree_parent",
    );

    var $_bUsePearError = true;

    function getID() { return $this->iId; }
    function setID($iId) { $this->iId = $iId; }
    function getFieldId() { return $this->iFieldId; }
    function setFieldId($iFieldId) {  $this->iFieldId = $iFieldId; }
    function getName() { return sanitizeForSQLtoHTML($this->sName); }
    function setName($sName) { $this->sName = sanitizeForSQL($sName); }
    function getParentNode() { return $this->iParentNode; }
    function setParentNode($iNode) { $this->iParentNode = $iParentNode; }

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

    // Static Functions (dull)
    function &get($iId) { return KTEntityUtil::get('MDTreeNode', $iId); }
    function &createFromArray($aOptions) { return KTEntityUtil::createFromArray('MDTreeNode', $aOptions); }
    function &getList($sWhereClause = null) { global $default; return KTEntityUtil::getList2('MDTreeNode', $sWhereClause); }

    /** end boilerplate.  anything interesting goes below here. */

}

/* simple class to encapsulate tree-as-a-whole behaviour.
    NBM - should this move, be refactored?  It certainly doesn't belong in the DB,
            since its just an aggregate utility.
*/
class MDTree {
    var $contents = null;       // private.
    var $mapnodes = null;
    var $root = null;
    var $field_id;
    var $lookups;
    var $activenodes = array();
    var $activevalue = null;

    function getRoot() { return $this->root; }
    function getMapping() { return $this->mapnodes; }
    function clear() {
        $this->contents = null;
        $this->mapnodes = null;
        $this->root = null;
        $this->lookups = null;
        $this->field_id = null;
    }

    /* function buildForField
     *
     * build a tree for a particular field instance.
     * sets contents, so we can edit "stuff".
     */
    function buildForField($iFieldId)
     {
        global $default;
        // before we start, we need to check that
        // the specified field exists and is organised into a tree.
        $organisedField =& DocumentField::get($iFieldId);
        if (PEAR::isError($organisedField) || ($organisedField === false)) {
            $this->clear();  // make sure we don't get pollution.
            return ; // and leave all null.  WHY DOESN'T PHP HAVE EXCEPTIONS?
        }

        if ($organisedField->getHasLookupTree() === false) {
            $this->clear(); // make sure we don't get pollution.
            return ; // not a tree-lookup.
        }
        // right. we are now ready to start with the treebuild.
        // root is a virtual node (id: 0).
        $this->field_id = $iFieldId;
        $orderedTreeNodes =& MDTreeNode::getList('WHERE document_field_id = '.$iFieldId);
        if (PEAR::isError($orderedTreeNodes) || ($orderedTreeNodes === false)) {
            #echo $orderedTreeNodes->message . "<br><br>";
            #echo $orderedTreeNodes->userinfo . "<br><br>";
            #echo print_r($orderedTreeNodes, true);
            #exit;
            $this->clear();  // make sure we don't get pollution.
            return ; // and leave all null.  WHY DOESN'T PHP HAVE EXCEPTIONS?
        }
        // since we have these nodes ordered by parent, we can perform a build
        // we can build:
        //      $this->mapnodes   [node_id => node]
        //      $this->root       [node_id => subtree_root_arr]
        //      $this->contents   [node_id => subtree_root_arr]
        // THIS IS IMPORTANT:  BOTH subtree_root_arr are the same object.
        // Without this, we CAN'T build the tree this way.  PLEASE, let PHP support
        // this magic.

        // initialise.

        $this->mapnodes = array(); // will hold the actual nodes, mapped by id.
        $this->contents = array(); // will hold references to each nodes subtree.
        $this->contents[0] = array();

        foreach ($orderedTreeNodes as $treeNode) {

            // step 1: set the map entry for this item.
            $iParent = $treeNode->getParentNode();
            $iCurrId = $treeNode->getId();
            $this->mapnodes[$iCurrId] = $treeNode; // always works, setting our own value.

            $parent_arr = null;
            if (!array_key_exists($iParent, $this->contents)) { $this->contents[$iParent] = array(); }
            if (!array_key_exists($iCurr, $this->contents)) { $this->contents[$iCurrId] = array(); }

            $this->contents[$iParent][] = $iCurrId;
            //$default->log->debug("MDTree::buildForField bound to subtree " . print_r($this->contents, true));
        }

        $md_list =& MetaData::getList('document_field_id = ' .$organisedField->getId() . ' AND disabled = 0 ');
        $this->lookups = array();
        foreach ($md_list as $lookup_value) {

            // failsafe. unparented and orphaned items go into root.
            $iParentId = $lookup_value->getTreeParent();
            if ($iParentId === null) $iParentId = 0;
            if (array_key_exists($iParentId, $this->contents)) {
                $target_set =& $this->contents[$iParentId];
            }
            else
            {
                $target_set =& $this->contents[0];
            }

            $leafArray = null;
            if (!array_key_exists("leaves", $target_set)) {
                $target_set["leaves"] = array($lookup_value->getId());
            }
            else
            {

                array_push($target_set["leaves"], $lookup_value->getId());
            }

            $this->lookups[$lookup_value->getId()] = $lookup_value;

        }

⌨️ 快捷键说明

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