From d844c13055c5bd878e5c8084bd456a1409e69d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20Cesar=20Laura=20Avenda=C3=B1o?= Date: Thu, 19 Jan 2023 17:28:14 +0000 Subject: [PATCH] PMCORE-4120 --- .../Util/Helpers/SaveAppDocumentTest.php | 4 +-- .../src/ProcessMaker/BusinessModel/Cases.php | 28 +++++++++++++++++-- .../engine/src/ProcessMaker/Util/helpers.php | 10 +++---- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Util/Helpers/SaveAppDocumentTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Util/Helpers/SaveAppDocumentTest.php index 6ce64547b..487c81723 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Util/Helpers/SaveAppDocumentTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Util/Helpers/SaveAppDocumentTest.php @@ -22,7 +22,7 @@ class SaveAppDocumentTest extends TestCase $appDocUid = G::generateUniqueID(); $pathCase = PATH_DB . config('system.workspace') . PATH_SEP . 'files' . PATH_SEP . G::getPathFromUID($appUid) . PATH_SEP; $res = saveAppDocument($files, $appUid, $appDocUid, 1, false); - $this->assertTrue($res); + $this->assertNotEmpty($res); G::rm_dir($pathCase); } @@ -41,7 +41,7 @@ class SaveAppDocumentTest extends TestCase $appDocUid = G::generateUniqueID(); $pathCase = PATH_DB . config('system.workspace') . PATH_SEP . 'files' . PATH_SEP . G::getPathFromUID($appUid) . PATH_SEP; $res = saveAppDocument($files, $appUid, $appDocUid, 1, false); - $this->assertTrue($res); + $this->assertNotEmpty($res); G::rm_dir($pathCase); } } \ No newline at end of file diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php index 3285b312c..a62db2ad2 100755 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php @@ -77,6 +77,7 @@ use Task as ModelTask; use TaskPeer; use Tasks as ClassesTasks; use TaskUserPeer; +use uploadDocumentData; use Users as ModelUsers; use UsersPeer; use WsBase; @@ -4173,10 +4174,10 @@ class Cases $appDocUid = G::generateUniqueID(); // Upload or move the file - $isUploaded = saveAppDocument($fileName, $appUid, $appDocUid, 1, $upload); + $pathFile = saveAppDocument($fileName, $appUid, $appDocUid, 1, $upload); // If the file was uploaded correctly we will to register in the DB - if ($isUploaded) { + if (!empty($pathFile)) { $attributes = [ "DOC_ID" => $noteId, "APP_DOC_UID" => $appDocUid, @@ -4193,6 +4194,29 @@ class Cases // List of files uploaded or copy $response['attachments'][$i++] = $attributes; + + //Plugin Hook PM_UPLOAD_DOCUMENT for upload document + $pluginRegistry = PluginRegistry::loadSingleton(); + + // If the hook exists try to execute + if ($pluginRegistry->existsTrigger(PM_UPLOAD_DOCUMENT) && class_exists('uploadDocumentData')) { + // Get hook details + $triggerDetail = $pluginRegistry->getTriggerInfo(PM_UPLOAD_DOCUMENT); + + // Instance object used by the hook + $documentData = new uploadDocumentData($appUid, $userUid, $pathFile, $attributes['APP_DOC_FILENAME'], $appDocUid, 1); + + // Execute hook + $uploadReturn = $pluginRegistry->executeTriggers(PM_UPLOAD_DOCUMENT, $documentData); + + // If the executions is correct, update the record related to the document + if ($uploadReturn) { + Documents::where('APP_DOC_UID', $appDocUid)->update(['APP_DOC_PLUGIN' => $triggerDetail->getNamespace()]); + + // Remove the file from the server + unlink($pathFile); + } + } } else { $response['attachment_errors'][$j++] = [ 'error' => 'error', diff --git a/workflow/engine/src/ProcessMaker/Util/helpers.php b/workflow/engine/src/ProcessMaker/Util/helpers.php index 5e810fe2d..2fbfdc33f 100644 --- a/workflow/engine/src/ProcessMaker/Util/helpers.php +++ b/workflow/engine/src/ProcessMaker/Util/helpers.php @@ -676,23 +676,23 @@ function saveAppDocument($file, $appUid, $appDocUid, $version = 1, $upload = tru $info = pathinfo($file["name"]); $extension = ((isset($info["extension"])) ? $info["extension"] : ""); $fileName = $appDocUid . "_" . $version . "." . $extension; - $pathCase = PATH_DATA_SITE . 'files' . PATH_SEP . G::getPathFromUID($appUid) . PATH_SEP; + $pathFile = $pathCase . $fileName; - $response = false; if ($upload) { G::uploadFile( $file["tmp_name"], $pathCase, $fileName ); - $response = true; } else { G::verifyPath($pathCase, true); - $response = copy($file["tmp_name"], $pathCase . $fileName); + if (!copy($file["tmp_name"], $pathCase . $fileName)) { + $pathFile = ''; + } } - return $response; + return $pathFile; } catch (Exception $e) { throw $e; }