File size: 9,186 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**

 * 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.

 */
//validate the data post

use ProcessMaker\Plugins\PluginRegistry;

$dynaForm = DynaformPeer::retrieveByPK($_GET["UID"]);

$flagDynaFormNewVersion = !is_null($dynaForm) && $dynaForm->getDynVersion() == 2;

//If no variables are submitted and the $_POST variable is empty
if (!isset($_POST['form'])) {
    $_POST['form'] = array();
}

if ($flagDynaFormNewVersion) {
    $dataForm = $_POST["form"];
}

$oForm = new Form( $_SESSION['PROCESS'] . '/' . $_GET['UID'], PATH_DYNAFORM );
$oForm->validatePost();

//load the variables
$oCase = new Cases();
$Fields = $oCase->loadCase( $_SESSION['APPLICATION'] );

if ($flagDynaFormNewVersion) {
    $Fields["APP_DATA"] = array_merge($Fields["APP_DATA"], $dataForm);
}

$Fields['APP_DATA'] = array_merge( $Fields['APP_DATA'], $_POST['form'] );

//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['PRO_UID'] = $Fields['PRO_UID'];
$aData['USER_UID'] = $_SESSION['USER_LOGGED'];
$aData['APP_STATUS'] = $Fields['APP_STATUS'];

//$aData = $oCase->loadCase( $_SESSION['APPLICATION'] );
$oCase->updateCase( $_SESSION['APPLICATION'], $aData );
G::SendTemporalMessage( 'ID_SAVED_SUCCESSFULLY', 'info' );

