Commit c59dfb4730d4a1e8819319318e40ac42bce1d181

Authored by Neil Blakey-Milner
1 parent d3b12e42

KTMetadataUtil::synchroniseMetadata allows for a field's lookup options

to be synchronised from a list of values (from LDAP, another database,
or manually).


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3908 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/metadata/metadatautil.inc.php
... ... @@ -532,6 +532,49 @@ class KTMetadataUtil {
532 532 return true;
533 533 }
534 534 // }}}
  535 +
  536 + // {{{ synchroniseMetadata
  537 + /**
  538 + * This function takes a list of metadata values and synchronises
  539 + * those values into the values that already exist for the field by
  540 + * adding new values and disabling values that aren't in the new
  541 + * list.
  542 + *
  543 + * XXX: Scalability: This function
  544 + */
  545 + function synchroniseMetadata($oField, $aNewMetadata) {
  546 + $iFieldId = KTUtil::getId($oField);
  547 +
  548 + $aCurrentAllValues = Metadata::getValuesByDocumentField($iFieldId);
  549 + $aCurrentEnabledValues = Metadata::getEnabledValuesByDocumentField($iFieldId);
  550 + $aCurrentDisabledValues = Metadata::getDisabledValuesByDocumentField($iFieldId);
  551 +
  552 + $aToBeAddedValues = array_diff($aNewMetadata, $aCurrentAllValues);
  553 + $aToBeDisabledValues = array_diff($aCurrentEnabledValues, $aNewMetadata);
  554 + $aToBeEnabledValues = array_intersect($aCurrentDisabledValues, $aNewMetadata);
  555 +
  556 + foreach ($aToBeAddedValues as $sValue) {
  557 + $oMetadata =& Metadata::createFromArray(array(
  558 + 'name' => $sValue,
  559 + 'docfieldid' => $iFieldId,
  560 + ));
  561 + }
  562 +
  563 + foreach ($aToBeDisabledValues as $sValue) {
  564 + $oMetadata =& Metadata::getByValueAndDocumentField($sValue, $iFieldId);
  565 + $oMetadata->updateFromArray(array(
  566 + 'disabled' => true,
  567 + ));
  568 + }
  569 +
  570 + foreach ($aToBeEnabledValues as $sValue) {
  571 + $oMetadata =& Metadata::getByValueAndDocumentField($sValue, $iFieldId);
  572 + $oMetadata->updateFromArray(array(
  573 + 'disabled' => false,
  574 + ));
  575 + }
  576 + }
  577 + // }}}
535 578 }
536 579  
537 580 ?>
... ...