From c59dfb4730d4a1e8819319318e40ac42bce1d181 Mon Sep 17 00:00:00 2001 From: Neil Blakey-Milner Date: Wed, 2 Nov 2005 14:10:43 +0000 Subject: [PATCH] KTMetadataUtil::synchroniseMetadata allows for a field's lookup options to be synchronised from a list of values (from LDAP, another database, or manually). --- lib/metadata/metadatautil.inc.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+), 0 deletions(-) diff --git a/lib/metadata/metadatautil.inc.php b/lib/metadata/metadatautil.inc.php index 7abbce2..783085b 100644 --- a/lib/metadata/metadatautil.inc.php +++ b/lib/metadata/metadatautil.inc.php @@ -532,6 +532,49 @@ class KTMetadataUtil { 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); + $oMetadata->updateFromArray(array( + 'disabled' => true, + )); + } + + foreach ($aToBeEnabledValues as $sValue) { + $oMetadata =& Metadata::getByValueAndDocumentField($sValue, $iFieldId); + $oMetadata->updateFromArray(array( + 'disabled' => false, + )); + } + } + // }}} } ?> -- libgit2 0.21.4