/**
  * 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;
 }
 function widgetsForFieldset($fieldsetOrType, $sContainerName, $oDocument = null)
 {
     // this is likely to be called repeatedly.
     if (is_null($this->oWF)) {
         $this->oWF =& KTWidgetFactory::getSingleton();
     }
     // we're going to create one of two things, here:
     //   - conditional fieldset widget
     //   - a "Fieldset" widget
     // FIXME delegate.
     $oFieldset =& $fieldsetOrType;
     $widgets = array();
     $fields = $oFieldset->getFields();
     if ($oFieldset->getIsConditional()) {
         $iMasterId = $oFieldset->getMasterFieldId();
         $oMasterField = DocumentField::get($iMasterId);
         $newfields = array();
         $newfields[] = $oMasterField;
         foreach ($fields as $oField) {
             if ($oField->getId() != $iMasterId) {
                 $newfields[] = $oField;
             }
         }
         foreach ($newfields as $oField) {
             $fname = 'metadata_' . $oField->getId();
             $value = null;
             if (!is_null($oDocument)) {
                 $oFL = DocumentFieldLink::getByDocumentAndField($oDocument, $oField);
                 if (!is_null($oFL) && !PEAR::isError($oFL)) {
                     $value = $oFL->getValue();
                 }
             }
             $widgets[] = $this->oWF->get('ktcore.widgets.conditionalselection', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription(), 'vocab' => MetaData::getEnabledByDocumentField($oField), 'id_method' => 'getName', 'label_method' => 'getName', 'unselected_label' => _kt("No selection."), 'simple_select' => false, 'master' => $oField->getId() == $iMasterId, 'masterid' => $iMasterId, 'fieldset' => $oFieldset->getId(), 'field' => $oField->getId()));
         }
     } else {
         foreach ($fields as $oField) {
             $fname = 'metadata_' . $oField->getId();
             $value = null;
             // check if we had an old value
             if (!is_null($oDocument)) {
                 $oFL = DocumentFieldLink::getByDocumentAndField($oDocument, $oField);
                 if (!is_null($oFL) && !PEAR::isError($oFL)) {
                     $value = $oFL->getValue();
                 }
             }
             // we have to hack in support for the hardcoded types of fields
             // handled by the "generic" widget.
             //
             // we try to make this a little more "sane"
             $type = '';
             if ($oField->getHasLookup()) {
                 if ($oField->getHasLookupTree()) {
                     $type = 'ktcore.fields.tree';
                 } else {
                     $type = 'ktcore.fields.lookup';
                 }
             } else {
                 $type = 'ktcore.fields.string';
             }
             if ($type == 'ktcore.fields.string') {
                 $widgets[] = $this->oWF->get('ktcore.widgets.string', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription()));
             } else {
                 if ($type == 'ktcore.fields.lookup') {
                     $widgets[] = $this->oWF->get('ktcore.widgets.entityselection', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription(), 'vocab' => MetaData::getEnabledByDocumentField($oField), 'id_method' => 'getName', 'label_method' => 'getName', 'unselected_label' => _kt("No selection."), 'simple_select' => false));
                 } else {
                     if ($type == 'ktcore.fields.tree') {
                         $widgets[] = $this->oWF->get('ktcore.widgets.treemetadata', array('label' => $oField->getName(), 'required' => $oField->getIsMandatory(), 'name' => $fname, 'value' => $value, 'description' => $oField->getDescription(), 'vocab' => MetaData::getEnabledByDocumentField($oField), 'field_id' => $oField->getId()));
                     }
                 }
             }
         }
     }
     return array($this->oWF->get('ktcore.widgets.fieldset', array('label' => $oFieldset->getName(), 'description' => $oFieldset->getDescription(), 'name' => $sContainerName, 'widgets' => $widgets)));
 }
 function formAdaptor($oFieldset, $oDocument = null)
 {
     $widgets = array();
     $validators = array();
     $oVF =& KTValidationFactory::getSingleton();
     $oWF =& KTWidgetFactory::getSingleton();
     $fields =& $oFieldset->getFields();
     foreach ($fields as $oField) {
         // FIXME we probably want to use some form of factory here.
         $field_name = 'metadata_' . $oField->getId();
         if ($field->getHasLookup()) {
             // lookups
             if ($field->getHasLookupTree()) {
                 // tree
                 // FIXME we don't handle trees yet
                 continue;
                 /*
                 $fieldTree = new MDTree();
                                     $fieldTree->buildForField($field->getId());
                                     $fieldTree->setActiveItem($current_value);
                     	    		$fieldOptions['tree'] = $fieldTree->_evilTreeRenderer($fieldTree, $fieldName);
                                     $oField = new KTTreeWidget($fieldLabel, $fieldDescription, $fieldName, $fieldValue, $page, $fieldRequired, null, $fieldErrors, $fieldOptions);
                 */
             } else {
                 // normal
                 $widgets[] = $oWF->get('ktcore.widgets.entityselection', array('label' => $oField->getName(), 'name' => 'metadata_' . $oField->getId(), 'description' => $oField->getDescription(), 'vocab' => MetaData::getEnabledByDocumentField($oField), 'id_method' => 'getName', 'label_method' => 'getName', 'required' => $oField->getIsMandatory()));
                 if ($oField->getIsMandatory()) {
                     $validators[] = $oVF->get('ktcore.validators.required', array('test' => $field_name, 'basename' => 'metadata_'));
                 }
                 $validators[] = $oVF->get('ktcore.validators.membership', array('test' => $field_name, 'output' => $field_name, 'basename' => $field_name, 'vocab' => MetaData::getEnabledValuesForField($oField)));
             }
         } else {
             $widgets[] = $oWF->get('ktcore.widgets.string', array('label' => $oField->getName(), 'output' => $field_name));
             if ($oField->getIsMandatory()) {
                 $validators[] = $oVF->get('ktcore.validators.required', array('test' => $field_name, 'basename' => $field_name));
             }
         }
     }
     return array('widgets' => $widgets, 'validators' => $validators);
 }
