PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/workflow/engine/methods/processes/processes_Import_Ajax.php

https://bitbucket.org/ferOnti/processmaker
PHP | 292 lines | 203 code | 45 blank | 44 comment | 70 complexity | d8592d9145c086cb270112e6c63a0b10 MD5 | raw file
  1. <?php
  2. /**
  3. * processes_ImportFile.php
  4. *
  5. * ProcessMaker Open Source Edition
  6. * Copyright (C) 2004 - 2008 Colosa Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
  22. * Coral Gables, FL, 33134, USA, or email info@colosa.com.
  23. */
  24. ini_set( 'max_execution_time', '0' );
  25. function reservedWordsSqlValidate ($data)
  26. {
  27. $arrayAux = array ();
  28. $reservedWordsSql = G::reservedWordsSql();
  29. foreach ($data->reportTables as $rptIndex => $rptValue) {
  30. if (in_array( strtoupper( $rptValue["REP_TAB_NAME"] ), $reservedWordsSql )) {
  31. $arrayAux[] = $rptValue["REP_TAB_NAME"];
  32. }
  33. }
  34. if (count( $arrayAux ) > 0) {
  35. throw (new Exception( G::LoadTranslation( "ID_PMTABLE_INVALID_NAME", array (implode( ", ", $arrayAux )
  36. ) ) ));
  37. }
  38. $arrayAux = array ();
  39. foreach ($data->reportTablesVars as $rptIndex => $rptValue) {
  40. if (in_array( strtoupper( $rptValue["REP_VAR_NAME"] ), $reservedWordsSql )) {
  41. $arrayAux[] = $rptValue["REP_VAR_NAME"];
  42. }
  43. }
  44. if (count( $arrayAux ) > 0) {
  45. throw (new Exception( G::LoadTranslation( "ID_PMTABLE_INVALID_FIELD_NAME", array (implode( ", ", $arrayAux )
  46. ) ) ));
  47. }
  48. }
  49. $action = isset( $_REQUEST['ajaxAction'] ) ? $_REQUEST['ajaxAction'] : null;
  50. $result = new stdClass();
  51. $result->success = true;
  52. $result->catchMessage = "";
  53. if ($action == "uploadFileNewProcess") {
  54. try {
  55. //type of file: only pm
  56. $processFileType = $_REQUEST["processFileType"];
  57. $oProcess = new stdClass();
  58. $oData = new stdClass();
  59. $isCorrectTypeFile = 1;
  60. if (isset( $_FILES['form']['type']['PROCESS_FILENAME'] )) {
  61. $allowedExtensions = array ($processFileType
  62. );
  63. $allowedExtensions = array ('pm');
  64. if (! in_array( end( explode( ".", $_FILES['form']['name']['PROCESS_FILENAME'] ) ), $allowedExtensions )) {
  65. throw new Exception( G::LoadTranslation( "ID_FILE_UPLOAD_INCORRECT_EXTENSION" ) );
  66. }
  67. }
  68. if ($processFileType != "pm") {
  69. throw new Exception( G::LoadTranslation( "ID_ERROR_UPLOAD_FILE_CONTACT_ADMINISTRATOR" ) );
  70. }
  71. if ($processFileType == "pm") {
  72. G::LoadClass( 'processes' );
  73. $oProcess = new Processes();
  74. }
  75. $result->success = true;
  76. $result->ExistProcessInDatabase = ""; //"" -Default
  77. //0 -Dont exist process
  78. //1 -exist process
  79. $result->ExistGroupsInDatabase = ""; //"" -Default
  80. //0 -Dont exist process
  81. //1 -exist process
  82. $optionGroupExistInDatabase = isset( $_REQUEST["optionGroupExistInDatabase"] ) ? $_REQUEST["optionGroupExistInDatabase"] : null;
  83. //!Upload file
  84. if (! is_null( $optionGroupExistInDatabase )) {
  85. $filename = $_REQUEST["PRO_FILENAME"];
  86. $path = PATH_DOCUMENT . 'input' . PATH_SEP;
  87. } else {
  88. if ($_FILES['form']['error']['PROCESS_FILENAME'] == 0) {
  89. $filename = $_FILES['form']['name']['PROCESS_FILENAME'];
  90. $path = PATH_DOCUMENT . 'input' . PATH_SEP;
  91. $tempName = $_FILES['form']['tmp_name']['PROCESS_FILENAME'];
  92. //$action = "none";
  93. G::uploadFile( $tempName, $path, $filename );
  94. }
  95. }
  96. //importing a bpmn diagram, using external class to do it.
  97. if ($processFileType == "bpmn") {
  98. G::LoadClass( 'bpmnExport' );
  99. $bpmn = new bpmnExport();
  100. $bpmn->importBpmn( $path . $filename );
  101. die();
  102. }
  103. //if file is a .pm file continues normally the importing
  104. if ($processFileType == "pm") {
  105. $oData = $oProcess->getProcessData( $path . $filename );
  106. }
  107. reservedWordsSqlValidate( $oData );
  108. //!Upload file
  109. $Fields['PRO_FILENAME'] = $filename;
  110. $Fields['IMPORT_OPTION'] = 2;
  111. $sProUid = $oData->process['PRO_UID'];
  112. $oData->process['PRO_UID_OLD'] = $sProUid;
  113. if ($oProcess->processExists( $sProUid )) {
  114. $result->ExistProcessInDatabase = 1;
  115. } else {
  116. $result->ExistProcessInDatabase = 0;
  117. }
  118. //!respect of the groups
  119. $result->ExistGroupsInDatabase = 1;
  120. $result->groupBeforeAccion = $action;
  121. if (! is_null( $optionGroupExistInDatabase )) {
  122. if ($optionGroupExistInDatabase == 1) {
  123. $oData->groupwfs = $oProcess->renameExistingGroups( $oData->groupwfs );
  124. } else if ($optionGroupExistInDatabase == 2) {
  125. $oBaseGroup = $oData->groupwfs;
  126. $oNewGroup = $oProcess->mergeExistingGroups( $oData->groupwfs );
  127. $oData->groupwfs = $oNewGroup;
  128. $oData->taskusers = $oProcess->mergeExistingUsers( $oBaseGroup, $oNewGroup, $oData->taskusers );
  129. }
  130. $result->ExistGroupsInDatabase = 0;
  131. } else {
  132. if (! ($oProcess->checkExistingGroups( $oData->groupwfs ) > 0)) {
  133. $result->ExistGroupsInDatabase = 0;
  134. }
  135. }
  136. //!respect of the groups
  137. if ($result->ExistProcessInDatabase == 0 && $result->ExistGroupsInDatabase == 0) {
  138. if ($processFileType == "pm") {
  139. $oProcess->createProcessFromData( $oData, $path . $filename );
  140. }
  141. }
  142. //!data ouput
  143. $result->sNewProUid = $sProUid;
  144. $result->proFileName = $Fields['PRO_FILENAME'];
  145. } catch (Exception $e) {
  146. $result->response = $e->getMessage();
  147. $result->catchMessage = $e->getMessage();
  148. $result->success = true;
  149. }
  150. }
  151. if ($action == "uploadFileNewProcessExist") {
  152. try {
  153. $option = $_REQUEST["IMPORT_OPTION"];
  154. $filename = $_REQUEST["PRO_FILENAME"];
  155. $processFileType = $_REQUEST["processFileType"];
  156. $result->ExistGroupsInDatabase = ""; //"" -Default
  157. //0 -Dont exist process
  158. //1 -exist process
  159. $optionGroupExistInDatabase = isset( $_REQUEST["optionGroupExistInDatabase"] ) ? $_REQUEST["optionGroupExistInDatabase"] : null;
  160. $sNewProUid = "";
  161. $oProcess = new stdClass();
  162. if ($processFileType != "pm") {
  163. throw new Exception( G::LoadTranslation( "ID_ERROR_UPLOAD_FILE_CONTACT_ADMINISTRATOR" ) );
  164. }
  165. //load the variables
  166. if ($processFileType == "pm") {
  167. G::LoadClass( 'processes' );
  168. $oProcess = new Processes();
  169. }
  170. $path = PATH_DOCUMENT . 'input' . PATH_SEP;
  171. if ($processFileType == "pm") {
  172. $oData = $oProcess->getProcessData( $path . $filename );
  173. }
  174. reservedWordsSqlValidate( $oData );
  175. $Fields['PRO_FILENAME'] = $filename;
  176. $sProUid = $oData->process['PRO_UID'];
  177. $oData->process['PRO_UID_OLD'] = $sProUid;
  178. $result->ExistGroupsInDatabase = 1;
  179. if (! is_null( $optionGroupExistInDatabase )) {
  180. if ($optionGroupExistInDatabase == 1) {
  181. $oData->groupwfs = $oProcess->renameExistingGroups( $oData->groupwfs );
  182. } else if ($optionGroupExistInDatabase == 2) {
  183. $oBaseGroup = $oData->groupwfs;
  184. $oNewGroup = $oProcess->mergeExistingGroups( $oData->groupwfs );
  185. $oData->groupwfs = $oNewGroup;
  186. $oData->taskusers = $oProcess->mergeExistingUsers( $oBaseGroup, $oNewGroup, $oData->taskusers );
  187. }
  188. $result->ExistGroupsInDatabase = 0;
  189. } else {
  190. if (! ($oProcess->checkExistingGroups( $oData->groupwfs ) > 0)) {
  191. $result->ExistGroupsInDatabase = 0;
  192. }
  193. }
  194. if ($result->ExistGroupsInDatabase == 0) {
  195. //Update the current Process, overwriting all tasks and steps
  196. if ($option == 1) {
  197. $oProcess->updateProcessFromData( $oData, $path . $filename );
  198. if (file_exists( PATH_OUTTRUNK . 'compiled' . PATH_SEP . 'xmlform' . PATH_SEP . $sProUid )) {
  199. $oDirectory = dir( PATH_OUTTRUNK . 'compiled' . PATH_SEP . 'xmlform' . PATH_SEP . $sProUid );
  200. while ($sObjectName = $oDirectory->read()) {
  201. if (($sObjectName != '.') && ($sObjectName != '..')) {
  202. unlink( PATH_OUTTRUNK . 'compiled' . PATH_SEP . 'xmlform' . PATH_SEP . $sProUid . PATH_SEP . $sObjectName );
  203. }
  204. }
  205. $oDirectory->close();
  206. }
  207. $sNewProUid = $sProUid;
  208. }
  209. //Disable current Process and create a new version of the Process
  210. if ($option == 2) {
  211. $oProcess->disablePreviousProcesses( $sProUid );
  212. $sNewProUid = $oProcess->getUnusedProcessGUID();
  213. $oProcess->setProcessGuid( $oData, $sNewProUid );
  214. $oProcess->setProcessParent( $oData, $sProUid );
  215. $oData->process['PRO_TITLE'] = "New - " . $oData->process['PRO_TITLE'] . ' - ' . date( 'M d, H:i' );
  216. $oProcess->renewAll( $oData );
  217. if ($processFileType == "pm") {
  218. $oProcess->createProcessFromData( $oData, $path . $filename );
  219. }
  220. }
  221. //Create a completely new Process without change the current Process
  222. if ($option == 3) {
  223. //krumo ($oData); die;
  224. $sNewProUid = $oProcess->getUnusedProcessGUID();
  225. $oProcess->setProcessGuid( $oData, $sNewProUid );
  226. $oData->process['PRO_TITLE'] = "Copy of - " . $oData->process['PRO_TITLE'] . ' - ' . date( 'M d, H:i' );
  227. $oProcess->renewAll( $oData );
  228. if ($processFileType == "pm") {
  229. $oProcess->createProcessFromData( $oData, $path . $filename );
  230. }
  231. }
  232. }
  233. //!data ouput
  234. $result->fileName = $filename;
  235. $result->importOption = $option;
  236. $result->sNewProUid = $sNewProUid;
  237. $result->success = true;
  238. $result->ExistGroupsInDatabase = $result->ExistGroupsInDatabase;
  239. $result->groupBeforeAccion = $action;
  240. //!data ouput
  241. } catch (Exception $e) {
  242. $result->response = $e->getMessage();
  243. $result->success = true;
  244. }
  245. }
  246. echo G::json_encode( $result );
  247. exit();