浏览代码

- First (partial) implementation of context and default values.

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@223 a333f486-631f-4898-b8df-5754b55c2be0
dflaven 15 年之前
父节点
当前提交
b4dfe4fd07

+ 24 - 6
application/applicationcontext.class.inc.php

@@ -14,6 +14,7 @@ class ApplicationContext
 {
 {
 	protected $aNames;
 	protected $aNames;
 	protected $aValues;
 	protected $aValues;
+	protected static $aDefaultValues; // Cache shared among all instances
 	
 	
 	public function __construct()
 	public function __construct()
 	{
 	{
@@ -29,16 +30,20 @@ class ApplicationContext
 	 */
 	 */
 	protected function ReadContext()
 	protected function ReadContext()
 	{
 	{
-		$this->aValues = array();
-		foreach($this->aNames as $sName)
+		if (empty(self::$aDefaultValues))
 		{
 		{
-			$sValue = utils::ReadParam($sName, '');
-			// TO DO: check if some of the context parameters are mandatory (or have default values)
-			if (!empty($sValue))
+			self::$aDefaultValues = array();
+			foreach($this->aNames as $sName)
 			{
 			{
-				$this->aValues[$sName] = $sValue;
+				$sValue = utils::ReadParam($sName, '');
+				// TO DO: check if some of the context parameters are mandatory (or have default values)
+				if (!empty($sValue))
+				{
+					self::$aDefaultValues[$sName] = $sValue;
+				}
 			}
 			}
 		}
 		}
+		$this->aValues = self::$aDefaultValues;
 	}
 	}
 	
 	
 	/**
 	/**
@@ -77,5 +82,18 @@ class ApplicationContext
 	{
 	{
 		return $this->aValues;
 		return $this->aValues;
 	}
 	}
+	/**
+	 * Removes the specified parameter from the context, for example when the same parameter
+	 * is already a search parameter
+	 * @param string $sParamName Name of the parameter to remove	 	 
+	 * @return none
+	 */	
+	public function Reset($sParamName)
+	{
+		if (isset($this->aValues[$sParamName]))
+		{
+			unset($this->aValues[$sParamName]);
+		}
+	}
 }
 }
 ?>
 ?>

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

@@ -507,12 +507,13 @@ abstract class cmdbAbstractObject extends CMDBObject
 	public static function GetSearchForm(web_page $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
 	public static function GetSearchForm(web_page $oPage, CMDBObjectSet $oSet, $aExtraParams = array())
 	{
 	{
 		static $iSearchFormId = 0;
 		static $iSearchFormId = 0;
+		$oAppContext = new ApplicationContext();
 		$sHtml = '';
 		$sHtml = '';
 		$numCols=4;
 		$numCols=4;
 		$iSearchFormId++;
 		$iSearchFormId++;
 		$sClassName = $oSet->GetFilter()->GetClass();
 		$sClassName = $oSet->GetFilter()->GetClass();
 
 
-		// Romain: temporariy removed the tab "OQL query" because it was not finalized
+		// Romain: temporarily removed the tab "OQL query" because it was not finalized
 		// (especially when used to add a link)
 		// (especially when used to add a link)
 		/*
 		/*
 		$sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
 		$sHtml .= "<div class=\"mini_tabs\" id=\"mini_tabs{$iSearchFormId}\"><ul>
@@ -536,6 +537,7 @@ abstract class cmdbAbstractObject extends CMDBObject
 		$aList = MetaModel::GetZListItems($sClassName, 'standard_search');
 		$aList = MetaModel::GetZListItems($sClassName, 'standard_search');
 		foreach($aList as $sFilterCode)
 		foreach($aList as $sFilterCode)
 		{
 		{
+			$oAppContext->Reset($sFilterCode); // Make sure the same parameter will not be passed twice
 			if (($index % $numCols) == 0)
 			if (($index % $numCols) == 0)
 			{
 			{
 				if ($index != 0)
 				if ($index != 0)
@@ -608,6 +610,7 @@ abstract class cmdbAbstractObject extends CMDBObject
 		$sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
 		$sHtml .= "<input type=\"hidden\" name=\"class\" value=\"$sClassName\" />\n";
 		$sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
 		$sHtml .= "<input type=\"hidden\" name=\"dosearch\" value=\"1\" />\n";
 		$sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
 		$sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_form\" />\n";
+		$sHtml .= $oAppContext->GetForForm();
 		$sHtml .= "</form>\n";		
 		$sHtml .= "</form>\n";		
 		$sHtml .= "</div><!-- Simple search form -->\n";
 		$sHtml .= "</div><!-- Simple search form -->\n";
 
 
@@ -634,6 +637,7 @@ abstract class cmdbAbstractObject extends CMDBObject
 			$sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
 			$sHtml .= "<input type=\"hidden\" name=\"$sName\" value=\"$sValue\" />\n";
 		}
 		}
 		$sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
 		$sHtml .= "<input type=\"hidden\" name=\"operation\" value=\"search_oql\" />\n";
+		$sHtml .= $oAppContext->GetForForm();
 		$sHtml .= "</table></form>\n";
 		$sHtml .= "</table></form>\n";
 		$sHtml .= "</div><!-- OQL query form -->\n";
 		$sHtml .= "</div><!-- OQL query form -->\n";
 		return $sHtml;
 		return $sHtml;
@@ -642,6 +646,12 @@ abstract class cmdbAbstractObject extends CMDBObject
 	public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
 	public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
 	{
 	{
 		static $iInputId = 0;
 		static $iInputId = 0;
+		if (isset($aArgs[$sAttCode]) && empty($value))
+		{
+			// default value passed by the context (either the app context of the operation)
+			$value = $aArgs[$sAttCode];
+		}
+
 		if (!empty($iId))
 		if (!empty($iId))
 		{
 		{
 			$iInputId = $iId;
 			$iInputId = $iId;
@@ -797,7 +807,7 @@ abstract class cmdbAbstractObject extends CMDBObject
 		$oPage->add("</form>\n");
 		$oPage->add("</form>\n");
 	}
 	}
 	
 	
-	public static function DisplayCreationForm(web_page $oPage, $sClass, $oObjectToClone = null)
+	public static function DisplayCreationForm(web_page $oPage, $sClass, $oObjectToClone = null, $aArgs = array())
 	{
 	{
 		static $iCreationFormId = 0;
 		static $iCreationFormId = 0;
 
 
@@ -835,7 +845,7 @@ abstract class cmdbAbstractObject extends CMDBObject
 				$sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
 				$sDisplayValue = ($oObjectToClone == null) ? '' : $oObjectToClone->GetDisplayValue($sAttCode);
 				$iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
 				$iOptions = isset($aStates[$sTargetState]['attribute_list'][$sAttCode]) ? $aStates[$sTargetState]['attribute_list'][$sAttCode] : 0;
 				
 				
-				$sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions);
+				$sHTMLValue = self::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $sValue, $sDisplayValue, '', '', $iOptions, $aArgs);
 				$aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
 				$aDetails[] = array('label' => $oAttDef->GetLabel(), 'value' => $sHTMLValue);
 			}
 			}
 		}
 		}

+ 9 - 3
application/displayblock.class.inc.php

@@ -257,10 +257,12 @@ class DisplayBlock
 				}
 				}
 				$sFilter = urlencode($this->m_oFilter->serialize());
 				$sFilter = urlencode($this->m_oFilter->serialize());
 				$aData = array();
 				$aData = array();
+				$oAppContext = new ApplicationContext();
+				$sParams = $oAppContext->GetForLink();
 				foreach($aGroupBy as $sValue => $iCount)
 				foreach($aGroupBy as $sValue => $iCount)
 				{
 				{
 					$aData[] = array ( 'group' => $sValue,
 					$aData[] = array ( 'group' => $sValue,
-									  'value' => "<a href=\"./UI.php?operation=search&dosearch=1&filter=$sFilter&$sGroupByField=".urlencode($sValue)."\">$iCount</a>"); // TO DO: add the context information
+									  'value' => "<a href=\"./UI.php?operation=search&dosearch=1&$sParams&filter=$sFilter&$sGroupByField=".urlencode($sValue)."\">$iCount</a>"); // TO DO: add the context information
 				}
 				}
 				$sHtml .= $oPage->GetTable(array('group' => array('label' => MetaModel::GetLabel($this->m_oFilter->GetClass(), $sGroupByField), 'description' => ''), 'value' => array('label'=>'Count', 'description' => 'Number of elements')), $aData);
 				$sHtml .= $oPage->GetTable(array('group' => array('label' => MetaModel::GetLabel($this->m_oFilter->GetClass(), $sGroupByField), 'description' => ''), 'value' => array('label'=>'Count', 'description' => 'Number of elements')), $aData);
 			}
 			}
@@ -287,7 +289,9 @@ class DisplayBlock
 				{
 				{
 					if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES)
 					if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES)
 					{
 					{
-						$sHtml .= $oPage->GetP("<a href=\"./UI.php?operation=new&class=$sClass\">Click here to create a new ".Metamodel::GetName($sClass)."</a>\n");
+						$oAppContext = new ApplicationContext();
+						$sParams = $oAppContext->GetForLink();
+						$sHtml .= $oPage->GetP("<a href=\"./UI.php?operation=new&class=$sClass&$sParams\">Click here to create a new ".Metamodel::GetName($sClass)."</a>\n");
 					}
 					}
 				}
 				}
 			}
 			}
@@ -312,7 +316,9 @@ class DisplayBlock
 				{
 				{
 					if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES)
 					if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES)
 					{
 					{
-						$sHtml .= $oPage->GetP("<a href=\"../pages/UI.php?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&id=".$aExtraParams['object_id']."&target_class=$sTargetClass&addObjects=true\">Click here to add new ".Metamodel::GetName($sTargetClass)."s</a>\n");
+						$oAppContext = new ApplicationContext();
+						$sParams = $oAppContext->GetForLink();
+						$sHtml .= $oPage->GetP("<a href=\"../pages/UI.php?operation=modify_links&class=$sClass&sParams&link_attr=".$aExtraParams['link_attr']."&id=".$aExtraParams['object_id']."&target_class=$sTargetClass&addObjects=true\">Click here to add new ".Metamodel::GetName($sTargetClass)."s</a>\n");
 					}
 					}
 				}
 				}
 			}
 			}

+ 4 - 2
application/uiwizard.class.inc.php

@@ -25,7 +25,7 @@ class UIWizard
 	/**
 	/**
 	 * Displays one step of the wizard
 	 * Displays one step of the wizard
 	 */	 
 	 */	 
-	public function DisplayWizardStep($aStep, $iStepIndex, &$iMaxInputId, &$aFieldsMap, $bFinishEnabled = false)
+	public function DisplayWizardStep($aStep, $iStepIndex, &$iMaxInputId, &$aFieldsMap, $bFinishEnabled = false, $aArgs = array())
 	{
 	{
 		if ($iStepIndex == 1) // one big form that contains everything, to make sure that the uploaded files are posted too
 		if ($iStepIndex == 1) // one big form that contains everything, to make sure that the uploaded files are posted too
 		{
 		{
@@ -60,7 +60,7 @@ class UIWizard
 				
 				
 				$sFieldFlag = ($iOptions & (OPT_ATT_MANDATORY | OPT_ATT_MUSTCHANGE)) ? ' <span class="hilite">*</span>' : '';
 				$sFieldFlag = ($iOptions & (OPT_ATT_MANDATORY | OPT_ATT_MUSTCHANGE)) ? ' <span class="hilite">*</span>' : '';
 				$oDefaultValuesSet = $oAttDef->GetDefaultValue(); // @@@ TO DO: get the object's current value if the object exists
 				$oDefaultValuesSet = $oAttDef->GetDefaultValue(); // @@@ TO DO: get the object's current value if the object exists
-				$sHTMLValue = cmdbAbstractObject::GetFormElementForField($this->m_oPage, $this->m_sClass, $sAttCode, $oAttDef, $oDefaultValuesSet, '', "att_$iMaxInputId", '', $iOptions);
+				$sHTMLValue = cmdbAbstractObject::GetFormElementForField($this->m_oPage, $this->m_sClass, $sAttCode, $oAttDef, $oDefaultValuesSet, '', "att_$iMaxInputId", '', $iOptions, $aArgs);
 				$aFieldsMap[$iMaxInputId] = $sAttCode;
 				$aFieldsMap[$iMaxInputId] = $sAttCode;
 				$aDetails[] = array('label' => $oAttDef->GetLabel().$sFieldFlag, 'value' => "<div id=\"field_$iMaxInputId\">$sHTMLValue</div>");
 				$aDetails[] = array('label' => $oAttDef->GetLabel().$sFieldFlag, 'value' => "<div id=\"field_$iMaxInputId\">$sHTMLValue</div>");
 				if ($oAttDef->GetValuesDef() != null)
 				if ($oAttDef->GetValuesDef() != null)
@@ -108,6 +108,7 @@ $sJSHandlerCode
 	 */	 	
 	 */	 	
 	public function DisplayFinalStep($iStepIndex, $aFieldsMap)
 	public function DisplayFinalStep($iStepIndex, $aFieldsMap)
 	{
 	{
+		$oAppContext = new ApplicationContext();
 		$this->m_oPage->add("<div class=\"wizContainer\" id=\"wizStep$iStepIndex\" style=\"display:none;\">\n");
 		$this->m_oPage->add("<div class=\"wizContainer\" id=\"wizStep$iStepIndex\" style=\"display:none;\">\n");
 		$this->m_oPage->add("<a name=\"step$iStepIndex\" />\n");
 		$this->m_oPage->add("<a name=\"step$iStepIndex\" />\n");
 		$this->m_oPage->P("Final step: confirmation");
 		$this->m_oPage->P("Final step: confirmation");
@@ -126,6 +127,7 @@ $sJSHandlerCode
 		$this->m_oPage->add("</script>\n");
 		$this->m_oPage->add("</script>\n");
 		$this->m_oPage->add("<div id=\"object_preview\">\n");
 		$this->m_oPage->add("<div id=\"object_preview\">\n");
 		$this->m_oPage->add("</div>\n");
 		$this->m_oPage->add("</div>\n");
+		$this->m_oPage->add($oAppContext->GetForForm());		
 		$this->m_oPage->add("<input type=\"submit\" value=\"Create ".MetaModel::GetName($this->m_sClass)."\" />\n");
 		$this->m_oPage->add("<input type=\"submit\" value=\"Create ".MetaModel::GetName($this->m_sClass)."\" />\n");
 		$this->m_oPage->add("</div>\n");
 		$this->m_oPage->add("</div>\n");
 		$this->m_oPage->add("</form>\n");
 		$this->m_oPage->add("</form>\n");

+ 4 - 4
pages/UI.php

@@ -350,6 +350,8 @@ switch($operation)
 			$oP->add_linked_script("../js/linkswidget.js");
 			$oP->add_linked_script("../js/linkswidget.js");
 			$oP->add_linked_script("../js/jquery.blockUI.js");
 			$oP->add_linked_script("../js/jquery.blockUI.js");
 			$oWizard = new UIWizard($oP, $sClass, $sStateCode);
 			$oWizard = new UIWizard($oP, $sClass, $sStateCode);
+			$oContext = new UserContext();
+			$aArgs = array_merge($oAppContext->GetAsHash(), utils::ReadParam('default', array()));
 			$sStateCode = $oWizard->GetTargetState(); // Will computes the default state if none was supplied
 			$sStateCode = $oWizard->GetTargetState(); // Will computes the default state if none was supplied
 			$sClassLabel = MetaModel::GetName($sClass);
 			$sClassLabel = MetaModel::GetName($sClass);
 			$oP->add("<h2>Creation of a new $sClassLabel</h2>");
 			$oP->add("<h2>Creation of a new $sClassLabel</h2>");
@@ -367,21 +369,19 @@ switch($operation)
 			foreach($aWizardSteps['mandatory'] as $aSteps)
 			foreach($aWizardSteps['mandatory'] as $aSteps)
 			{
 			{
 				$oP->SetCurrentTab("Step $iStepIndex *");
 				$oP->SetCurrentTab("Step $iStepIndex *");
-				$oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap);
+				$oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, false /* no finish button */, $aArgs);
 				//$oP->add("</div>\n");
 				//$oP->add("</div>\n");
 				$iStepIndex++;
 				$iStepIndex++;
 			}	
 			}	
 			foreach($aWizardSteps['optional'] as $aSteps)
 			foreach($aWizardSteps['optional'] as $aSteps)
 			{
 			{
 				$oP->SetCurrentTab("Step $iStepIndex *");
 				$oP->SetCurrentTab("Step $iStepIndex *");
-				$oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, true); // true means enable the finish button
+				$oWizard->DisplayWizardStep($aSteps, $iStepIndex, $iMaxInputId, $aFieldsMap, true, $aArgs); // true means enable the finish button
 				//$oP->add("</div>\n");
 				//$oP->add("</div>\n");
 				$iStepIndex++;
 				$iStepIndex++;
 			}
 			}
 			$oWizard->DisplayFinalStep($iStepIndex, $aFieldsMap);	
 			$oWizard->DisplayFinalStep($iStepIndex, $aFieldsMap);	
 			
 			
-			$oAppContext = new ApplicationContext();
-			$oContext = new UserContext();
 			$oObj = null;
 			$oObj = null;
 			if (!empty($id))
 			if (!empty($id))
 			{
 			{