Explorar el Código

Implementation of AllowedValues for fields that depend on other fields

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@148 a333f486-631f-4898-b8df-5754b55c2be0
dflaven hace 15 años
padre
commit
56768a0572

+ 6 - 6
application/cmdbabstract.class.inc.php

@@ -601,7 +601,7 @@ abstract class cmdbAbstractObject extends CMDBObject
 		return $sHtml;
 	}
 	
-	public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0)
+	public static function GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '', $iFlags = 0, $aArgs = array())
 	{
 		static $iInputId = 0;
 		if (!empty($iId))
@@ -630,7 +630,7 @@ abstract class cmdbAbstractObject extends CMDBObject
 				break;
 				
 				case 'Password':
-				$sHTMLValue = "<input type=\"password\" size=\"20\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
+				$sHTMLValue = "<input type=\"password\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}/>";
 				break;
 				
 				case 'Text':
@@ -645,19 +645,19 @@ abstract class cmdbAbstractObject extends CMDBObject
 				case 'String':
 				default:
 					// #@# todo - add context information (depending on dimensions)
-					$aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, array());
+					$aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs);
 					if ($aAllowedValues !== null)
 					{
 						//Enum field or external key, display a combo
 						if (count($aAllowedValues) == 0)
 						{
-							$sHTMLValue = "<input type=\"text\" size=\"70\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
+							$sHTMLValue = "<input type=\"text\" size=\"30\" value=\"\" name=\"attr_{$sAttCode}{$sNameSuffix}\" id=\"$iInputId\"{$sCSSClasses}/>";
 						}
 						else if (count($aAllowedValues) > 50)
 						{
 							// too many choices, use an autocomplete
 							// The input for the auto complete
-							$sHTMLValue = "<input type=\"text\" id=\"label_$iInputId\" size=\"50\" name=\"\" value=\"$sDisplayValue\"{$sCSSClasses}/>";
+							$sHTMLValue = "<input type=\"text\" id=\"label_$iInputId\" size=\"30\" name=\"\" value=\"$sDisplayValue\"{$sCSSClasses}/>";
 							// another hidden input to store & pass the object's Id
 							$sHTMLValue .= "<input type=\"hidden\" id=\"$iInputId\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" />\n";
 							$oPage->add_ready_script("\$('#label_$iInputId').autocomplete('./ajax.render.php', { minChars:3, onItemSelect:selectItem, onFindValue:findValue, formatItem:formatItem, autoFill:true, keyHolder:'#$iInputId', extraParams:{operation:'autocomplete', sclass:'$sClass',attCode:'".$sAttCode."'}});");
@@ -678,7 +678,7 @@ abstract class cmdbAbstractObject extends CMDBObject
 					}
 					else
 					{
-						$sHTMLValue = "<input type=\"text\" size=\"50\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}>";
+						$sHTMLValue = "<input type=\"text\" size=\"30\" name=\"attr_{$sAttCode}{$sNameSuffix}\" value=\"$value\" id=\"$iInputId\"{$sCSSClasses}>";
 					}
 					break;
 			}

+ 1 - 0
application/uiwizard.class.inc.php

@@ -89,6 +89,7 @@ class UIWizard
 function OnEnterStep{$iStepIndex}()
 {
 	oWizardHelper.ResetQuery();
+	oWizardHelper.UpdateWizard();
 	
 $sJSHandlerCode
 

+ 11 - 1
application/wizardhelper.class.inc.php

@@ -60,7 +60,7 @@ class WizardHelper
 					$oSet = DBObjectSet::FromArray($sLinkedClass, $aLinkedObjectsArray);
 					$oObj->Set($sAttCode, $oSet);
 				}
-				else if (($oAttDef->IsExternalKey()) && ($value != '') )
+				else if (($oAttDef->IsExternalKey()) && (!empty($value)) )
 				{
 					// For external keys: load the target object so that external fields
 					// get filled too
@@ -176,6 +176,16 @@ class WizardHelper
 		return $aFields;
 	}
 	
