Просмотр исходного кода

Better initialization of objects from the "Context" values:
- map the parameters (for example for Provider Contract)
- set the default values when creating a "secondary" object via the (+) button

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1391 a333f486-631f-4898-b8df-5754b55c2be0

dflaven 14 лет назад
Родитель
Сommit
f5709d5f96
3 измененных файлов с 41 добавлено и 17 удалено
  1. 29 0
      application/applicationcontext.class.inc.php
  2. 11 1
      application/ui.extkeywidget.class.inc.php
  3. 1 16
      pages/UI.php

+ 29 - 0
application/applicationcontext.class.inc.php

@@ -204,6 +204,35 @@ class ApplicationContext
 		}
 	}
 
+	/**
+	 * Initializes the given object with the default values provided by the context
+	 */
+	public function InitObjectFromContext(DBObject &$oObj)
+	{
+		$sClass = get_class($oObj);
+		foreach($this->GetNames() as $key)
+		{
+			$aCallSpec = array($sClass, 'MapContextParam');
+			if (is_callable($aCallSpec))
+			{
+				$sAttCode = call_user_func($aCallSpec, $key); // Returns null when there is no mapping for this parameter					
+			}
+
+			if (MetaModel::IsValidAttCode($sClass, $sAttCode))
+			{
+				$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
+				if ($oAttDef->IsWritable())
+				{
+					$value = $this->GetCurrentValue($key, null);
+					if (!is_null($value))
+					{
+						$oObj->Set($sAttCode, $value);
+					}
+				}
+			}
+		}		
+	}
+	
 	static $m_sUrlMakerClass = null;
 
 	/**

+ 11 - 1
application/ui.extkeywidget.class.inc.php

@@ -322,10 +322,20 @@ EOF
 	 */
 	public function GetObjectCreationForm(WebPage $oPage)
 	{
+		// Set all the default values in an object and clone this "default" object
+		$oNewObj = MetaModel::NewObject($this->sTargetClass);
+
+		// 1st - set context values
+		$oAppContext = new ApplicationContext();
+		$oAppContext->InitObjectFromContext($oNewObj);
+
+		// 2nd - set values from the page argument 'default'
+		$oNewObj->UpdateObjectFromArg('default');
+
 		$sDialogTitle = addslashes($this->sTitle);
 		$oPage->add('<div id="ac_create_'.$this->iId.'"><div class="wizContainer" style="vertical-align:top;"><div id="dcr_'.$this->iId.'">');
 		$oPage->add("<h1>".MetaModel::GetClassIcon($this->sTargetClass)."&nbsp;".Dict::Format('UI:CreationTitle_Class', MetaModel::GetName($this->sTargetClass))."</h1>\n");
-	 	cmdbAbstractObject::DisplayCreationForm($oPage, $this->sTargetClass, null, array(), array('formPrefix' => $this->iId, 'noRelations' => true));	
+	 	cmdbAbstractObject::DisplayCreationForm($oPage, $this->sTargetClass, $oNewObj, array(), array('formPrefix' => $this->iId, 'noRelations' => true));	
 		$oPage->add('</div></div></div>');
 //		$oPage->add_ready_script("\$('#ac_create_$this->iId').dialog({ width: $(window).width()*0.8, height: 'auto', autoOpen: false, modal: true, title: '$sDialogTitle'});\n");
 		$oPage->add_ready_script("\$('#ac_create_$this->iId').dialog({ width: 'auto', height: 'auto', autoOpen: false, modal: true, title: '$sDialogTitle'});\n");

+ 1 - 16
pages/UI.php

@@ -1087,22 +1087,7 @@ EOF
 				$oObjToClone = MetaModel::NewObject($sRealClass);
 
 				// 1st - set context values
-				$aContext = $oAppContext->GetAsHash();
-				foreach($oAppContext->GetNames() as $key)
-				{
-					if (MetaModel::IsValidAttCode($sRealClass, $key))
-					{
-						$oAttDef = MetaModel::GetAttributeDef($sRealClass, $key);
-						if ($oAttDef->IsWritable())
-						{
-							$value = $oAppContext->GetCurrentValue($key, null);
-							if (!is_null($value))
-							{
-								$oObjToClone->Set($key, $value);
-							}
-						}
-					}
-				}
+				$oAppContext->InitObjectFromContext($oObjToClone);
 
 				// 2nd - set values from the page argument 'default'
 				$oObjToClone->UpdateObjectFromArg('default');