Browse Source

Bug fix: allow modifying an object as if in target state, before applying the stimulus (cf PortalWebpage).

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1508 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 13 years ago
parent
commit
b82e9b6022
2 changed files with 35 additions and 19 deletions
  1. 12 9
      application/cmdbabstract.class.inc.php
  2. 23 10
      application/portalwebpage.class.inc.php

+ 12 - 9
application/cmdbabstract.class.inc.php

@@ -2175,10 +2175,12 @@ EOF
 	
 	
 	/**
 	/**
 	 * Updates the object from a flat array of values
 	 * Updates the object from a flat array of values
-	 * @param array $aAttList array of attcode
+	 * @param $aAttList array $aAttList array of attcode
+	 * @param $aErrors array Returns information about slave attributes
+	 * @param $sTargetState string Target state for which to evaluate the writeable attributes (=current state is empty)
 	 * @return array of attcodes that can be used for writing on the current object
 	 * @return array of attcodes that can be used for writing on the current object
 	 */
 	 */
-	public function GetWriteableAttList($aAttList, &$aErrors)
+	public function GetWriteableAttList($aAttList, &$aErrors, $sTargetState = '')
 	{
 	{
 		if (!is_array($aAttList))
 		if (!is_array($aAttList))
 		{
 		{
@@ -2187,7 +2189,8 @@ EOF
 			// WARNING: if you change this also check the functions DisplayModifyForm and DisplayCaseLog
 			// WARNING: if you change this also check the functions DisplayModifyForm and DisplayCaseLog
 			foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode => $oAttDef)
 			foreach(MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode => $oAttDef)
 			{
 			{
-				$iFlags = $this->GetAttributeFlags($sAttCode);
+				$aVoid = array();
+				$iFlags = $this->GetAttributeFlags($sAttCode, $aVoid, $sTargetState);
 				if ($oAttDef instanceof AttributeCaseLog)
 				if ($oAttDef instanceof AttributeCaseLog)
 				{
 				{
 					if (!($iFlags & (OPT_ATT_HIDDEN|OPT_ATT_SLAVE|OPT_ATT_READONLY)))
 					if (!($iFlags & (OPT_ATT_HIDDEN|OPT_ATT_SLAVE|OPT_ATT_READONLY)))
@@ -2198,13 +2201,13 @@ EOF
 				}
 				}
 			}
 			}
 		}
 		}
-
 		$aWriteableAttList = array();
 		$aWriteableAttList = array();
 		foreach($aAttList as $sAttCode)
 		foreach($aAttList as $sAttCode)
 		{
 		{
 			$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
 			$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
 			
 			
-			$iFlags = $this->GetAttributeFlags($sAttCode);
+			$aVoid = array();
+			$iFlags = $this->GetAttributeFlags($sAttCode, $aVoid, $sTargetState);
 			if ($oAttDef->IsWritable())
 			if ($oAttDef->IsWritable())
 			{
 			{
 				if ( $iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
 				if ( $iFlags & (OPT_ATT_HIDDEN | OPT_ATT_READONLY))
@@ -2328,11 +2331,11 @@ EOF
 	/**
 	/**
 	 * Updates the object from the POSTed parameters (form)
 	 * Updates the object from the POSTed parameters (form)
 	 */
 	 */
-	public function UpdateObjectFromPostedForm($sFormPrefix = '', $aAttList = null)
+	public function UpdateObjectFromPostedForm($sFormPrefix = '', $aAttList = null, $sTargetState = '')
 	{
 	{
 		$aErrors = array();
 		$aErrors = array();
 		$aValues = array();
 		$aValues = array();
-		foreach($this->GetWriteableAttList($aAttList, $aErrors) as $sAttCode => $oAttDef)
+		foreach($this->GetWriteableAttList($aAttList, $aErrors, $sTargetState) as $sAttCode => $oAttDef)
 		{
 		{
 			if ($oAttDef->GetEditClass() == 'Document')
 			if ($oAttDef->GetEditClass() == 'Document')
 			{
 			{
@@ -2361,14 +2364,14 @@ EOF
 	/**
 	/**
 	 * Updates the object from a given page argument
 	 * Updates the object from a given page argument
 	 */
 	 */
-	public function UpdateObjectFromArg($sArgName, $aAttList = null)
+	public function UpdateObjectFromArg($sArgName, $aAttList = null, $sTargetState = '')
 	{
 	{
 		$aErrors = array();
 		$aErrors = array();
 
 
 		$aRawValues = utils::ReadParam($sArgName, array(), '', 'raw_data');
 		$aRawValues = utils::ReadParam($sArgName, array(), '', 'raw_data');
 
 
 		$aValues = array();
 		$aValues = array();
-		foreach($this->GetWriteableAttList($aAttList, $aErrors) as $sAttCode => $oAttDef)
+		foreach($this->GetWriteableAttList($aAttList, $aErrors, $sTargetState) as $sAttCode => $oAttDef)
 		{
 		{
 			if (isset($aRawValues[$sAttCode]))
 			if (isset($aRawValues[$sAttCode]))
 			{
 			{

+ 23 - 10
application/portalwebpage.class.inc.php

@@ -614,20 +614,33 @@ EOF
 		}
 		}
 	
 	
 		$sClass = get_class($oObj);
 		$sClass = get_class($oObj);
-	
-		$oObj->UpdateObjectFromPostedForm('' /* form prefix */, $aAttList);
-	
-	   // Optional: apply a stimulus
-	   //
+
 		$sStimulus = trim(utils::ReadPostedParam('apply_stimulus', ''));
 		$sStimulus = trim(utils::ReadPostedParam('apply_stimulus', ''));
-	   if (!empty($sStimulus))
-	   {
+		$sTargetState = '';
+		if (!empty($sStimulus))
+		{
+			// Compute the target state
+
+			$aTransitions = $oObj->EnumTransitions();
+			if (!isset($aTransitions[$sStimulus]))
+			{
+				throw new ApplicationException(Dict::Format('UI:Error:Invalid_Stimulus_On_Object_In_State', $sStimulus, $oObj->GetName(), $oObj->GetStateLabel()));
+			}
+			$sTargetState = $aTransitions[$sStimulus]['target_state'];
+		}
+			
+		$oObj->UpdateObjectFromPostedForm('' /* form prefix */, $aAttList, $sTargetState);
+
+		// Optional: apply a stimulus
+		//
+		if (!empty($sStimulus))
+		{
 			if (!$oObj->ApplyStimulus($sStimulus))
 			if (!$oObj->ApplyStimulus($sStimulus))
 			{
 			{
-				throw new Exception("Cannot apply stimulus '$sStimulus' to {$oObj->GetName()}");
+					throw new Exception("Cannot apply stimulus '$sStimulus' to {$oObj->GetName()}");
 			}
 			}
 		}
 		}
-	
+		
 		// Record the change
 		// Record the change
 		//
 		//
 		$oMyChange = MetaModel::NewObject("CMDBChange");
 		$oMyChange = MetaModel::NewObject("CMDBChange");
@@ -636,7 +649,7 @@ EOF
 		$oMyChange->Set("userinfo", $sUserString);
 		$oMyChange->Set("userinfo", $sUserString);
 		$iChangeId = $oMyChange->DBInsert();
 		$iChangeId = $oMyChange->DBInsert();
 		$oObj->DBUpdateTracked($oMyChange);
 		$oObj->DBUpdateTracked($oMyChange);
-	
+		
 		$this->p("<h1>".Dict::Format('UI:Class_Object_Updated', MetaModel::GetName(get_class($oObj)), $oObj->GetName())."</h1>\n");
 		$this->p("<h1>".Dict::Format('UI:Class_Object_Updated', MetaModel::GetName(get_class($oObj)), $oObj->GetName())."</h1>\n");
 	}
 	}