metadatautil.inc.php

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

PHP
720
字号
        return DBUtil::getOneResultKey($aQuery, 'parent_field_id');
    }
    // }}}

    // {{{ getChildFieldIds
    /**
     * In a conditional fieldset, a field's values affect other fields'
     * values in an ordered fashion.  This function gets the field ids
     * for the fields that this field affects.
     */
    function getChildFieldIds($oField) {
        $iFieldId = KTUtil::getId($oField);
        $sTable = KTUtil::getTableName('field_orders');
        $aQuery = array("SELECT child_field_id FROM $sTable WHERE parent_field_id = ?",
            array($iFieldId),
        );
        return DBUtil::getResultArrayKey($aQuery, 'child_field_id');
    }
    // }}}

    // {{{ getOrCreateValueInstanceForLookup
    /**
     * Used as a helper function in simple conditional fieldset
     * administration, this function either returns the existing value
     * instance for a lookup, or creates a new value instance and
     * returns that.
     */
    function &getOrCreateValueInstanceForLookup(&$oLookup) {
        $oLookup =& KTUtil::getObject('MetaData', $oLookup);
        $oValueInstance =& KTValueInstance::getByLookupSingle($oLookup);
        if (PEAR::isError($oValueInstance)) {
            return $oValueInstance;
        }
        // If we got a value instance, return it.
        if (!is_null($oValueInstance)) {
            return $oValueInstance;
        }
        // Else create one and return it.
        return KTValueInstance::createFromArray(array(
            'fieldid' => $oLookup->getDocFieldId(),
            'fieldvalueid' => $oLookup->getId(),
        ));
    }
    // }}}

    // {{{ getNextValuesForLookup
    /**
     * Used as a helper for simple conditional fieldset administration,
     * this function returns an array of lookup ids (Metadata->id) for
     * each of the columns/fields that this lookup's column affects.
     *
     * Return value:
     *
     * Associative array keyed by field_id, value is an array of lookup
     * ids.
     *
     * array(
     *      1 => array(1, 2, 3, 4),
     *      ...
     * );
     */
    function getNextValuesForLookup($oLookup) {
        /* 
         * GENERAL GAME PLAN
         *
         * Get the instance attached to the lookup, and and call
         * getNextValuesForBehaviour on its behaviour.
         *
         * If there's no instance or behaviour, return an empty array
         * for each field that the lookup's field affects.
         */
        
        $oLookup =& KTUtil::getObject('MetaData', $oLookup);
        $oInstance =& KTValueInstance::getByLookupSingle($oLookup);
        if (PEAR::isError($oInstance)) {
            $GLOBALS['default']->log->error('KTMetadataUtil::getNextValuesForLookup, got dud instance id, returned: ' . print_r($oInstance, true));
            return $oInstance;
        }
        if (!is_null($oInstance) && $oInstance->getBehaviourId()) {
            // if we have an instance, and we have a behaviour, return
            // the actual values for that behaviour.
            $oBehaviour =& KTFieldBehaviour::get($oInstance->getBehaviourId());
            if (PEAR::isError($oBehaviour)) {
                $GLOBALS['default']->log->error('KTMetadataUtil::getNextValuesForLookup, got dud behaviour id, returned: ' . print_r($oBehaviour, true));
                return $res;
            }
            return KTMetadataUtil::getNextValuesForBehaviour($oBehaviour);
        }
        // No instance or no behaviour, so send an empty array for each
        // field that we affect.
        $aChildFieldIds = KTMetadataUtil::getChildFieldIds($oLookup->getDocFieldId());
        if (PEAR::isError($aChildFieldIds)) {
            $GLOBALS['default']->log->error('KTMetadataUtil::getNextValuesForLookup, getChildFieldIds returned: ' . print_r($aChildFieldIds, true));
            return $res;
        }
        foreach ($aChildFieldIds as $iFieldId) {
            $aValues[$iFieldId] = array();
        }
        return $aValues;
    }
    // }}}

    // {{{ getNextValuesForBehaviour
    /**
     * Given a behaviour, return an array of lookup ids (Metadata->id)
     * that are available for each of the columns/fields that this
     * behaviour's column affects.
     *
     * Return value:
     *
     * Associative array keyed by field_id, value is an array of lookup
     * ids.
     *
     * array(
     *      1 => array(1, 2, 3, 4),
     *      ...
     * );
     */
    function getNextValuesForBehaviour($oBehaviour) {
        $oBehaviour =& KTUtil::getObject('KTFieldBehaviour', $oBehaviour);
        $aValues = array();
        $sTable = KTUtil::getTableName('field_behaviour_options'); 
        $aChildFieldIds = KTMetadataUtil::getChildFieldIds($oBehaviour->getFieldId());
        foreach ($aChildFieldIds as $iFieldId) {
            $aValues[$iFieldId] = array();
        }
        $aQuery = array(
            "SELECT field_id, instance_id FROM $sTable WHERE behaviour_id = ?",
            array($oBehaviour->getId()),
        );
        $aRows = DBUtil::getResultArray($aQuery);
        if (PEAR::isError($aRows)) {
            return $aRows;
        }
        foreach ($aRows as $aRow) {
            $oInstance =& KTValueInstance::get($aRow['instance_id']);
            // need to wean out the disabled values.
            // now get the metadata value.
            $oMetadata = MetaData::get($oInstance->getFieldValueId());
            if (PEAR::isError($oMetadata)) { 
                continue; // invalid link.  bugger.
            }
            if ($oMetadata->getDisabled()) {
                continue; // disabled.
            }
            $aValues[$aRow['field_id']][] = $oInstance->getFieldValueId();
        }
        return $aValues;
    }
    // }}}

    // {{{
    function validateCompleteness($oFieldset) {
        $res = KTMetadataUtil::checkConditionalFieldsetCompleteness($oFieldset);
        // errors, false, or null are all false.        
        if ($res === true) {
            return true;
        } 
        
        return false;
    }
    
    
    // }}}

    // {{{ checkConditionalFieldsetCompleteness
    /**
     * Checks whether a conditional fieldset has the necessary
     * relationships set up to be usable - this means that for each
     * field, no matter how it is reached, there is at least one option
     * available to choose.
     */
    function checkConditionalFieldsetCompleteness($oFieldset) {
        $oFieldset =& KTUtil::getObject('KTFieldset', $oFieldset);

        if ($oFieldset->getIsConditional() == false) {
            // If we're not conditional, we are fine.
            return true;
        }

        /*
         * First, ensure at least one master field item has a behaviour
         * assigned to it.  That allows at least one item in the master
         * field to be chosen.
         */

        $iMasterFieldId = $oFieldset->getMasterFieldId();
        $sTable = KTUtil::getTableName('field_value_instances');
        $sLookupTable = KTUtil::getTableName('metadata_lookup');
        $aQuery = array(
            "SELECT COUNT(FVI.id) AS cnt FROM $sTable AS FVI LEFT JOIN $sLookupTable AS ML ON (FVI.field_value_id = ML.id) WHERE FVI.field_id = ? AND ML.disabled = 0",
            array($iMasterFieldId),
        );
        $iCount = DBUtil::getOneResultKey($aQuery, 'cnt');
        if (PEAR::isError($iCount)) {
            return $iCount;
        }
        $GLOBALS['default']->log->debug("Number of value instances for master field: $iCount");
        if ($iCount == 0) {
            $GLOBALS['default']->log->debug("Number of value instances for master field is zero, failing");
            return PEAR::raiseError(_kt("Master field has no values which are assigned to behaviours."));
        }
        $GLOBALS['default']->log->debug("Number of value instances for master field is positive, continuing");

        // fix for KTS-1023
        // check that each master-field value has a valueinstance assigned.
        $sTable = KTUtil::getTableName('metadata_lookup');
        $aQuery = array(
            "SELECT COUNT(id) AS cnt FROM $sTable WHERE document_field_id = ? AND disabled = 0 ",
            array($iMasterFieldId),
        );
        $iValCount = DBUtil::getOneResultKey($aQuery, 'cnt');
        
        // assumes that there cannot be more than 1 value instance for each master-field-value.
        if ($iValCount != $iCount) {
            return PEAR::raiseError(sprintf(_kt('%d values for the Master Field are not assigned to behaviours.'), ($iValCount - $iCount)));
        }

        /*
         * Plan: For each behaviour that is assigned on the system,
         * ensure that it allows at least one value instance in each of
         * the fields that it needs to affect.
         */

        $sTable = KTUtil::getTableName('field_value_instances');
        $sFieldTable = KTUtil::getTableName('document_fields');
        $aQuery = array(
            "SELECT DISTINCT FV.behaviour_id AS behaviour_id FROM $sTable AS FV INNER JOIN $sFieldTable AS F ON FV.field_id = F.id WHERE F.parent_fieldset = ? AND FV.behaviour_id IS NOT NULL",
            array($oFieldset->getId()),
        );
        $aBehaviourIds = DBUtil::getResultArrayKey($aQuery, 'behaviour_id');
        if (PEAR::isError($aBehaviourIds)) {
            return $aBehaviourIds;
        }

        foreach ($aBehaviourIds as $iBehaviourId) {
            $GLOBALS['default']->log->debug("Checking behaviour id: " . $iBehaviourId);
            $oBehaviour =& KTFieldBehaviour::get($iBehaviourId);
            $sBehaviourName = $oBehaviour->getName();
            $sBehaviourHumanName = $oBehaviour->getHumanName();
            $iParentFieldId = $oBehaviour->getFieldId();
            $GLOBALS['default']->log->debug("   field is " .  $iParentFieldId);
            $aNextFields = KTMetadataUtil::getChildFieldIds($iParentFieldId);
            $oParentField =& DocumentField::get($iParentFieldId);
            $sParentFieldName = $oParentField->getName();
            $GLOBALS['default']->log->debug("   next fields must include " . print_r($aNextFields, true));
            $sTable = KTUtil::getTableName('field_behaviour_options');
            $aQuery = array(
                "SELECT DISTINCT field_id FROM $sTable WHERE behaviour_id = ?",
                array($iBehaviourId),
            );
            $aFields = DBUtil::getResultArrayKey($aQuery, 'field_id');
            $GLOBALS['default']->log->debug("   actual fields are " . print_r($aNextFields, true));
            /*
            foreach ($aNextFields as $iFieldId) {
                if (!in_array($iFieldId, $aFields)) {
                    $GLOBALS['default']->log->debug("   field $iFieldId is not included, failing");
                    $oChildField =& DocumentField::get($iFieldId);
                    $sChildFieldName = $oChildField->getName();
                    return PEAR::raiseError("Child field $sChildFieldName of parent field $sParentFieldName has no selectable values in behaviour $sBehaviourHumanName ($sBehaviourName)");

            }
            */            
        }
        $GLOBALS['default']->log->debug("Got through: passed!");
        return true;
    }
    // }}}
    
    // {{{ synchroniseMetadata
    /**
     * This function takes a list of metadata values and synchronises
     * those values into the values that already exist for the field by
     * adding new values and disabling values that aren't in the new
     * list.
     *
     * XXX: Scalability: This function 
     */
    function synchroniseMetadata($oField, $aNewMetadata) {
        $iFieldId = KTUtil::getId($oField);

        $aCurrentAllValues = Metadata::getValuesByDocumentField($iFieldId);
        $aCurrentEnabledValues = Metadata::getEnabledValuesByDocumentField($iFieldId);
        $aCurrentDisabledValues = Metadata::getDisabledValuesByDocumentField($iFieldId);

        $aToBeAddedValues = array_diff($aNewMetadata, $aCurrentAllValues);
        $aToBeDisabledValues = array_diff($aCurrentEnabledValues, $aNewMetadata);
        $aToBeEnabledValues = array_intersect($aCurrentDisabledValues, $aNewMetadata);

        foreach ($aToBeAddedValues as $sValue) {
            $oMetadata =& Metadata::createFromArray(array(
                'name' => $sValue,
                'docfieldid' => $iFieldId,
            ));
        }

        foreach ($aToBeDisabledValues as $sValue) {
            $oMetadata =& Metadata::getByValueAndDocumentField($sValue, $iFieldId);
            if (PEAR::isError($oMetadata)) {
                var_dump($aToBeDisabledValues);
                var_dump($sValue);
                var_dump($iFieldId);
                var_dump($oMetadata);
                exit(0);
            }
            if (!$oMetadata->getIsStuck()) {
                $oMetadata->updateFromArray(array(
                    'disabled' => true,
                ));
            }
        }

        foreach ($aToBeEnabledValues as $sValue) {
            $oMetadata =& Metadata::getByValueAndDocumentField($sValue, $iFieldId);
            if (!$oMetadata->getIsStuck()) {
                $oMetadata->updateFromArray(array(
                    'disabled' => false,
                ));
            }
        }
    }
    // }}}

    // {{{ fieldsetsForDocument 
    function fieldsetsForDocument($oDocument, $iTypeOverride = null) {
        global $default;
        $oDocument = KTUtil::getObject('Document', $oDocument);
        $iMetadataVersionId = $oDocument->getMetadataVersionId();
        $iDocumentTypeId = $oDocument->getDocumentTypeId();
        if (!is_null($iTypeOverride)) {
            $iDocumentTypeId = $iTypeOverride;
        }

        $sQuery = "SELECT DISTINCT F.id AS fieldset_id " .
            "FROM $default->document_metadata_version_table AS DM INNER JOIN document_fields_link AS DFL ON DM.id = DFL.metadata_version_id " .
            "INNER JOIN $default->document_fields_table AS DF ON DF.ID = DFL.document_field_id " .
            "INNER JOIN $default->fieldsets_table AS F ON F.id = DF.parent_fieldset " .
            "WHERE DM.id = ?" .
            "AND F.disabled = false";
        $aParam = array($iMetadataVersionId);
        $aDocumentFieldsetIds = DBUtil::getResultArrayKey(array($sQuery, $aParam), 'fieldset_id');

        $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => true));
        $aSpecificFieldsetIds = KTFieldset::getForDocumentType($iDocumentTypeId, array('ids' => true));

        $aFieldsetIds = kt_array_merge($aDocumentFieldsetIds, $aGenericFieldsetIds, $aSpecificFieldsetIds);
        $aFieldsetIds = array_unique($aFieldsetIds);
        sort($aFieldsetIds);

        $aRet = array();
        foreach ($aFieldsetIds as $iID) {
            $aRet[] = call_user_func(array('KTFieldset', 'get'), $iID);
        }
        return $aRet;
    }
    // }}}
}

?>

⌨️ 快捷键说明

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