/**
  * 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_useBehaviourAndAssign()
 {
     $parent_behaviour = KTUtil::arrayGet($_REQUEST, 'parent_behaviour');
     $fieldset_id = KTUtil::arrayGet($_REQUEST, 'fieldset_id');
     $field_id = KTUtil::arrayGet($_REQUEST, 'field_id');
     $behaviour_id = KTUtil::arrayGet($_REQUEST, 'behaviour_id');
     $lookups_to_assign = KTUtil::arrayGet($_REQUEST, 'lookups_to_assign');
     // array
     $oBehaviour =& $this->oValidator->validateBehaviour($behaviour_id);
     $aValueInstanceIds = array();
     foreach ($lookups_to_assign as $iLookupId) {
         $res = $oValueInstance =& KTValueInstance::createFromArray(array('fieldid' => $field_id, 'behaviourid' => $oBehaviour->getId(), 'fieldvalueid' => abs($iLookupId)));
         $aValueInstanceIds[] = $res->getId();
     }
     if ($parent_behaviour) {
         $oParentBehaviour =& $this->oValidator->validateBehaviour($parent_behaviour);
         $sTable = KTUtil::getTableName('field_behaviour_options');
         $aOptions = array('noid' => true);
         foreach ($aValueInstanceIds as $iId) {
             $res = DBUtil::autoInsert($sTable, array('behaviour_id' => $oParentBehaviour->getId(), 'field_id' => $field_id, 'instance_id' => $iId), $aOptions);
         }
     }
     header('Content-type: application/xml');
     $oTemplating =& KTTemplating::getSingleton();
     $oTemplate =& $oTemplating->loadTemplate('ktcore/metadata/conditional/ajax_complex_use_behaviour_and_assign');
     return $oTemplate->render();
 }