From e2d60e7c8f5a2b639b1db3e3f1f9354974864c5b Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Fri, 11 Sep 2009 20:35:16 +0000 Subject: [PATCH] Implementation of AllowedValues for fields that depend on other fields SVN:trunk[148] --- application/cmdbabstract.class.inc.php | 12 +++++----- application/uiwizard.class.inc.php | 1 + application/wizardhelper.class.inc.php | 12 +++++++++- js/forms-json-utils.js | 11 ++++++++- js/wizardhelper.js | 31 +++++++++++++++++++++++++- pages/UI.php | 2 ++ pages/ajax.render.php | 22 ++++++++---------- 7 files changed, 69 insertions(+), 22 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 10664f92d..146f88e42 100644 --- a/application/cmdbabstract.class.inc.php +++ b/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 = ""; + $sHTMLValue = ""; 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 = ""; + $sHTMLValue = ""; } else if (count($aAllowedValues) > 50) { // too many choices, use an autocomplete // The input for the auto complete - $sHTMLValue = ""; + $sHTMLValue = ""; // another hidden input to store & pass the object's Id $sHTMLValue .= "\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 = ""; + $sHTMLValue = ""; } break; } diff --git a/application/uiwizard.class.inc.php b/application/uiwizard.class.inc.php index 886b8fd39..f49228dbc 100644 --- a/application/uiwizard.class.inc.php +++ b/application/uiwizard.class.inc.php @@ -89,6 +89,7 @@ class UIWizard function OnEnterStep{$iStepIndex}() { oWizardHelper.ResetQuery(); + oWizardHelper.UpdateWizard(); $sJSHandlerCode diff --git a/application/wizardhelper.class.inc.php b/application/wizardhelper.class.inc.php index 3ab7b5c1c..578932e18 100644 --- a/application/wizardhelper.class.inc.php +++ b/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 diff --git a/js/forms-json-utils.js b/js/forms-json-utils.js index 1c6b5b1e3..60754bd07 100644 --- a/js/forms-json-utils.js +++ b/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); diff --git a/js/wizardhelper.js b/js/wizardhelper.js index e1b50bcba..89377316b 100644 --- a/js/wizardhelper.js +++ b/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){ diff --git a/pages/UI.php b/pages/UI.php index ec6136aac..0de789f0d 100644 --- a/pages/UI.php +++ b/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); \n"); diff --git a/pages/ajax.render.php b/pages/ajax.render.php index 9d4365f76..b5e892a44 100644 --- a/pages/ajax.render.php +++ b/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 = "\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());