+	public function GetTargetClass()
+	{
+		return $this->m_aData['m_sClass'];
+	}
+	
+	public function GetIdForField($sFieldName)
+	{
+		return $this->m_aData['m_oFieldsMap'][$sFieldName];
+	}
+	
 	static function ParseJsonSet($oMe, $sLinkClass, $sExtKeyToMe, $sJsonSet)
 	{
 		$aSet = json_decode($sJsonSet, true); // true means hash array instead of object

+ 10 - 1
js/forms-json-utils.js

@@ -57,7 +57,16 @@ function ReloadObjectFromServer(sJSON)
 function GoToStep(iCurrentStep, iNextStep)
 {
 	var oCurrentStep = document.getElementById('wizStep'+iCurrentStep);
-	if (CheckMandatoryFields('wizStep'+iCurrentStep))
+	if (iNextStep > iCurrentStep)
+	{
+		// Check the values when moving forward
+		if (CheckMandatoryFields('wizStep'+iCurrentStep))
+		{
+			oCurrentStep.style.display = 'none';
+			ActivateStep(iNextStep);
+		}
+	}
+	else
 	{
 		oCurrentStep.style.display = 'none';
 		ActivateStep(iNextStep);

+ 30 - 1
js/wizardhelper.js

@@ -9,6 +9,7 @@ function WizardHelper(sClass)
 					 'm_aAllowedValuesRequested': [],
 					 'm_aDefaultValue': [],
 					 'm_aAllowedValues': [],
+					 'm_iFieldsCount' : 0,
 					};
 	this.m_oData.m_sClass = sClass;
 	
@@ -18,6 +19,12 @@ function WizardHelper(sClass)
 		this.m_oData.m_oFieldsMap = oFieldsMap;
 		
 	}
+	this.SetFieldsCount = function (count)
+	{
+		this.m_oData.m_iFieldsCount = count;
+		
+	}
+
 	this.RequestDefaultValue = function (sFieldName)
 	{
 		currentValue = this.UpdateCurrentValue(sFieldName);
@@ -50,12 +57,20 @@ function WizardHelper(sClass)
 	this.ResetQuery = function ()
 	{
 		this.m_oData.m_aDefaultValueRequested = [];
+		this.m_oData.m_aDefaultValue = [];
 		this.m_oData.m_aAllowedValuesRequested = [];
+		this.m_oData.m_aAllowedValues = [];
 	}
 	
 	this.UpdateFields = function ()
 	{
 		//console.log('** UpdateFields **')
+		for(i=0; i< this.m_oData.m_aAllowedValuesRequested.length; i++)
+		{
+			sAttCode = this.m_oData.m_aAllowedValuesRequested[i];
+			sFieldId = this.m_oData.m_oFieldsMap[sAttCode];
+			$('#field_'+sFieldId).html(this.m_oData.m_aAllowedValues[sFieldId]);
+		}
 		for(i=0; i< this.m_oData.m_aDefaultValueRequested.length; i++)
 		{
 			sAttCode = this.m_oData.m_aDefaultValueRequested[i];
@@ -67,10 +82,24 @@ function WizardHelper(sClass)
 		}
 	}
 	
+	this.UpdateWizard = function ()
+	{
+		//console.log('** UpdateWizard **')
+		for(i=0; i< this.m_oData.m_iFieldsCount; i++)
+		{
+			value = $('#att_'+i).val();
+			if (value == '')
+			{
+				value = null;
+			}
+			this.m_oData.m_aCurrentValues[i] = value;
+		}
+	}
+	
 	this.AjaxQueryServer = function ()
 	{
 		//console.log('data sent:', this.ToJSON());
-		//console.log('oWizard:', this);
+		console.log('oWizard:', this);
 		$.get('ajax.render.php?json_obj=' + this.ToJSON(),
 		   { operation: 'wizard_helper' },
 			function(json_data){

+ 2 - 0
pages/UI.php

@@ -368,12 +368,14 @@ switch($operation)
 			{
 				$aNewFieldsMap[$sFieldCode] = $id;
 			}
+			$iFieldsCount = count($aFieldsMap);
 			$sJsonFieldsMap = json_encode($aNewFieldsMap);
 		
 			$oP->add("
 			// Initializes the object once at the beginning of the page...
 			var oWizardHelper = new WizardHelper('$sClass');
 			oWizardHelper.SetFieldsMap($sJsonFieldsMap);
+			oWizardHelper.SetFieldsCount($iFieldsCount);
 		
 			ActivateStep(1);
 			</script>\n");

+ 9 - 13
pages/ajax.render.php

@@ -77,25 +77,21 @@ switch($operation)
 	$sJson = utils::ReadParam('json_obj', '');
 	$oWizardHelper = WizardHelper::FromJSON($sJson);
 	$oObj = $oWizardHelper->GetTargetObject(); 
+	$sClass = $oWizardHelper->GetTargetClass();
 	foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
 	{
-		$oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
-		$oWizardHelper->SetDefaultValue($sAttCode, $oAttDef->GetDefaultValue());
+		$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
+		$defaultValue = $oAttDef->GetDefaultValue();
+		$oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
+		$oObj->Set($sAttCode, $defaultValue);
 	}
 	foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
 	{
-		 $aAllowedValues = MetaModel::GetAllowedValues_att(get_class($oObj), $sAttCode, array('this' => $oObj));
-		// Few choices, use a normal 'select'
-		$sHTMLValue = "<select name=\"attr_{$sAttCode}\"\n";
-		$sHTMLValue .= "<option value=\"0\">-- select one --</option>\n";
-		foreach($aAllowedValues as $key => $display_value)
-		{
-			$sSelected = ''; //($value == $key) ? ' selected' : '';
-			$sHTMLValue .= "<option value=\"$key\"$sSelected>$display_value</option>\n";
-		}
-		$sHTMLValue .= "</select>\n";
+		$sId = $oWizardHelper->GetIdForField($sAttCode);
+		$value = $oObj->Get($sAttCode);
+		$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
+		$sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, '', 'att_'.$sId, '', 0, array('this' => $oObj));
 
-		// Improvement: what if the list is too long?
 		$oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
 	}
 	$oPage->add($oWizardHelper->ToJSON());