/**
  * Given a behaviour, return an array of lookup ids (Metadata->id)
  * that are available for each of the columns/fields that this
  * behaviour'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 getNextValuesForBehaviour($oBehaviour)
 {
     $oBehaviour =& KTUtil::getObject('KTFieldBehaviour', $oBehaviour);
     $aValues = array();
     $sTable = KTUtil::getTableName('field_behaviour_options');
     $aChildFieldIds = KTMetadataUtil::getChildFieldIds($oBehaviour->getFieldId());
     foreach ($aChildFieldIds as $iFieldId) {
         $aValues[$iFieldId] = array();
     }
     $aQuery = array("SELECT field_id, instance_id FROM {$sTable} WHERE behaviour_id = ?", array($oBehaviour->getId()));
     $aRows = DBUtil::getResultArray($aQuery);
     if (PEAR::isError($aRows)) {
         return $aRows;
     }
     foreach ($aRows as $aRow) {
         $oInstance =& KTValueInstance::get($aRow['instance_id']);
         // need to wean out the disabled values.
         // now get the metadata value.
         $oMetadata = MetaData::get($oInstance->getFieldValueId());
         if (PEAR::isError($oMetadata)) {
             continue;
             // invalid link.  bugger.
         }
         if ($oMetadata->getDisabled()) {
             continue;
             // disabled.
         }
         $aValues[$aRow['field_id']][] = $oInstance->getFieldValueId();
     }
     return $aValues;
 }
 function &getByLookupAndParentBehaviour($oLookup, $oBehaviour, $aOptions = null)
 {
     $iLookupId = KTUtil::getId($oLookup);
     $iBehaviourId = KTUtil::getId($oBehaviour);
     $GLOBALS['default']->log->debug('KTValueInstance::getByLookupAndParentBehaviour: lookup id is ' . print_r($iLookupId, true));
     $GLOBALS['default']->log->debug('KTValueInstance::getByLookupAndParentBehaviour: behaviour id is ' . $iBehaviourId);
     $sInstanceTable = KTUtil::getTableName('field_value_instances');
     $sBehaviourOptionsTable = KTUtil::getTableName('field_behaviour_options');
     $aQuery = array("SELECT instance_id FROM {$sBehaviourOptionsTable} AS BO INNER JOIN\n            {$sInstanceTable} AS I ON BO.instance_id = I.id WHERE\n            BO.behaviour_id = ? AND I.field_value_id = ?", array($iBehaviourId, $iLookupId));
     $iId = DBUtil::getOneResultKey($aQuery, 'instance_id');
     if (PEAR::isError($iId)) {
         $GLOBALS['default']->log->error('KTValueInstance::getByLookupAndParentBehaviour: error from db is: ' . print_r($iId, true));
         return $iId;
     }
     if (is_null($iId)) {
         return null;
     }
     $GLOBALS['default']->log->debug('KTValueInstance::getByLookupAndParentBehaviour: id of instance is ' . $iId);
     if (KTUtil::arrayGet($aOptions, 'ids')) {
         return $iId;
     }
     return KTValueInstance::get($iId);
 }
 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();
 }