metadatautil.inc.php

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

PHP
720
字号
<?php
/**
 * $Id: metadatautil.inc.php 8387 2008-04-22 16:36:04Z kevin_fourie $
 *
 * 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(KT_LIB_DIR . '/documentmanagement/MetaData.inc');
require_once(KT_LIB_DIR . '/documentmanagement/DocumentField.inc');
require_once(KT_LIB_DIR . '/metadata/valueinstance.inc.php');
require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
require_once(KT_LIB_DIR . '/metadata/fieldbehaviour.inc.php');

class KTMetadataUtil {

    // {{{ _getNextForBehaviour
    /**
     * Recursively traverses a fieldset from the behaviour assigned to
     * the master field and thereafter subsequent fields uncovering the
     * fields that should be filled in given the columns/fields that
     * have already be filled in, and providing the values for them
     * based on the behaviours specified by the existing values in their
     * parent fields (in combination with _their_ parent fields).
     */
    function _getNextForBehaviour($oBehaviour, $aCurrentSelections) {
        /*
         * GENERAL GAME PLAN
         *
         * For this behaviour, get the fields that this behaviour
         * affects.  Also get the values that this behaviour prescribes
         * for those fields.
         *
         * Then, for each of the next fields, check if they are already
         * filled in.
         *
         * If not, leave that field in the set of values that need to be
         * filled in, and move on to the next field for this behaviour.
         *
         * If it is filled in, remove the field from the set of values
         * to be filled in.  But add the set of fields and values that
         * the choice of value in this field prescribe (using a
         * recursive call to this function).
         */
        
        $oBehaviour =& KTUtil::getObject('KTFieldBehaviour', $oBehaviour);
        if(PEAR::isError($oBehaviour)) {
	    return array();
        }
        
        $GLOBALS['default']->log->debug('KTMetadataUtil::_getNextForBehaviour, behaviour is ' . $oBehaviour->getId());

        $aValues = KTMetadataUtil::getNextValuesForBehaviour($oBehaviour);
        
        $iFieldId = $oBehaviour->getFieldId();
        $aNextFields = KTMetadataUtil::getChildFieldIds($iFieldId);
                
        $GLOBALS['default']->log->debug('KTMetadataUtil::_getNextForBehaviour, next fields for ' . $iFieldId . ' are: ' . print_r($aNextFields, true)); 

        foreach ($aNextFields as $iThisFieldId) {
            if (!in_array($iThisFieldId, array_keys($aCurrentSelections))) {
                $GLOBALS['default']->log->debug('KTMetadataUtil::_getNextForBehaviour, field ' . $iThisFieldId . ' is not selected');

            } else {
                $GLOBALS['default']->log->debug('KTMetadataUtil::_getNextForBehaviour, field ' . $iThisFieldId . ' is selected');
                unset($aValues[$iThisFieldId]);
                
                // here we need a Lookup field.
                if (!is_a($aCurrentSelections[$iThisFieldId], 'MetaData')) { 
                    $oL = MetaData::getByValueAndDocumentField($aCurrentSelections[$iThisFieldId], $iThisFieldId);
                } else { $oL = $aCurrentSelections[$iThisFieldId]; }
                
                $oInstance = KTValueInstance::getByLookupAndParentBehaviour($oL, $oBehaviour);
                if (is_null($oInstance)) {
                    // somehow an invalid value hit us here.
                    continue;
                }
                
                $GLOBALS['default']->log->debug('KTMetadataUtil::_getNextForBehaviour, instance is ' . print_r($oInstance, true));
                $nextBehaviour = $oInstance->getBehaviourId(); // may be NULL
                if (!is_null($nextBehaviour)) {
                    $oChildBehaviour =& KTFieldBehaviour::get($nextBehaviour);
                    $GLOBALS['default']->log->debug('KTMetadataUtil::_getNextForBehaviour, field ' . $iThisFieldId . ' is not selected');
                
                    $aMyValues = KTMetadataUtil::_getNextForBehaviour($oChildBehaviour, $aCurrentSelections);
                    $GLOBALS['default']->log->debug('KTMetadataUtil::_getNextForBehaviour, values are ' . print_r($aMyValues, true));
                    foreach ($aMyValues as $k => $v) {
                        $aValues[$k] = $v;
                    }
                }
            }
        }

        $GLOBALS['default']->log->debug('KTMetadataUtil::_getNextForBehaviour, final values are ' . print_r($aValues, true));
        
        // now we need to clean this up: remove no-value entries.
        $temp = $aValues;
        foreach ($temp as $k => $v) {
            if (empty($v)) {
                unset($aValues[$k]);
            }
        }
        return $aValues;
    }
    // }}}
    
    // {{{ getNext
    /**
     * Given a set of selected values (aCurrentSelections) for a given
     * field set (iFieldSet), returns an array (possibly empty) with the
     * keys set to newly uncovered fields, and the contents an array of
     * the value instances that are available to choose in those fields.
     *
     * Return value:
     *
     * array(
     *      array('field' => DocumentField, 'values' => array(Metadata, Metadata)),
     *      ...
     * )
     *
     */
    function getNext($oFieldset, $aCurrentSelections) {
        /*
         * GENERAL GAME PLAN
         *
         * Firstly, if there are no current selections, return the
         * master field and all of its values.
         *
         * If there are selections, get the behaviour for the selected
         * value of the master field, and call _getNextForBehaviour on
         * it, passing in the current selections.  This will return an
         * array keyed on field id with values of an array of lookup ids
         * for that field.
         *
         * Convert these to objects and the return format.
         */
        $oFieldset =& KTUtil::getObject('KTFieldset', $oFieldset);
        $GLOBALS['default']->log->debug('KTMetadataUtil::getNext, selections are: ' . print_r($aCurrentSelections, true));

        if (empty($aCurrentSelections)) {
            
            $oField =& DocumentField::get($oFieldset->getMasterFieldId());
            if (PEAR::isError($oField)) {
                return array();
            }
            // FIXME we now need:  the VALUES of MasterField that are assigned to a behaviour. (ONLY)
            return array($oField->getId() => array('field' => $oField, 'values' => MetaData::getEnabledByDocumentField($oField)));
        }

        $oMasterField =& DocumentField::get($oFieldset->getMasterFieldId());
        if (PEAR::isError($oMasterField)) {
            return array();
        }
        $aSelectedFields = array_keys($aCurrentSelections);
        
        $field = $oMasterField->getId();
        $val = $aCurrentSelections[$field];
        $lookup = MetaData::getByValueAndDocumentField($val, $field);

        //var_dump($lookup); exit(0);
        $oValueInstance = KTValueInstance::getByLookupSingle($lookup);
        if (PEAR::isError($oValueInstance) || is_null($oValueInstance) || ($oValueInstance === false)) { return true; } // throw an error

        $aValues = KTMetadataUtil::_getNextForBehaviour($oValueInstance->getBehaviourId(), $aCurrentSelections);
        $GLOBALS['default']->log->debug('KTMetadataUtil::getNext, values are ' . print_r($aValues, true));
        $aReturn = array();
        foreach ($aValues as $iFieldId => $aValueIds) {
            $aTheseValues = array();
            foreach ($aValueIds as $iLookupId) {
                $aTheseValues[$iLookupId] = MetaData::get($iLookupId);
            }
            $aReturn[$iFieldId] = array(
                'field' => DocumentField::get($iFieldId),
                'values' => $aTheseValues,
            );
        }
        return $aReturn;
    }
    // }}}

    // {{{ getMasterField
    /**
     * A conditional fieldset has a single field which is not affected
     * by other values.  This is the master field.  This function gets
     * the master field for the fieldset provided.
     */
    function getMasterField($oFieldset) {
        $oFieldset =& KTUtil::getObject('KTFieldset', $oFieldset);
        if ($oFieldset->getMasterField()) {
            return DocumentField::get($oFieldset->getMasterField());
        }
    }
    // }}}

    // {{{ removeSetsFromDocumentType
    /**
     * Removes a non-generic fieldset from a given document type.
     *
     * (Generic fieldsets are made available to and are required for all
     * (subsequent) documents.  Non-generic fieldsets are made available
     * to and are required for all (subsequent) documents that have a
     * particular document type.)
     */
    function removeSetsFromDocumentType($oDocumentType, $aFieldsets) {
        if (is_object($oDocumentType)) {
            $iDocumentTypeId = $oDocumentType->getId();
        } else {
            $iDocumentTypeId = $oDocumentType;
        }
        if (!is_array($aFieldsets)) {
            $aFieldsets = array($aFieldsets);
        }
        if (empty($aFieldsets)) {
            return true;
        }
        $aIds = array();
        foreach ($aFieldsets as $oFieldset) {
            if (is_object($oFieldset)) {
                $iFieldsetId = $oFieldset->getId();
            } else {
                $iFieldsetId = $oFieldset;
            }
            $aIds[] = $iFieldsetId;
        }
        // Converts to (?, ?, ?) for query
        $sParam = DBUtil::paramArray($aIds);
        $aWhere = KTUtil::whereToString(array(
            array('document_type_id = ?', array($iDocumentTypeId)),
            array("fieldset_id IN ($sParam)", $aIds),
        ));
        $sTable = KTUtil::getTableName('document_type_fieldsets');
        $aQuery = array(
            "DELETE FROM $sTable WHERE {$aWhere[0]}",
            $aWhere[1],
        );
        return DBUtil::runQuery($aQuery);
    }
    // }}}

    // {{{ addSetsToDocumentType
    /**
     * Adds a non-generic fieldset to a given document type.
     *
     * (Generic fieldsets are made available to and are required for all
     * (subsequent) documents.  Non-generic fieldsets are made available
     * to and are required for all (subsequent) documents that have a
     * particular document type.)
     */
    function addSetsToDocumentType($oDocumentType, $aFieldsets) {
        if (is_object($oDocumentType)) {
            $iDocumentTypeId = $oDocumentType->getId();
        } else {
            $iDocumentTypeId = $oDocumentType;
        }
        if (!is_array($aFieldsets)) {
            $aFieldsets = array($aFieldsets);
        }
        $aIds = array();
        foreach ($aFieldsets as $oFieldset) {
            if (is_object($oFieldset)) {
                $iFieldsetId = $oFieldset->getId();
            } else {
                $iFieldsetId = $oFieldset;
            }
            $aIds[] = $iFieldsetId;
        }

        $sTable = KTUtil::getTableName('document_type_fieldsets');
        foreach ($aIds as $iId) {
            $res = DBUtil::autoInsert($sTable, array(
                'document_type_id' => $iDocumentTypeId,
                'fieldset_id' => $iId,
            ));
            if (PEAR::isError($res)) {
                return $res;
            }
        }
        return true;
    }
    // }}}

    // {{{ addFieldOrder
    /**
     * Informs the system that the parent field's values in affects the
     * child field's values in a conditional fieldset.
     */
    function addFieldOrder($oParentField, $oChildField, $oFieldset) {
        $iParentFieldId = KTUtil::getId($oParentField);
        $iChildFieldId = KTUtil::getId($oChildField);
        $iFieldsetId = KTUtil::getId($oFieldset);

        $aOptions = array('noid' => true);
        $sTable = KTUtil::getTableName('field_orders');
        $aValues = array(
            'parent_field_id' => $iParentFieldId,
            'child_field_id' => $iChildFieldId,
            'fieldset_id' => $iFieldsetId,
        );
        return DBUtil::autoInsert($sTable, $aValues, $aOptions);
    }
    // }}}

    // {{{ removeFieldOrdering
    /**
     * Removes all field ordering for the given fieldset.
     */
    function removeFieldOrdering($oFieldset) {
        $iFieldsetId = KTUtil::getId($oFieldset);
        $sTable = KTUtil::getTableName('field_orders');
        $aQuery = array(
            "DELETE FROM $sTable WHERE fieldset_id = ?",
            array($iFieldsetId),
        );
        return DBUtil::runQuery($aQuery);
    }
    // }}}

    // {{{ getParentFieldId
    /**
     * In a conditional fieldset, a field's values is affected by a
     * single parent field's values in an ordered fashion (unless it is
     * the root/master field).  This function gets the field id for the
     * field that this field is affected by.
     */
    function getParentFieldId($oField) {
        $sTable = KTUtil::getTableName('field_orders');
        $aQuery = array("SELECT parent_field_id FROM $sTable WHERE child_field_id = ?",
            array($oField->getId()),
        );

⌨️ 快捷键说明

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