diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 2d79b41c4..e8a2acacc 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -1564,7 +1564,8 @@ EOF } $iTransactionId = utils::GetNewTransactionId(); $oPage->SetTransactionId($iTransactionId); - $oPage->add("
m_iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return CheckFields('form_{$this->m_iFormId}', true)\">\n"); + $oPage->add("m_iFormId}\" enctype=\"multipart/form-data\" method=\"post\" onSubmit=\"return OnSubmit('form_{$this->m_iFormId}');\">\n"); + $oPage->add_ready_script("$(window).unload(function() { OnUnload('$iTransactionId') } );\n"); $oPage->AddTabContainer(OBJECT_PROPERTIES_TAB, $sPrefix); $oPage->SetCurrentTabContainer(OBJECT_PROPERTIES_TAB); diff --git a/js/forms-json-utils.js b/js/forms-json-utils.js index ed394311b..07330229f 100644 --- a/js/forms-json-utils.js +++ b/js/forms-json-utils.js @@ -10,6 +10,8 @@ var oObj = {}; // of Id 'att_2' in the form var aFieldsMap = new Array; +window.bInSubmit = false; // For handling form cancellation via OnBeforeUnload events + // Update the whole object from the form and also update its // JSON (serialized) representation in the (hidden) field function UpdateObjectFromForm(aFieldsMap, oObj) @@ -116,6 +118,28 @@ function ActivateStep(iTargetStep) // ); // } //} +function OnUnload(sTransactionId) +{ + if (!window.bInSubmit) + { + // If it's not a submit, then it's a "cancel" (Pressing the Cancel button, closing the window, using the back button...) + $.post('../pages/ajax.render.php', {operation: 'on_form_cancel', transaction_id: sTransactionId }, function() + { + // Do nothing for now... + }); + } +} + +function OnSubmit(sFormId) +{ + window.bInSubmit=true; // This is a submit, make sure that when the page gets unloaded we don't cancel the action + var bResult = CheckFields(sFormId, true); + if (!bResult) + { + window.bInSubmit = false; // Submit is/will be canceled + } + return bResult; +} // Store the result of the form validation... there may be several forms per page, beware var oFormErrors = { err_form0: 0 }; @@ -129,7 +153,7 @@ function CheckFields(sFormId, bDisplayAlert) // The two 'fields' below will be updated when the 'validate' event is processed oFormErrors['err_'+sFormId] = 0; // Number of errors encountered when validating the form oFormErrors['input_'+sFormId] = null; // First 'input' with an error, to set the focus to it - $('#'+sFormId+' :input').each( function() + $('#'+sFormId+' :input[type!=hidden]').each( function() { validateEventResult = $(this).trigger('validate', sFormId); } diff --git a/pages/UI.php b/pages/UI.php index 242a6467c..497177ea8 100644 --- a/pages/UI.php +++ b/pages/UI.php @@ -1474,7 +1474,7 @@ EOF $oObj->DisplayBareProperties($oP); $oP->add(''); $oP->add("
\n"); - $oP->add("\n"); + $oP->add("\n"); $oP->add("
\n"); $oP->details($aDetails); $oP->add("
\n"); @@ -1685,7 +1685,7 @@ EOF $aExpectedAttributes = $aTargetState['attribute_list']; $oP->add("

$sActionDetails

\n"); $oP->add("
\n"); - $oP->add("\n"); + $oP->add("\n"); $aDetails = array(); $iFieldIndex = 0; $aFieldsMap = array(); diff --git a/pages/ajax.render.php b/pages/ajax.render.php index 0e46e0144..9536624ae 100644 --- a/pages/ajax.render.php +++ b/pages/ajax.render.php @@ -485,7 +485,19 @@ try // Can be useful in case a user got some corrupted prefs... appUserPreferences::ClearPreferences(); break; - + + case 'on_form_cancel': + // Called when a creation/modification form is cancelled by the end-user + // Let's take this opportunity to inform the plug-ins so that they can perform some cleanup + $iTransactionId = utils::ReadParam('transaction_id', 0); + $sTempId = session_id().'_'.$iTransactionId; + foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance) + { + $oExtensionInstance->OnFormCancel($sTempId); + } + + break; + default: $oPage->p("Invalid query."); } diff --git a/portal/index.php b/portal/index.php index c3110afeb..caca1e2f7 100644 --- a/portal/index.php +++ b/portal/index.php @@ -283,7 +283,7 @@ function RequestCreationForm($oP, $oUserOrg) // Starts the validation when the page is ready CheckFields('request_form', false); $('#request_form').submit( function() { - return CheckFields('request_form', true); + return OnSubmit('request_form'); }); EOF ); @@ -745,7 +745,7 @@ function DisplayResolvedRequestForm($oP, UserRequest $oRequest) // Starts the validation when the page is ready CheckFields('request_form', false); $('#request_form').submit( function() { - return CheckFields('request_form', true); + return OnSubmit('request_form'); }); EOF );