BUG 10875 The saveForm Javascript function runs "Before Assignment" triggers as long as the form passes validation SOLVED

- We reused the file cases_SaveData.php file to save data by ajax
- We created a new file saveForm.php to save data by ajax
This commit is contained in:
Julio Cesar Laura
2013-02-22 16:25:34 -04:00
parent e5a8ccfc32
commit ab3036f023
3 changed files with 147 additions and 5 deletions

View File

@@ -3255,14 +3255,16 @@ var saveAndRefreshForm = function(oObject) {
* refreshed or submited, at least not in the traditional sense.
**/
var saveForm = function(oObject) {
var saveForm = function(oObject, actionParameter) {
if (oObject) {
ajax_post(oObject.form.action,oObject.form,'POST');
var actionUrl = actionParameter || oObject.form.action.replace('cases_SaveData', 'saveForm');
ajax_post(actionUrl, oObject.form, 'POST');
}
else {
var oAux = window.document.getElementsByTagName('form');
if (oAux.length > 0) {
ajax_post(oAux[0].action,oAux[0],'POST');
var actionUrl = actionParameter || oAux[0].action.replace('cases_SaveData', 'saveForm');
ajax_post(actionUrl, oAux[0], 'POST');
}
}
};

View File

@@ -1225,8 +1225,8 @@ else{var arrayForm=document.getElementsByTagName("form");var inputAux;var id="";
var arrayLink=frm.getElementsByTagName("a");for(i2=0;i2<=arrayLink.length-1;i2++){var link=arrayLink[i2];if(typeof link.id!="undefined"&&link.id!=""&&link.id!="form[DYN_BACKWARD]"&&link.id!="form[DYN_FORWARD]"){var strHtml=link.parentNode.innerHTML;strHtml=stringReplace("\\x0A","",strHtml);strHtml=stringReplace("\\x0D","",strHtml);strHtml=stringReplace("\\x09","",strHtml);if(/^.*pm:field.*$/.test(strHtml)){id=link.id+"_";if(!document.getElementById(id)){var strAux=link.id.replace("form[","");strAux=strAux.substring(0,strAux.length-1);inputAux=document.createElement("input");inputAux.type="hidden";inputAux.id=id;inputAux.name=link.id;inputAux.value=link.href;frm.appendChild(inputAux);inputAux=document.createElement("input");inputAux.type="hidden";inputAux.id=id+"label";inputAux.name="form["+strAux+"_label]";inputAux.value=link.innerHTML;frm.appendChild(inputAux);}}}}}
return true;}};var getObject=function(sObject){var i;var oAux=null;var iLength=__aObjects__.length;for(i=0;i<iLength;i++){oAux=__aObjects__[i].getElementByName(sObject);if(oAux){return oAux;}}
return oAux;};var saveAndRefreshForm=function(oObject){if(oObject){oObject.form.action+='&_REFRESH_=1';oObject.form.submit();}
else{var oAux=window.document.getElementsByTagName('form');if(oAux.length>0){oAux[0].action+='&_REFRESH_=1';oAux[0].submit();}}};var saveForm=function(oObject){if(oObject){ajax_post(oObject.form.action,oObject.form,'POST');}
else{var oAux=window.document.getElementsByTagName('form');if(oAux.length>0){ajax_post(oAux[0].action,oAux[0],'POST');}}};var validateURL=function(url){var regexp=/http?s?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;if(regexp.test(url)){return true;}else{return false;}};var saveAndRedirectForm=function(oObject,oLocation){saveForm(oObject);if(validateURL(oLocation)){if(typeof(parent)!="undefined"){parent.location.href=oLocation;}else{document.location.href=oLocation;}}};var removeRequiredById=function(sFieldName){if(!notValidateThisFields.inArray(sFieldName)){notValidateThisFields.push(sFieldName);var oAux=document.getElementById('__notValidateThisFields__');if(oAux){oAux.value=notValidateThisFields.toJSONString();}}};var enableRequiredById=function(sFieldName){if(notValidateThisFields.inArray(sFieldName)){var i;var aAux=[];for(i=0;i<notValidateThisFields.length;i++){if(notValidateThisFields[i]!=sFieldName){aAux.push(notValidateThisFields[i]);}}
else{var oAux=window.document.getElementsByTagName('form');if(oAux.length>0){oAux[0].action+='&_REFRESH_=1';oAux[0].submit();}}};var saveForm=function(oObject,actionParameter){if(oObject){var actionUrl=actionParameter||oObject.form.action.replace('cases_SaveData','saveForm');ajax_post(actionUrl,oObject.form,'POST');}
else{var oAux=window.document.getElementsByTagName('form');if(oAux.length>0){var actionUrl=actionParameter||oAux[0].action.replace('cases_SaveData','saveForm');ajax_post(actionUrl,oAux[0],'POST');}}};var validateURL=function(url){var regexp=/http?s?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;if(regexp.test(url)){return true;}else{return false;}};var saveAndRedirectForm=function(oObject,oLocation){saveForm(oObject);if(validateURL(oLocation)){if(typeof(parent)!="undefined"){parent.location.href=oLocation;}else{document.location.href=oLocation;}}};var removeRequiredById=function(sFieldName){if(!notValidateThisFields.inArray(sFieldName)){notValidateThisFields.push(sFieldName);var oAux=document.getElementById('__notValidateThisFields__');if(oAux){oAux.value=notValidateThisFields.toJSONString();}}};var enableRequiredById=function(sFieldName){if(notValidateThisFields.inArray(sFieldName)){var i;var aAux=[];for(i=0;i<notValidateThisFields.length;i++){if(notValidateThisFields[i]!=sFieldName){aAux.push(notValidateThisFields[i]);}}
notValidateThisFields=aAux;var oAux=document.getElementById('__notValidateThisFields__');if(oAux){oAux.value=notValidateThisFields.toJSONString();}}};function dynaformVerifyFieldName(){pme_validating=true;setTimeout('verifyFieldName1();',0);return true;}
function verifyFieldName1(){if(getField('PME_VALIDATE_NAME').value=='__error_session__'){showPrompt('refreshDynaformEditor');return;}
verifyFieldNameFunction();}

View File

@@ -0,0 +1,140 @@
<?php
/**
* saveForm.php
*
* ProcessMaker Open Source Edition
* Copyright (C) 2004 - 2013 Colosa Inc.23
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
* Coral Gables, FL, 33134, USA, or email info@colosa.com.
*/
try {
if ($_GET['APP_UID'] !== $_SESSION['APPLICATION']) {
die( G::LoadTranslation( 'ID_INVALID_APPLICATION_ID_MSG', array ('<a href=\'' . $_SERVER['HTTP_REFERER'] . '\'>{1}</a>',G::LoadTranslation( 'ID_REOPEN' )
) ) );
}
$oForm = new Form( $_SESSION["PROCESS"] . "/" . $_GET["UID"], PATH_DYNAFORM );
$oForm->validatePost();
//Includes
G::LoadClass( "case" );
//Load the variables
$oCase = new Cases();
$oCase->thisIsTheCurrentUser( $_SESSION["APPLICATION"], $_SESSION["INDEX"], $_SESSION["USER_LOGGED"], "REDIRECT", "casesListExtJs" );
$Fields = $oCase->loadCase( $_SESSION["APPLICATION"] );
$Fields["APP_DATA"] = array_merge( $Fields["APP_DATA"], G::getSystemConstants() );
$Fields["APP_DATA"] = array_merge( $Fields["APP_DATA"], $_POST["form"] );
//save data in PM Tables if necessary
$newValues = array ();
foreach ($_POST['form'] as $sField => $sAux) {
if (isset( $oForm->fields[$sField]->pmconnection ) && isset( $oForm->fields[$sField]->pmfield )) {
if (($oForm->fields[$sField]->pmconnection != '') && ($oForm->fields[$sField]->pmfield != '')) {
if (isset( $oForm->fields[$oForm->fields[$sField]->pmconnection] )) {
require_once PATH_CORE . 'classes' . PATH_SEP . 'model' . PATH_SEP . 'AdditionalTables.php';
$oAdditionalTables = new AdditionalTables();
try {
$aData = $oAdditionalTables->load( $oForm->fields[$oForm->fields[$sField]->pmconnection]->pmtable, true );
} catch (Exception $oError) {
$aData = array ('FIELDS' => array ()
);
}
$aKeys = array ();
$aAux = explode( '|', $oForm->fields[$oForm->fields[$sField]->pmconnection]->keys );
$i = 0;
$aValues = array ();
foreach ($aData['FIELDS'] as $aField) {
if ($aField['FLD_KEY'] == '1') {
$aKeys[$aField['FLD_NAME']] = (isset( $aAux[$i] ) ? G::replaceDataField( $aAux[$i], $Fields['APP_DATA'] ) : '');
$i ++;
}
if ($aField['FLD_NAME'] == $oForm->fields[$sField]->pmfield) {
$aValues[$aField['FLD_NAME']] = $Fields['APP_DATA'][$sField];
} else {
$aValues[$aField['FLD_NAME']] = '';
}
}
try {
$aRow = $oAdditionalTables->getDataTable( $oForm->fields[$oForm->fields[$sField]->pmconnection]->pmtable, $aKeys );
} catch (Exception $oError) {
$aRow = false;
}
if ($aRow) {
foreach ($aValues as $sKey => $sValue) {
if ($sKey != $oForm->fields[$sField]->pmfield) {
$aValues[$sKey] = $aRow[$sKey];
}
}
try {
$oAdditionalTables->updateDataInTable( $oForm->fields[$oForm->fields[$sField]->pmconnection]->pmtable, $aValues );
} catch (Exception $oError) {
//Nothing
}
} else {
try {
// assembling the field list in order to save the data ina new record of a pm table
if (empty( $newValues )) {
$newValues = $aValues;
} else {
foreach ($aValues as $aValueKey => $aValueCont) {
if (trim( $newValues[$aValueKey] ) == '') {
$newValues[$aValueKey] = $aValueCont;
}
}
}
//$oAdditionalTables->saveDataInTable ( $oForm->fields [$oForm->fields [$sField]->pmconnection]->pmtable, $aValues );
} catch (Exception $oError) {
//Nothing
}
}
}
}
}
}
//save data
$aData = array ();
$aData['APP_NUMBER'] = $Fields['APP_NUMBER'];
$aData['APP_PROC_STATUS'] = $Fields['APP_PROC_STATUS'];
$aData['APP_DATA'] = $Fields['APP_DATA'];
$aData['DEL_INDEX'] = $_SESSION['INDEX'];
$aData['TAS_UID'] = $_SESSION['TASK'];
$aData['CURRENT_DYNAFORM'] = $_GET['UID'];
$aData['USER_UID'] = $_SESSION['USER_LOGGED'];
$aData['APP_STATUS'] = $Fields['APP_STATUS'];
$aData['PRO_UID'] = $_SESSION['PROCESS'];
$oCase->updateCase( $_SESSION['APPLICATION'], $aData );
// saving the data ina pm table in case that is a new record
if (! empty( $newValues )) {
$id = key( $newValues );
if (! $oAdditionalTables->updateDataInTable( $oForm->fields[$oForm->fields[$id]->pmconnection]->pmtable, $newValues )) {
//<--This is to know if it is a new registry on the PM Table
$oAdditionalTables->saveDataInTable( $oForm->fields[$oForm->fields[$id]->pmconnection]->pmtable, $newValues );
}
}
die('OK');
} catch (Exception $e) {
die($e->getMessage());
}