Commit 73dbf3b888b1e5d3db8caf0a4ab7b27148e9d344

Authored by nbm
1 parent 419602e6

Add checkConditionalFieldsetCompleteness which checks whether the

configuration of a conditional fieldset makes sense enough to be used.

Also avoid clobbering aValues in getNext...


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3805 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/metadata/metadatautil.inc.php
... ... @@ -27,6 +27,7 @@
27 27  
28 28 require_once(KT_LIB_DIR . "/ktentity.inc");
29 29 require_once(KT_LIB_DIR . '/documentmanagement/MetaData.inc');
  30 +require_once(KT_LIB_DIR . '/documentmanagement/DocumentField.inc');
30 31 require_once(KT_LIB_DIR . '/metadata/valueinstance.inc.php');
31 32 require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
32 33 require_once(KT_LIB_DIR . '/metadata/fieldbehaviour.inc.php');
... ... @@ -90,13 +91,13 @@ class KTMetadataUtil {
90 91 $GLOBALS['default']->log->debug('KTMetadataUtil::getNext, values are ' . print_r($aValues, true));
91 92 $aReturn = array();
92 93 foreach ($aValues as $iFieldId => $aValueIds) {
93   - $aValues = array();
  94 + $aTheseValues = array();
94 95 foreach ($aValueIds as $iLookupId) {
95   - $aValues[$iLookupId] = MetaData::get($iLookupId);
  96 + $aTheseValues[$iLookupId] = MetaData::get($iLookupId);
96 97 }
97 98 $aReturn[$iFieldId] = array(
98 99 'field' => DocumentField::get($iFieldId),
99   - 'values' => $aValues,
  100 + 'values' => $aTheseValues,
100 101 );
101 102 }
102 103 return $aReturn;
... ... @@ -301,6 +302,92 @@ class KTMetadataUtil {
301 302 }
302 303 return $aValues;
303 304 }
  305 +
  306 + // {{{ checkConditionalFieldsetCompleteness
  307 + /**
  308 + * Checks whether a conditional fieldset has the necessary
  309 + * relationships set up to be usable - this means that for each
  310 + * field, no matter how it is reached, there is at least one option
  311 + * available to choose.
  312 + */
  313 + function checkConditionalFieldsetCompleteness($oFieldset) {
  314 + $oFieldset =& KTUtil::getObject('KTFieldset', $oFieldset);
  315 +
  316 + if ($oFieldset->getIsConditional() == false) {
  317 + // If we're not conditional, we are fine.
  318 + return true;
  319 + }
  320 +
  321 + /*
  322 + * First, ensure at least one master field item has a behaviour
  323 + * assigned to it. That allows at least one item in the master
  324 + * field to be chosen.
  325 + */
  326 +
  327 + $iMasterFieldId = $oFieldset->getMasterFieldId();
  328 + $sTable = KTUtil::getTableName('field_value_instances');
  329 + $aQuery = array(
  330 + "SELECT COUNT(id) AS cnt FROM $sTable WHERE field_id = ?",
  331 + array($iMasterFieldId),
  332 + );
  333 + $iCount = DBUtil::getOneResultKey($aQuery, 'cnt');
  334 + if (PEAR::isError($iCount)) {
  335 + return $iCount;
  336 + }
  337 + $GLOBALS['default']->log->debug("Number of value instances for master field: $iCount");
  338 + if ($iCount == 0) {
  339 + $GLOBALS['default']->log->debug("Number of value instances for master field is zero, failing");
  340 + return PEAR::raiseError("Master field has no selectable values");
  341 + }
  342 + $GLOBALS['default']->log->debug("Number of value instances for master field is positive, continuing");
  343 +
  344 + /*
  345 + * Plan: For each behaviour that is assigned on the system,
  346 + * ensure that it allows at least one value instance in each of
  347 + * the fields that it needs to affect.
  348 + */
  349 +
  350 + $sTable = KTUtil::getTableName('field_value_instances');
  351 + $sFieldTable = KTUtil::getTableName('document_fields');
  352 + $aQuery = array(
  353 + "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",
  354 + array($oFieldset->getId()),
  355 + );
  356 + $aBehaviourIds = DBUtil::getResultArrayKey($aQuery, 'behaviour_id');
  357 + if (PEAR::isError($aBehaviourIds)) {
  358 + return $aBehaviourIds;
  359 + }
  360 +
  361 + foreach ($aBehaviourIds as $iBehaviourId) {
  362 + $GLOBALS['default']->log->debug("Checking behaviour id: " . $iBehaviourId);
  363 + $oBehaviour =& KTFieldBehaviour::get($iBehaviourId);
  364 + $sBehaviourName = $oBehaviour->getName();
  365 + $iParentFieldId = $oBehaviour->getFieldId();
  366 + $GLOBALS['default']->log->debug(" field is " . $iParentFieldId);
  367 + $aNextFields = KTMetadataUtil::getChildFieldIds($iParentFieldId);
  368 + $oParentField =& DocumentField::get($iParentFieldId);
  369 + $sParentFieldName = $oParentField->getName();
  370 + $GLOBALS['default']->log->debug(" next fields must include " . print_r($aNextFields, true));
  371 + $sTable = KTUtil::getTableName('field_behaviour_options');
  372 + $aQuery = array(
  373 + "SELECT DISTINCT field_id FROM $sTable WHERE behaviour_id = ?",
  374 + array($iBehaviourId),
  375 + );
  376 + $aFields = DBUtil::getResultArrayKey($aQuery, 'field_id');
  377 + $GLOBALS['default']->log->debug(" actual fields are " . print_r($aNextFields, true));
  378 + foreach ($aNextFields as $iFieldId) {
  379 + if (!in_array($iFieldId, $aFields)) {
  380 + $GLOBALS['default']->log->debug(" field $iFieldId is not included, failing");
  381 + $oChildField =& DocumentField::get($iFieldId);
  382 + $sChildFieldName = $oChildField->getName();
  383 + return PEAR::raiseError("Child field $sChildFieldName of parent field $sParentFieldName has no selectable values in behaviour $sBehaviourName");
  384 + }
  385 + }
  386 + }
  387 + $GLOBALS['default']->log->debug("Got through: passed!");
  388 + return true;
  389 + }
  390 + // }}}
304 391 }
305 392  
306 393 ?>
... ...