/**
  * 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;
 }
示例#2
0
 function do_createlookupvalues()
 {
     $oForm = $this->form_addlookups();
     $res = $oForm->validate();
     $data = $res['results'];
     $errors = $res['errors'];
     $extra_errors = array();
     $failed = array();
     $lookups = array();
     $raw_lookups = $data['lookups'];
     $lookup_candidates = explode("\n", $raw_lookups);
     foreach ($lookup_candidates as $candidate) {
         $name = trim($candidate);
         if (empty($name)) {
             continue;
         }
         // check for existing or to-be-created lookups.
         if ($lookups[$name]) {
             $failed[$name] = $name;
             continue;
         }
         if ($failed[$name]) {
             continue;
             // already blown up, fix it.
         }
         $oOldLookup = MetaData::getByValueAndDocumentField($name, $this->oField);
         if (!PEAR::isError($oOldLookup)) {
             $failed[$name] = $name;
             continue;
         }
         $lookups[$name] = $name;
     }
     if (!empty($failed)) {
         $extra_errors['lookups'][] = sprintf(_kt("The following lookups you specified already exist, or are specified twice: %s"), implode(', ', $failed));
     } else {
         if (empty($lookups)) {
             $extra_errors['lookups'][] = _kt("You must have at least 1 new lookup value.");
         }
     }
     if (!empty($errors) || !empty($extra_errors)) {
         return $oForm->handleError(null, $extra_errors);
     }
     $data['lookups'] = $lookups;
     foreach ($lookups as $value) {
         $oLookup = MetaData::createFromArray(array('DocFieldId' => $this->oField->getId(), 'sName' => $value, 'iTreeParent' => null, 'bDisabled' => false, 'bIsStuck' => false));
         if (PEAR::isError($oLookup)) {
             return $oForm->handleError(sprintf(_kt("Failed to create lookup: %s"), $oLookup->getMessage()));
         }
     }
     $this->successRedirectTo('managefield', sprintf(_kt("%d lookups added."), count($lookups)));
 }