Browse Source

Completed the implementation of 1:n links. The default value should now works for all external keys (whether they are displayed as an autocomplete or or a simple select)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@253 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 15 years ago
parent
commit
bd74f4e6c9
2 changed files with 27 additions and 14 deletions
  1. 25 13
      application/cmdbabstract.class.inc.php
  2. 2 1
      pages/ajax.render.php

+ 25 - 13
application/cmdbabstract.class.inc.php

@@ -35,7 +35,7 @@ abstract class cmdbAbstractObject extends CMDBObject
 
 	protected static function MakeHyperLink($sObjClass, $sObjKey, $aAvailableFields)
 	{
-		if ($sObjKey == 0) return '<em>undefined</em>';
+		if ($sObjKey <= 0) return '<em>undefined</em>'; // Objects built in memory have negative IDs
 
 		$oAppContext = new ApplicationContext();	
 		$sExtClassNameAtt = MetaModel::GetNameAttributeCode($sObjClass);
@@ -89,23 +89,35 @@ abstract class cmdbAbstractObject extends CMDBObject
 			
 			if ($oAtt->IsExternalKey())
 			{
-				// retrieve the "external fields" linked to this external key
 				$sTargetClass = $oAtt->GetTargetClass();
-				$aAvailableFields = array();
-				foreach (MetaModel::GetExternalFields(get_class($this), $sAttCode) as $oExtField)
+				if ($this->IsNew())
 				{
-					$aAvailableFields[$oExtField->GetExtAttCode()] = $oExtField->GetAsHTML($this->Get($oExtField->GetCode()));
-				}
-				$sExtClassNameAtt = MetaModel::GetNameAttributeCode($sTargetClass);
-				// Use the "name" of the target class as the label of the hyperlink
-				// unless it's not available in the external fields...
-				if (isset($aAvailableFields[$sExtClassNameAtt]))
-				{
-					$sDisplayValue = $aAvailableFields[$sExtClassNameAtt];
+					// The current object exists only in memory, don't try to query it in the DB !
+					// instead let's query for the object pointed by the external key, and get its name
+					$targetObjId = $this->Get($sAttCode);
+					$oTargetObj = MetaModel::GetObject($sTargetClass, $targetObjId, false); // false => not sure it exists
+					if (is_object($oTargetObj))
+					{
+						$sDisplayValue = $oTargetObj->GetName();
+					}					
 				}
 				else
 				{
-					$sDisplayValue = implode(' / ', $aAvailableFields);
+					// retrieve the "external fields" linked to this external key
+					foreach (MetaModel::GetExternalFields(get_class($this), $sAttCode) as $oExtField)
+					{
+						$aAvailableFields[$oExtField->GetExtAttCode()] = $oExtField->GetAsHTML($this->Get($oExtField->GetCode()));
+					}
+					// Use the "name" of the target class as the label of the hyperlink
+					// unless it's not available in the external fields...
+					if (isset($aAvailableFields[$sExtClassNameAtt]))
+					{
+						$sDisplayValue = $aAvailableFields[$sExtClassNameAtt];
+					}
+					else
+					{
+						$sDisplayValue = implode(' / ', $aAvailableFields);
+					}
 				}
 			}
 			else

+ 2 - 1
pages/ajax.render.php

@@ -89,8 +89,9 @@ switch($operation)
 	{
 		$sId = $oWizardHelper->GetIdForField($sAttCode);
 		$value = $oObj->Get($sAttCode);
+		$displayValue = $oObj->GetDisplayValue($sAttCode);
 		$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
-		$sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, '', 'att_'.$sId, '', 0, array('this' => $oObj));
+		$sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, 'att_'.$sId, '', 0, array('this' => $oObj));
 
 		$oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
 	}