/**
  * 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;
 }
 function do_getAssignedList()
 {
     $parent_behaviour = KTUtil::arrayGet($_REQUEST, 'parent_behaviour');
     //$fieldset_id = KTUtil::arrayGet($_REQUEST, 'fieldset_id'); //
     $oFieldset =& $this->oValidator->validateFieldset(KTUtil::arrayGet($_REQUEST, 'fieldset_id'));
     $field_id = KTUtil::arrayGet($_REQUEST, 'field_id');
     $oField =& $this->oValidator->validateField(KTUtil::arrayGet($_REQUEST, 'field_id'));
     header('Content-type: application/xml');
     $oTemplating =& KTTemplating::getSingleton();
     $oTemplate =& $oTemplating->loadTemplate('ktcore/metadata/conditional/ajax_complex_get_item_list');
     $aValues = array();
     $aBehaviours = array();
     foreach ($oField->getEnabledValues() as $oValue) {
         if (empty($parent_behaviour)) {
             $oInstance = KTValueInstance::getByLookupSingle($oValue);
             if (!empty($oInstance)) {
                 if (is_null($aBehaviours[$oInstance->getBehaviourId()])) {
                     $aBehaviours[$oInstance->getBehaviourId()] = KTFieldBehaviour::get($oInstance->getBehaviourId());
                 }
                 $aValues[$oInstance->getId()] = $oValue->getName() . ' - ' . $aBehaviours[$oInstance->getBehaviourId()]->getName();
             }
             // No parent behaviour (thus master column), so any
             // instance will do to prevent showing this value
             continue;
         }
         $iInstanceId = KTValueInstance::getByLookupAndParentBehaviour($oValue, $parent_behaviour, array('ids' => true));
         if (!empty($iInstanceId)) {
             $oInstance = KTValueInstance::get($iInstanceId);
             //print $oInstance->getBehaviourId() . ' - ';
             //continue;
             $behaviour_id = $oInstance->getBehaviourId();
             if (is_null($behaviour_id)) {
                 $aValues[$oInstance->getId()] = $oValue->getName();
             } else {
                 if (is_null($aBehaviours[$behaviour_id])) {
                     $aBehaviours[$behaviour_id] = KTFieldBehaviour::get($behaviour_id);
                 }
                 $aValues[$oInstance->getId()] = $oValue->getName() . ' - ' . $aBehaviours[$behaviour_id]->getName();
             }
         }
     }
     $aData = array('values' => $aValues);
     $oTemplate->setData($aData);
     return $oTemplate->render();
 }