function getWidgetForMetadataField($field, $current_value, $page, $errors = null, $vocab = null, $aOptions = null)
{
    // all fields have these elements.
    $fieldLabel = $field->getName();
    $fieldDescription = $field->getDescription();
    $fieldValue = $current_value;
    $fieldErrors = $errors;
    // array of strings
    $fieldName = 'metadata_' . $field->getID();
    $fieldOptions = array();
    $fieldRequired = $field->getIsMandatory() || KTUtil::arrayGet($aOptions, 'required', false);
    if ($fieldRequired == 1) {
        $fieldRequired = true;
    }
    if ($errors === null) {
        $fieldErrors = array();
    } else {
        $fieldErrors = $errors;
    }
    // now we need to break, based on a few aspects of the oField (DocumentField)
    if ($field->getHasLookup()) {
        // could either be normal, or a tree.
        // ignore trees (for now).
        if (!$field->getHasLookupTree()) {
            // FIXME we need to somehow handle both value-value and id-value here
            // extract the lookup.
            if ($vocab === null) {
                // allow override
                $lookups = MetaData::getEnabledByDocumentField($field);
                $fieldOptions['vocab'] = array();
                // FIXME handle lookups
                $fieldOptions['vocab'][''] = _kt('Select a value');
                foreach ($lookups as $md) {
                    $fieldOptions['vocab'][$md->getName()] = $md->getName();
                }
            } else {
                $fieldOptions['vocab'] = $vocab;
            }
            $oField = new KTLookupWidget($fieldLabel, $fieldDescription, $fieldName, $fieldValue, $page, $fieldRequired, null, $fieldErrors, $fieldOptions);
        } else {
            // FIXME vocab's are _not_ supported for tree-inputs.  this means conditional-tree-widgets are not unsupported.
            // for trees, we are currently brutal.
            $fieldTree = new MDTree();
            $fieldTree->buildForField($field->getId());
            $fieldTree->setActiveItem($current_value);
            $fieldOptions['tree'] = $fieldTree->_evilTreeRenderer($fieldTree, $fieldName);
            $oField = new KTTreeWidget($fieldLabel, $fieldDescription, $fieldName, $fieldValue, $page, $fieldRequired, null, $fieldErrors, $fieldOptions);
        }
    } else {
        $oField = new KTBaseWidget($fieldLabel, $fieldDescription, $fieldName, $fieldValue, $page, $fieldRequired, null, $fieldErrors, $fieldOptions);
    }
    return $oField;
}