//Save files
if (isset( $_FILES["form"]["name"] ) && count( $_FILES["form"]["name"] ) > 0) {
	$arrayField = array ();
	$arrayFileName = array ();
	$arrayFileTmpName = array ();
	$arrayFileError = array ();
	$i = 0;

	foreach ($_FILES["form"]["name"] as $fieldIndex => $fieldValue) {
		if (is_array( $fieldValue )) {
			foreach ($fieldValue as $index => $value) {
				if (is_array( $value )) {
					foreach ($value as $grdFieldIndex => $grdFieldValue) {
						$arrayField[$i]["grdName"] = $fieldIndex;
						$arrayField[$i]["grdFieldName"] = $grdFieldIndex;
						$arrayField[$i]["index"] = $index;

						$arrayFileName[$i] = $_FILES["form"]["name"][$fieldIndex][$index][$grdFieldIndex];
						$arrayFileTmpName[$i] = $_FILES["form"]["tmp_name"][$fieldIndex][$index][$grdFieldIndex];
						$arrayFileError[$i] = $_FILES["form"]["error"][$fieldIndex][$index][$grdFieldIndex];
						$i = $i + 1;
					}
				}
			}
		} else {
			$arrayField[$i] = $fieldIndex;

			$arrayFileName[$i] = $_FILES["form"]["name"][$fieldIndex];
			$arrayFileTmpName[$i] = $_FILES["form"]["tmp_name"][$fieldIndex];
			$arrayFileError[$i] = $_FILES["form"]["error"][$fieldIndex];
			$i = $i + 1;
		}
	}
	if (count( $arrayField ) > 0) {
		for ($i = 0; $i <= count( $arrayField ) - 1; $i ++) {
			if ($arrayFileError[$i] == 0) {
				$indocUid = null;
				$fieldName = null;
				$fileSizeByField = 0;

				if (is_array( $arrayField[$i] )) {
					if (isset( $_POST["INPUTS"][$arrayField[$i]["grdName"]][$arrayField[$i]["grdFieldName"]] ) && ! empty( $_POST["INPUTS"][$arrayField[$i]["grdName"]][$arrayField[$i]["grdFieldName"]] )) {
						$indocUid = $_POST["INPUTS"][$arrayField[$i]["grdName"]][$arrayField[$i]["grdFieldName"]];
					}

					$fieldName = $arrayField[$i]["grdName"] . "_" . $arrayField[$i]["index"] . "_" . $arrayField[$i]["grdFieldName"];

					if (isset($_FILES["form"]["size"][$arrayField[$i]["grdName"]][$arrayField[$i]["index"]][$arrayField[$i]["grdFieldName"]])) {
						$fileSizeByField = $_FILES["form"]["size"][$arrayField[$i]["grdName"]][$arrayField[$i]["index"]][$arrayField[$i]["grdFieldName"]];
					}
				} else {
					if (isset( $_POST["INPUTS"][$arrayField[$i]] ) && ! empty( $_POST["INPUTS"][$arrayField[$i]] )) {
						$indocUid = $_POST["INPUTS"][$arrayField[$i]];
					}

					$fieldName = $arrayField[$i];

					if (isset($_FILES["form"]["size"][$fieldName])) {
						$fileSizeByField = $_FILES["form"]["size"][$fieldName];
					}
				}

				if ($indocUid != null) {

					$oInputDocument = new InputDocument();
					$aID = $oInputDocument->load( $indocUid );

					//Get the Custom Folder ID (create if necessary)
					$oFolder = new AppFolder();

					//***Validating the file allowed extensions***
					$res = G::verifyInputDocExtension($aID['INP_DOC_TYPE_FILE'], $arrayFileName[$i], $arrayFileTmpName[$i]);
					if($res->status == 0){
						$message = $res->message;
						G::SendMessageText( $message, "ERROR" );
						$backUrlObj = explode( "sys" . config("system.workspace"), $_SERVER['HTTP_REFERER'] );
						G::header( "location: " . "/sys" . config("system.workspace") . $backUrlObj[1] );
						die();
					}

					//--- Validate Filesize of $_FILE
					$inpDocMaxFilesize = $aID["INP_DOC_MAX_FILESIZE"];
					$inpDocMaxFilesizeUnit = $aID["INP_DOC_MAX_FILESIZE_UNIT"];

					$inpDocMaxFilesize = $inpDocMaxFilesize * (($inpDocMaxFilesizeUnit == "MB")? 1024 *1024 : 1024); //Bytes

					if ($inpDocMaxFilesize > 0 && $fileSizeByField > 0) {
						if ($fileSizeByField > $inpDocMaxFilesize) {
							G::SendMessageText(G::LoadTranslation("ID_SIZE_VERY_LARGE_PERMITTED"), "ERROR");
							$arrayAux1 = explode("sys" . config("system.workspace"), $_SERVER["HTTP_REFERER"]);
							G::header("location: /sys" . config("system.workspace") . $arrayAux1[1]);
							exit(0);
						}
					}

					$aFields = array ("APP_UID" => $_SESSION["APPLICATION"],"DEL_INDEX" => $_SESSION["INDEX"],"USR_UID" => $_SESSION["USER_LOGGED"],"DOC_UID" => $indocUid,"APP_DOC_TYPE" => "INPUT","APP_DOC_CREATE_DATE" => date( "Y-m-d H:i:s" ),"APP_DOC_COMMENT" => "","APP_DOC_TITLE" => "","APP_DOC_FILENAME" => $arrayFileName[$i],"FOLDER_UID" => $oFolder->createFromPath( $aID["INP_DOC_DESTINATION_PATH"] ),"APP_DOC_TAGS" => $oFolder->parseTags( $aID["INP_DOC_TAGS"] ),"APP_DOC_FIELDNAME" => $fieldName);
				} else {
					$aFields = array ("APP_UID" => $_SESSION["APPLICATION"],"DEL_INDEX" => $_SESSION["INDEX"],"USR_UID" => $_SESSION["USER_LOGGED"],"DOC_UID" => - 1,"APP_DOC_TYPE" => "ATTACHED","APP_DOC_CREATE_DATE" => date( "Y-m-d H:i:s" ),"APP_DOC_COMMENT" => "","APP_DOC_TITLE" => "","APP_DOC_FILENAME" => $arrayFileName[$i],"APP_DOC_FIELDNAME" => $fieldName);
				}

				$oAppDocument = new AppDocument();
				$oAppDocument->create( $aFields );

				$iDocVersion = $oAppDocument->getDocVersion();
				$sAppDocUid = $oAppDocument->getAppDocUid();
				$aInfo = pathinfo( $oAppDocument->getAppDocFilename() );
				$sExtension = ((isset( $aInfo["extension"] )) ? $aInfo["extension"] : "");
				$pathUID = G::getPathFromUID($_SESSION["APPLICATION"]);
				$sPathName = PATH_DOCUMENT . $pathUID . PATH_SEP;
				$sFileName = $sAppDocUid . "_" . $iDocVersion . "." . $sExtension;
				G::uploadFile( $arrayFileTmpName[$i], $sPathName, $sFileName );

				//Plugin Hook PM_UPLOAD_DOCUMENT for upload document
				$oPluginRegistry = PluginRegistry::loadSingleton();

				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->getNamespace();

						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 );
					}
				}
			}
		}
	}
}
//Define the STEP_POSITION
$ex = isset($_GET['ex']) ? $_GET['ex'] : 0;
//go to the next step
$aNextStep = $oCase->getNextSupervisorStep( $_SESSION['PROCESS'], $_SESSION['STEP_POSITION'] );
G::header( 'Location: cases_StepToRevise?type=DYNAFORM&ex=' . $ex . '&PRO_UID=' . $_SESSION['PROCESS'] . '&DYN_UID=' . $aNextStep['UID'] . '&APP_UID=' . $_SESSION['APPLICATION'] . '&position=' . $aNextStep['POSITION'] . '&DEL_INDEX=' . $_SESSION['INDEX'] );
die();