2011-04-25 16:29:38 -04:00
< ? php
2011-01-19 16:46:33 +00:00
/**
* cases_SaveData . php
*
* ProcessMaker Open Source Edition
* Copyright ( C ) 2004 - 2008 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 .
*
2010-12-02 23:34:41 +00:00
*/
2011-01-19 16:46:33 +00:00
//validate the data post
2010-12-02 23:34:41 +00:00
2011-11-23 19:10:25 -04:00
try {
2010-12-02 23:34:41 +00:00
2011-11-23 19:10:25 -04:00
if ( $_GET [ 'APP_UID' ] !== $_SESSION [ 'APPLICATION' ]) {
throw new Exception ( G :: LoadTranslation ( 'ID_INVALID_APPLICATION_ID_MSG' , array ( '<a href=\'' . $_SERVER [ 'HTTP_REFERER' ] . '\'>{1}</a>' , G :: LoadTranslation ( 'ID_REOPEN' ))));
}
2010-12-02 23:34:41 +00:00
2011-11-23 19:10:25 -04:00
$oForm = new Form ( $_SESSION [ 'PROCESS' ] . '/' . $_GET [ 'UID' ], PATH_DYNAFORM );
$oForm -> validatePost ();
2010-12-02 23:34:41 +00:00
2011-11-23 19:10:25 -04:00
/* Includes */
G :: LoadClass ( 'case' );
2010-12-02 23:34:41 +00:00
2011-11-23 19:10:25 -04:00
//load the variables
$oCase = new Cases ( );
$oCase -> thisIsTheCurrentUser ( $_SESSION [ 'APPLICATION' ], $_SESSION [ 'INDEX' ], $_SESSION [ 'USER_LOGGED' ], 'REDIRECT' , 'cases_List' );
$Fields = $oCase -> loadCase ( $_SESSION [ 'APPLICATION' ] );
$Fields [ 'APP_DATA' ] = array_merge ( $Fields [ 'APP_DATA' ], G :: getSystemConstants () );
$Fields [ 'APP_DATA' ] = array_merge ( $Fields [ 'APP_DATA' ], ( array ) $_POST [ 'form' ] );
2010-12-02 23:34:41 +00:00
2011-11-23 19:10:25 -04:00
#here we must verify if is a debug session
$trigger_debug_session = $_SESSION [ 'TRIGGER_DEBUG' ] [ 'ISSET' ]; #here we must verify if is a debugg session
2010-12-02 23:34:41 +00:00
2011-11-23 19:10:25 -04:00
#trigger debug routines...
2010-12-02 23:34:41 +00:00
2011-04-28 12:06:16 -04:00
2011-11-23 19:10:25 -04:00
//cleaning debug variables
$_SESSION [ 'TRIGGER_DEBUG' ] [ 'ERRORS' ] = Array ();
$_SESSION [ 'TRIGGER_DEBUG' ] [ 'DATA' ] = Array ();
$_SESSION [ 'TRIGGER_DEBUG' ] [ 'TRIGGERS_NAMES' ] = Array ();
$_SESSION [ 'TRIGGER_DEBUG' ] [ 'TRIGGERS_VALUES' ] = Array ();
2010-12-02 23:34:41 +00:00
2011-11-23 19:10:25 -04:00
$triggers = $oCase -> loadTriggers ( $_SESSION [ 'TASK' ], 'DYNAFORM' , $_GET [ 'UID' ], 'AFTER' );
$_SESSION [ 'TRIGGER_DEBUG' ] [ 'NUM_TRIGGERS' ] = count ( $triggers );
$_SESSION [ 'TRIGGER_DEBUG' ] [ 'TIME' ] = 'AFTER' ;
if ( $_SESSION [ 'TRIGGER_DEBUG' ] [ 'NUM_TRIGGERS' ] != 0 ) {
$_SESSION [ 'TRIGGER_DEBUG' ] [ 'TRIGGERS_NAMES' ] = $oCase -> getTriggerNames ( $triggers );
$_SESSION [ 'TRIGGER_DEBUG' ] [ 'TRIGGERS_VALUES' ] = $triggers ;
}
if ( $_SESSION [ 'TRIGGER_DEBUG' ] [ 'NUM_TRIGGERS' ] != 0 ) {
//Execute after triggers - Start
$Fields [ 'APP_DATA' ] = $oCase -> ExecuteTriggers ( $_SESSION [ 'TASK' ], 'DYNAFORM' , $_GET [ 'UID' ], 'AFTER' , $Fields [ 'APP_DATA' ] );
//Execute after triggers - End
}
//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
}
}
}
}
}
}
// saving the data ina pm table in case that is a new record
2012-02-22 15:56:11 -04:00
if ( ! empty ( $newValues )) {
$id = key ( $newValues );
while ( strlen ( $id ) < 3 ) {
$id = '_' . $id ;
}
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 );
}
2011-11-23 19:10:25 -04:00
}
//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 );
//save files
require_once 'classes/model/AppDocument.php' ;
if ( isset ( $_FILES [ 'form' ] )) {
foreach ( $_FILES [ 'form' ] [ 'name' ] as $sFieldName => $vValue ) {
if ( $_FILES [ 'form' ] [ 'error' ] [ $sFieldName ] == 0 ) {
2012-04-23 19:08:09 -04:00
$oAppDocument = new AppDocument ();
2011-11-23 19:10:25 -04:00
if ( isset ( $_POST [ 'INPUTS' ] [ $sFieldName ] ) && $_POST [ 'INPUTS' ] [ $sFieldName ] != '' ) {
require_once ( 'classes/model/AppFolder.php' );
2010-12-02 23:34:41 +00:00
require_once ( 'classes/model/InputDocument.php' );
2011-11-23 19:10:25 -04:00
$oInputDocument = new InputDocument ();
2010-12-02 23:34:41 +00:00
$aID = $oInputDocument -> load ( $_POST [ 'INPUTS' ] [ $sFieldName ]);
2011-11-23 19:10:25 -04:00
//Get the Custom Folder ID (create if necessary)
2010-12-02 23:34:41 +00:00
$oFolder = new AppFolder ();
$folderId = $oFolder -> createFromPath ( $aID [ 'INP_DOC_DESTINATION_PATH' ]);
//Tags
$fileTags = $oFolder -> parseTags ( $aID [ 'INP_DOC_TAGS' ]);
2011-11-23 19:10:25 -04:00
$aFields = array (
'APP_UID' => $_SESSION [ 'APPLICATION' ],
'DEL_INDEX' => $_SESSION [ 'INDEX' ],
'USR_UID' => $_SESSION [ 'USER_LOGGED' ],
'DOC_UID' => $_POST [ 'INPUTS' ] [ $sFieldName ],
'APP_DOC_TYPE' => 'INPUT' ,
'APP_DOC_CREATE_DATE' => date ( 'Y-m-d H:i:s' ),
'APP_DOC_COMMENT' => '' ,
'APP_DOC_TITLE' => '' ,
'APP_DOC_FILENAME' => $_FILES [ 'form' ] [ 'name' ] [ $sFieldName ],
'FOLDER_UID' => $folderId ,
2011-02-22 23:10:43 +00:00
'APP_DOC_TAGS' => $fileTags
2011-11-23 19:10:25 -04:00
);
}
else {
$aFields = array (
'APP_UID' => $_SESSION [ 'APPLICATION' ],
'DEL_INDEX' => $_SESSION [ 'INDEX' ],
'USR_UID' => $_SESSION [ 'USER_LOGGED' ],
2012-04-23 19:08:09 -04:00
'DOC_UID' => - 1 ,
2011-11-23 19:10:25 -04:00
'APP_DOC_TYPE' => 'ATTACHED' ,
'APP_DOC_CREATE_DATE' => date ( 'Y-m-d H:i:s' ),
'APP_DOC_COMMENT' => '' ,
'APP_DOC_TITLE' => '' ,
'APP_DOC_FILENAME' => $_FILES [ 'form' ] [ 'name' ] [ $sFieldName ]
);
}
2012-04-23 19:08:09 -04:00
$oAppDocument -> create ( $aFields );
2011-11-23 19:10:25 -04:00
$iDocVersion = $oAppDocument -> getDocVersion ();
2012-04-23 19:08:09 -04:00
$sAppDocUid = $oAppDocument -> getAppDocUid ();
$aInfo = pathinfo ( $oAppDocument -> getAppDocFilename ());
2011-11-23 19:10:25 -04:00
$sExtension = ( isset ( $aInfo [ 'extension' ] ) ? $aInfo [ 'extension' ] : '' );
$sPathName = PATH_DOCUMENT . $_SESSION [ 'APPLICATION' ] . PATH_SEP ;
$sFileName = $sAppDocUid . '_' . $iDocVersion . '.' . $sExtension ;
G :: uploadFile ( $_FILES [ 'form' ] [ 'tmp_name' ] [ $sFieldName ], $sPathName , $sFileName );
2012-04-23 19:08:09 -04:00
2011-11-23 19:10:25 -04:00
//Plugin Hook PM_UPLOAD_DOCUMENT for upload document
2012-04-23 19:08:09 -04:00
$oPluginRegistry = & PMPluginRegistry :: getSingleton ();
if ( $oPluginRegistry -> existsTrigger ( PM_UPLOAD_DOCUMENT ) && class_exists ( " uploadDocumentData " )) {
$triggerDetail = $oPluginRegistry -> getTriggerInfo ( PM_UPLOAD_DOCUMENT );
$documentData = new uploadDocumentData ( $_SESSION [ " APPLICATION " ], $_SESSION [ " USER_LOGGED " ], $sPathName . $sFileName , $aFields [ " APP_DOC_FILENAME " ], $sAppDocUid , $iDocVersion );
$uploadReturn = $oPluginRegistry -> executeTriggers ( PM_UPLOAD_DOCUMENT , $documentData );
if ( $uploadReturn ) {
$aFields [ " APP_DOC_PLUGIN " ] = $triggerDetail -> sNamespace ;
if ( ! isset ( $aFields [ " APP_DOC_UID " ])) {
$aFields [ " APP_DOC_UID " ] = $sAppDocUid ;
}
if ( ! isset ( $aFields [ " DOC_VERSION " ])) {
$aFields [ " DOC_VERSION " ] = $iDocVersion ;
}
$oAppDocument -> update ( $aFields );
unlink ( $sPathName . $sFileName );
2011-11-23 19:10:25 -04:00
}
}
}
}
}
//go to the next step
$aNextStep = $oCase -> getNextStep ( $_SESSION [ 'PROCESS' ], $_SESSION [ 'APPLICATION' ], $_SESSION [ 'INDEX' ], $_SESSION [ 'STEP_POSITION' ] );
if ( isset ( $_GET [ '_REFRESH_' ] )) {
G :: header ( 'location: ' . $_SERVER [ 'HTTP_REFERER' ] );
die ();
}
2010-12-02 23:34:41 +00:00
2012-01-26 09:33:01 -04:00
$_SESSION [ 'STEP_POSITION' ] = $aNextStep [ 'POSITION' ];
2011-11-23 19:10:25 -04:00
$_SESSION [ 'BREAKSTEP' ][ 'NEXT_STEP' ] = $aNextStep [ 'PAGE' ];
2012-01-26 09:33:01 -04:00
$debuggerAvailable = true ;
2012-01-31 18:45:30 -04:00
if ( isset ( $_SESSION [ 'current_ux' ]) && $_SESSION [ 'current_ux' ] == 'SIMPLIFIED' ) {
2012-01-26 09:33:01 -04:00
$debuggerAvailable = false ;
}
2010-12-02 23:34:41 +00:00
2012-01-26 09:33:01 -04:00
if ( $trigger_debug_session && $debuggerAvailable ) {
2011-11-23 19:10:25 -04:00
$_SESSION [ 'TRIGGER_DEBUG' ] [ 'BREAKPAGE' ] = $aNextStep [ 'PAGE' ];
$aNextStep [ 'PAGE' ] = $aNextStep [ 'PAGE' ] . '&breakpoint=triggerdebug' ;
}
2010-12-02 23:34:41 +00:00
2011-11-23 19:10:25 -04:00
$oForm -> validatePost ();
$oJSON = new Services_JSON ( );
$_POST [ '__notValidateThisFields__' ] = ( isset ( $_POST [ '__notValidateThisFields__' ]) && $_POST [ '__notValidateThisFields__' ] != '' ) ? $_POST [ '__notValidateThisFields__' ] : $_POST [ 'DynaformRequiredFields' ];
if ( $missing_req_values = $oForm -> validateRequiredFields ( $_POST [ 'form' ], $oJSON -> decode ( stripslashes ( $_POST [ '__notValidateThisFields__' ] ) ) )) {
$_POST [ 'next_step' ] = $aNextStep ;
$_POST [ 'previous_step' ] = $oCase -> getPreviousStep ( $_SESSION [ 'PROCESS' ], $_SESSION [ 'APPLICATION' ], $_SESSION [ 'INDEX' ], $_SESSION [ 'STEP_POSITION' ] );
$_POST [ 'req_val' ] = $missing_req_values ;
$G_PUBLISH = new Publisher ( );
$G_PUBLISH -> AddContent ( 'view' , 'cases/missRequiredFields' );
G :: RenderPage ( 'publish' , 'blank' );
exit ( 0 );
}
2010-12-02 23:34:41 +00:00
2011-11-23 19:10:25 -04:00
G :: header ( 'location: ' . $aNextStep [ 'PAGE' ] );
}
2012-04-23 19:08:09 -04:00
catch ( Exception $e ) {
2011-11-23 19:10:25 -04:00
$G_PUBLISH = new Publisher ;
$aMessage = array ();
$aMessage [ 'MESSAGE' ] = $e -> getMessage ();
$G_PUBLISH -> AddContent ( 'xmlform' , 'xmlform' , 'login/showMessage' , '' , $aMessage );
G :: RenderPage ( 'publish' , 'blank' );
die ;
}