function do_storeRelationship() { // handle the store, and DON'T give a 500 ;) does not act on the information. global $default; $default->log->error(http_build_query($_REQUEST)); $iFieldsetId = KTUtil::arrayGet($_REQUEST, 'fieldset_id'); $parent_field = KTUtil::arrayGet($_REQUEST, 'parent_field'); $parent_lookup = KTUtil::arrayGet($_REQUEST, 'parent_lookup'); $child_lookups = KTUtil::arrayGet($_REQUEST, 'child_lookups'); // child lookups is a nested array. in python it would be: // child_lookups = // { // field_id:[lookup_id, lookup_id], // field_id:[lookup_id, lookup_id], // } $oFieldset =& KTFieldset::get($iFieldsetId); $oFieldset->setIsComplete(false); $oFieldset->update(); $oParentInstance = KTMetadataUtil::getOrCreateValueInstanceForLookup($parent_lookup); $iBehaviourId = $oParentInstance->getBehaviourId(); $oParentMetadata =& MetaData::get($oParentInstance->getFieldValueId()); if (is_null($iBehaviourId)) { $oBehaviour =& KTFieldBehaviour::createFromArray(array('name' => 'autoinstance' . $oParentInstance->getId(), 'humanname' => 'Auto instance' . $oParentMetadata->getName(), 'fieldid' => $oParentInstance->getFieldId())); } else { $oBehaviour =& KTFieldBehaviour::get($iBehaviourId); } if (PEAR::isError($oBehaviour)) { var_dump($oBehaviour); return $oBehaviour; } $iBehaviourId = $oBehaviour->getId(); $oParentInstance->setBehaviourId($iBehaviourId); $oParentInstance->update(); $sTable = KTUtil::getTableName('field_behaviour_options'); $aOptions = array('noid' => true); $aQuery = array("DELETE FROM {$sTable} WHERE behaviour_id = ?", array($iBehaviourId)); $res = DBUtil::runQuery($aQuery); foreach ($child_lookups as $iFieldId => $aLookups) { foreach ($aLookups as $iLookupId) { $oValueInstance =& KTMetadataUtil::getOrCreateValueInstanceForLookup($iLookupId); if (PEAR::isError($oValueInstance)) { var_dump($oValueInstance); return $oValueInstance; } $res = DBUtil::autoInsert($sTable, array('behaviour_id' => $iBehaviourId, 'field_id' => $iFieldId, 'instance_id' => $oValueInstance->getId()), $aOptions); if (PEAR::isError($res)) { var_dump($res); return $res; } } } }
/** * Checks whether a conditional fieldset has the necessary * relationships set up to be usable - this means that for each * field, no matter how it is reached, there is at least one option * available to choose. */ function checkConditionalFieldsetCompleteness($oFieldset) { $oFieldset =& KTUtil::getObject('KTFieldset', $oFieldset); if ($oFieldset->getIsConditional() == false) { // If we're not conditional, we are fine. return true; } /* * First, ensure at least one master field item has a behaviour * assigned to it. That allows at least one item in the master * field to be chosen. */ $iMasterFieldId = $oFieldset->getMasterFieldId(); $sTable = KTUtil::getTableName('field_value_instances'); $sLookupTable = KTUtil::getTableName('metadata_lookup'); $aQuery = array("SELECT COUNT(FVI.id) AS cnt FROM {$sTable} AS FVI LEFT JOIN {$sLookupTable} AS ML ON (FVI.field_value_id = ML.id) WHERE FVI.field_id = ? AND ML.disabled = 0", array($iMasterFieldId)); $iCount = DBUtil::getOneResultKey($aQuery, 'cnt'); if (PEAR::isError($iCount)) { return $iCount; } $GLOBALS['default']->log->debug("Number of value instances for master field: {$iCount}"); if ($iCount == 0) { $GLOBALS['default']->log->debug("Number of value instances for master field is zero, failing"); return PEAR::raiseError(_kt("Master field has no values which are assigned to behaviours.")); } $GLOBALS['default']->log->debug("Number of value instances for master field is positive, continuing"); // fix for KTS-1023 // check that each master-field value has a valueinstance assigned. $sTable = KTUtil::getTableName('metadata_lookup'); $aQuery = array("SELECT COUNT(id) AS cnt FROM {$sTable} WHERE document_field_id = ? AND disabled = 0 ", array($iMasterFieldId)); $iValCount = DBUtil::getOneResultKey($aQuery, 'cnt'); // assumes that there cannot be more than 1 value instance for each master-field-value. if ($iValCount != $iCount) { return PEAR::raiseError(sprintf(_kt('%d values for the Master Field are not assigned to behaviours.'), $iValCount - $iCount)); } /* * Plan: For each behaviour that is assigned on the system, * ensure that it allows at least one value instance in each of * the fields that it needs to affect. */ $sTable = KTUtil::getTableName('field_value_instances'); $sFieldTable = KTUtil::getTableName('document_fields'); $aQuery = array("SELECT DISTINCT FV.behaviour_id AS behaviour_id FROM {$sTable} AS FV INNER JOIN {$sFieldTable} AS F ON FV.field_id = F.id WHERE F.parent_fieldset = ? AND FV.behaviour_id IS NOT NULL", array($oFieldset->getId())); $aBehaviourIds = DBUtil::getResultArrayKey($aQuery, 'behaviour_id'); if (PEAR::isError($aBehaviourIds)) { return $aBehaviourIds; } foreach ($aBehaviourIds as $iBehaviourId) { $GLOBALS['default']->log->debug("Checking behaviour id: " . $iBehaviourId); $oBehaviour =& KTFieldBehaviour::get($iBehaviourId); $sBehaviourName = $oBehaviour->getName(); $sBehaviourHumanName = $oBehaviour->getHumanName(); $iParentFieldId = $oBehaviour->getFieldId(); $GLOBALS['default']->log->debug(" field is " . $iParentFieldId); $aNextFields = KTMetadataUtil::getChildFieldIds($iParentFieldId); $oParentField =& DocumentField::get($iParentFieldId); $sParentFieldName = $oParentField->getName(); $GLOBALS['default']->log->debug(" next fields must include " . print_r($aNextFields, true)); $sTable = KTUtil::getTableName('field_behaviour_options'); $aQuery = array("SELECT DISTINCT field_id FROM {$sTable} WHERE behaviour_id = ?", array($iBehaviourId)); $aFields = DBUtil::getResultArrayKey($aQuery, 'field_id'); $GLOBALS['default']->log->debug(" actual fields are " . print_r($aNextFields, true)); /* foreach ($aNextFields as $iFieldId) { if (!in_array($iFieldId, $aFields)) { $GLOBALS['default']->log->debug(" field $iFieldId is not included, failing"); $oChildField =& DocumentField::get($iFieldId); $sChildFieldName = $oChildField->getName(); return PEAR::raiseError("Child field $sChildFieldName of parent field $sParentFieldName has no selectable values in behaviour $sBehaviourHumanName ($sBehaviourName)"); } */ } $GLOBALS['default']->log->debug("Got through: passed!"); return true; }
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(); }
function do_finalRename() { $fieldset_id = KTUtil::arrayGet($_REQUEST, "fieldset_id"); $aRenamed = (array) KTUtil::arrayGet($_REQUEST, "renamed"); $this->startTransaction(); foreach ($aRenamed as $bid => $new_name) { $oBehaviour = KTFieldBehaviour::get($bid); if (PEAR::isError($oBehaviour)) { continue; } // skip it... $oBehaviour->setName(trim($new_name)); $res = $oBehaviour->update(); if (PEAR::isError($res)) { $this->errorRedirectToMain(_kt('Failed to change name of behaviour.'), sprintf('action=edit&fFieldsetId=%s', $fieldset_id)); } } $this->addInfoMessage(_kt("Behaviour names changed.")); $this->commitTransaction(); redirect($this->sParentUrl); exit(0); }