/**
  * Given a set of selected values (aCurrentSelections) for a given
  * field set (iFieldSet), returns an array (possibly empty) with the
  * keys set to newly uncovered fields, and the contents an array of
  * the value instances that are available to choose in those fields.
  *
  * Return value:
  *
  * array(
  *      array('field' => DocumentField, 'values' => array(Metadata, Metadata)),
  *      ...
  * )
  *
  */
 function getNext($oFieldset, $aCurrentSelections)
 {
     /*
      * GENERAL GAME PLAN
      *
      * Firstly, if there are no current selections, return the
      * master field and all of its values.
      *
      * If there are selections, get the behaviour for the selected
      * value of the master field, and call _getNextForBehaviour on
      * it, passing in the current selections.  This will return an
      * array keyed on field id with values of an array of lookup ids
      * for that field.
      *
      * Convert these to objects and the return format.
      */
     $oFieldset =& KTUtil::getObject('KTFieldset', $oFieldset);
     $GLOBALS['default']->log->debug('KTMetadataUtil::getNext, selections are: ' . print_r($aCurrentSelections, true));
     if (empty($aCurrentSelections)) {
         $oField =& DocumentField::get($oFieldset->getMasterFieldId());
         if (PEAR::isError($oField)) {
             return array();
         }
         // FIXME we now need:  the VALUES of MasterField that are assigned to a behaviour. (ONLY)
         return array($oField->getId() => array('field' => $oField, 'values' => MetaData::getEnabledByDocumentField($oField)));
     }
     $oMasterField =& DocumentField::get($oFieldset->getMasterFieldId());
     if (PEAR::isError($oMasterField)) {
         return array();
     }
     $aSelectedFields = array_keys($aCurrentSelections);
     $field = $oMasterField->getId();
     $val = $aCurrentSelections[$field];
     $lookup = MetaData::getByValueAndDocumentField($val, $field);
     //var_dump($lookup); exit(0);
     $oValueInstance = KTValueInstance::getByLookupSingle($lookup);
     if (PEAR::isError($oValueInstance) || is_null($oValueInstance) || $oValueInstance === false) {
         return true;
     }
     // throw an error
     $aValues = KTMetadataUtil::_getNextForBehaviour($oValueInstance->getBehaviourId(), $aCurrentSelections);
     $GLOBALS['default']->log->debug('KTMetadataUtil::getNext, values are ' . print_r($aValues, true));
     $aReturn = array();
     foreach ($aValues as $iFieldId => $aValueIds) {
         $aTheseValues = array();
         foreach ($aValueIds as $iLookupId) {
             $aTheseValues[$iLookupId] = MetaData::get($iLookupId);
         }
         $aReturn[$iFieldId] = array('field' => DocumentField::get($iFieldId), 'values' => $aTheseValues);
     }
     return $aReturn;
 }