Commit d089e62c8401e6ba4f48df591c7a8310bc40330c

Authored by Neil Blakey-Milner
1 parent ea74efd4

Add getOrCreateValueInstanceForLookup for adding instances for lookups

in the simple admin case, getNextValuesForLookup for the simple admin
case.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3759 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/metadata/metadatautil.inc.php
... ... @@ -26,6 +26,8 @@
26 26 */
27 27  
28 28 require_once(KT_LIB_DIR . "/ktentity.inc");
  29 +require_once(KT_LIB_DIR . '/documentmanagement/MetaData.inc');
  30 +require_once(KT_LIB_DIR . '/metadata/valueinstance.inc.php');
29 31  
30 32 class KTMetadataUtil {
31 33 // {{{ getNext
... ... @@ -149,13 +151,74 @@ class KTMetadataUtil {
149 151  
150 152 // {{{ getChildFieldIds
151 153 function getChildFieldIds($oField) {
  154 + $iFieldId = KTUtil::getId($oField);
152 155 $sTable = KTUtil::getTableName('field_orders');
153 156 $aQuery = array("SELECT child_field_id FROM $sTable WHERE parent_field_id = ?",
154   - array($oField->getId()),
  157 + array($iFieldId),
155 158 );
156 159 return DBUtil::getResultArrayKey($aQuery, 'child_field_id');
157 160 }
158 161 // }}}
  162 +
  163 + function &getOrCreateValueInstanceForLookup(&$oLookup) {
  164 + $oLookup =& KTUtil::getObject('MetaData', $oLookup);
  165 + $oValueInstance =& KTValueInstance::getByLookupSingle($oLookup);
  166 + if (PEAR::isError($oValueInstance)) {
  167 + return $oValueInstance;
  168 + }
  169 + // If we got a value instance, return it.
  170 + if (!is_null($oValueInstance)) {
  171 + return $oValueInstance;
  172 + }
  173 + return KTValueInstance::createFromArray(array(
  174 + 'fieldid' => $oLookup->getDocFieldId(),
  175 + 'fieldvalueid' => $oLookup->getId(),
  176 + ));
  177 + }
  178 +
  179 + function getNextValuesForLookup($oLookup) {
  180 + $oLookup =& KTUtil::getObject('MetaData', $oLookup);
  181 + $oInstance =& KTValueInstance::getByLookupSingle($oLookup);
  182 + if (PEAR::isError($oInstance)) {
  183 + return $oInstance;
  184 + }
  185 + if (!is_null($oInstance) && $oInstance->getBehaviourId()) {
  186 + // if we have an instance, and we have a behaviour, return
  187 + // the actual values for that behaviour.
  188 + $oBehaviour =& KTFieldBehaviour::get($oInstance->getBehaviourId());
  189 + return KTMetadataUtil::getNextValuesForBehaviour($oBehaviour);
  190 + }
  191 + // No instance or no behaviour, so send an empty array for each
  192 + // field that we affect.
  193 + $aChildFieldIds = KTMetadataUtil::getChildFieldIds($oLookup->getDocFieldId());
  194 + foreach ($aChildFieldIds as $iFieldId) {
  195 + $aValues[$iFieldId] = array();
  196 + }
  197 + return $aValues;
  198 + }
  199 +
  200 + function getNextValuesForBehaviour($oBehaviour) {
  201 + $oBehaviour =& KTUtil::getObject('KTFieldBehaviour', $oBehaviour);
  202 + $aValues = array();
  203 + $sTable = KTUtil::getTableName('field_behaviour_options');
  204 + $aChildFieldIds = KTMetadataUtil::getChildFieldIds($oBehaviour->getFieldId());
  205 + foreach ($aChildFieldIds as $iFieldId) {
  206 + $aValues[$iFieldId] = array();
  207 + }
  208 + $aQuery = array(
  209 + "SELECT field_id, instance_id FROM $sTable WHERE behaviour_id = ?",
  210 + array($oBehaviour->getId()),
  211 + );
  212 + $aRows = DBUtil::getResultArray($aQuery);
  213 + if (PEAR::isError($aRows)) {
  214 + return $aRows;
  215 + }
  216 + foreach ($aRows as $aRow) {
  217 + $oInstance =& KTValueInstance::get($aRow['instance_id']);
  218 + $aValues[$aRow['field_id']][] = $oInstance->getFieldValueId();
  219 + }
  220 + return $aValues;
  221 + }
159 222 }
160 223  
161 224 ?>
... ...