function do_updateActiveFields()
 {
     global $default;
     $default->log->error(http_build_query($_REQUEST));
     $active_field = KTUtil::arrayGet($_REQUEST, 'active_field');
     // field which is "active".
     $fieldset_id = KTUtil::arrayGet($_REQUEST, 'fieldset_id');
     // REMEMBER TO SET CONTENT-TYPE application/xml
     $oTemplating =& KTTemplating::getSingleton();
     $oTemplate =& $oTemplating->loadTemplate('ktcore/metadata/conditional/ajax_simple_update_active_fields');
     $oTemplate->setData(array('aFieldIds' => KTMetadataUtil::getChildFieldIds($active_field)));
     header("Content-Type: application/xml");
     print $oTemplate->render();
     exit(0);
 }
 /**
  * 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;
 }
 function do_getActiveFields()
 {
     $GLOBALS['default']->log->error(print_r($_REQUEST, true));
     $parent_behaviour = KTUtil::arrayGet($_REQUEST, 'parent_behaviour');
     // $fieldset_id = KTUtil::arrayGet($_REQUEST, 'fieldset_id'); //
     $oFieldset =& $this->oValidator->validateFieldset(KTUtil::arrayGet($_REQUEST, 'fieldset_id'));
     if (empty($parent_behaviour)) {
         $aFieldIds = array($oFieldset->getMasterFieldId());
     } else {
         $oBehaviour =& $this->oValidator->validateBehaviour($parent_behaviour);
         $iActiveFieldId = $oBehaviour->getFieldId();
         $aFieldIds = KTMetadataUtil::getChildFieldIds($iActiveFieldId);
     }
     $oTemplate =& $this->oValidator->validateTemplate('ktcore/metadata/conditional/ajax_complex_get_active_fields');
     $oTemplate->setData(array('aFieldIds' => $aFieldIds));
     $GLOBALS['default']->log->error(print_r(KTMetadataUtil::getChildFieldIds($iActiveFieldId), true));
     header('Content-type: application/xml');
     /// header('Content-type: text/plain');
     return $oTemplate->render();
 }