diff --git a/tests/unit/workflow/engine/classes/model/AppNotesTest.php b/tests/unit/workflow/engine/classes/model/AppNotesTest.php index d97615690..bc22a33d1 100644 --- a/tests/unit/workflow/engine/classes/model/AppNotesTest.php +++ b/tests/unit/workflow/engine/classes/model/AppNotesTest.php @@ -215,9 +215,8 @@ class AppNotesTest extends TestCase 'DOC_ID' => $appNote->NOTE_ID ]); - $appUid = $appDocument->APP_UID; $appNotes = new ModelAppNotes(); - $result = $appNotes->getAttachedFilesFromTheCaseNote($appNote->NOTE_ID); + $result = $appNotes->getAttachedFilesFromTheCaseNote($appNote->NOTE_ID, $appDocument->APP_UID); $this->assertNotEmpty($result); } diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/DocumentsTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/DocumentsTest.php index af49f7982..cfdd36078 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/DocumentsTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/DocumentsTest.php @@ -61,7 +61,7 @@ class DocumentsTest extends TestCase $appDocument =Documents::factory()->create([ 'DOC_ID' => $appNote->NOTE_ID ]); - $result = Documents::getFiles($appDocument->DOC_ID); + $result = Documents::getFiles($appDocument->DOC_ID, $appDocument->APP_UID); $this->assertNotEmpty($result); } diff --git a/workflow/engine/classes/Cases.php b/workflow/engine/classes/Cases.php index 0a1bd955a..9508dd99f 100644 --- a/workflow/engine/classes/Cases.php +++ b/workflow/engine/classes/Cases.php @@ -3730,6 +3730,7 @@ class Cases if ($folderitem->filename == $aRow['APP_DOC_UID']) { $aFields['DOWNLOAD_LABEL'] = G::LoadTranslation('ID_GET_EXTERNAL_FILE'); $aFields['DOWNLOAD_LINK'] = $folderitem->downloadScript; + unset($aFields['NEWVERSION_LABEL'], $aFields['VERSIONHISTORY_LABEL']); continue; } } @@ -3997,7 +3998,7 @@ class Cases $uploadReturn = $pluginRegistry->executeTriggers(PM_UPLOAD_DOCUMENT, $documentData); if ($uploadReturn) { - $arrayField["APP_DOC_PLUGIN"] = $triggerDetail->sNamespace; + $arrayField["APP_DOC_PLUGIN"] = $triggerDetail->getNamespace(); if (!isset($arrayField["APP_DOC_UID"])) { $arrayField["APP_DOC_UID"] = $appDocUid; diff --git a/workflow/engine/classes/PmDynaform.php b/workflow/engine/classes/PmDynaform.php index 697d4879b..a217ba7b1 100644 --- a/workflow/engine/classes/PmDynaform.php +++ b/workflow/engine/classes/PmDynaform.php @@ -6,7 +6,9 @@ use ProcessMaker\Core\System; use ProcessMaker\BusinessModel\DynaForm\SuggestTrait; use ProcessMaker\BusinessModel\Cases; use ProcessMaker\BusinessModel\DynaForm\ValidatorFactory; +use ProcessMaker\Model\Documents; use ProcessMaker\Model\Dynaform as ModelDynaform; +use ProcessMaker\Plugins\PluginRegistry; /** * Implementing pmDynaform library in the running case. @@ -25,6 +27,7 @@ class PmDynaform private $propertiesToExclude = []; private $sysSys = null; private $fieldsAppData; + private $filesFromPlugin = [Documents::DOC_TYPE_ATTACHED => null, Documents::DOC_TYPE_INPUT => null]; public $credentials = null; public $displayMode = null; public $fields = null; @@ -567,6 +570,7 @@ class PmDynaform $oCriteriaAppDocument = new Criteria("workflow"); $oCriteriaAppDocument->addSelectColumn(AppDocumentPeer::APP_DOC_UID); $oCriteriaAppDocument->addSelectColumn(AppDocumentPeer::DOC_VERSION); + $oCriteriaAppDocument->addSelectColumn(AppDocumentPeer::APP_DOC_TYPE); $oCriteriaAppDocument->add(AppDocumentPeer::APP_UID, $this->fields["APP_DATA"]["APPLICATION"]); $oCriteriaAppDocument->add(AppDocumentPeer::APP_DOC_FIELDNAME, $json->name); $oCriteriaAppDocument->add(AppDocumentPeer::APP_DOC_STATUS, 'ACTIVE'); @@ -582,8 +586,44 @@ class PmDynaform $oAppDocument = new AppDocument(); if ($row = $rs->getRow()) { + // Only get the information from the plugin once + if (is_null($this->filesFromPlugin[$row['APP_DOC_TYPE']])) { + // Plugin Hook PM_CASE_DOCUMENT_LIST to get the files uploaded as attachments + $pluginRegistry = PluginRegistry::loadSingleton(); + + // If the hook exists try to execute + if ($pluginRegistry->existsTrigger(PM_CASE_DOCUMENT_LIST) && class_exists('folderData')) { + // Build the required object + $folderData = new folderData(null, null, $this->fields['APP_DATA']['APPLICATION'], null, null); + $folderData->PMType = $row['APP_DOC_TYPE']; + $folderData->returnList = true; + + // Get elements + $this->filesFromPlugin[$row['APP_DOC_TYPE']] = $pluginRegistry->executeTriggers(PM_CASE_DOCUMENT_LIST, $folderData); + } else { + // If not exist, set an empty array + $this->filesFromPlugin[$row['APP_DOC_TYPE']] = []; + } + } + // Load document data $oAppDocument->load($row["APP_DOC_UID"], $row["DOC_VERSION"]); - $links[] = "../cases/cases_ShowDocument?a=" . $row["APP_DOC_UID"] . "&v=" . $row["DOC_VERSION"]; + + // Build the default link + $link = "../cases/cases_ShowDocument?a=" . $row["APP_DOC_UID"] . "&v=" . $row["DOC_VERSION"]; + + // If exist related file in the plugin, check if the file is the same + if (is_array($this->filesFromPlugin[$row['APP_DOC_TYPE']])) { + foreach ($this->filesFromPlugin[$row['APP_DOC_TYPE']] as $file) { + // If exists the same file, replace the download link + if ($file->filename === $row['APP_DOC_UID']) { + $link = $file->downloadScript; + continue; + } + } + } + + // Set the link and another related information + $links[] = $link; $labelsFromDb[] = $oAppDocument->getAppDocFilename(); $appDocUids[] = $row["APP_DOC_UID"]; } diff --git a/workflow/engine/classes/model/AppFolder.php b/workflow/engine/classes/model/AppFolder.php index 52f5675de..d230fd367 100644 --- a/workflow/engine/classes/model/AppFolder.php +++ b/workflow/engine/classes/model/AppFolder.php @@ -670,25 +670,25 @@ class AppFolder extends BaseAppFolder switch ($row4['OUT_DOC_GENERATE']) { case "PDF": - $downloadLink = "../cases/cases_ShowOutputDocument?a=" . $appDocUid . "&v=" . $docVersion . "&ext=pdf" . "&random=" . rand(); + $downloadLink = "../cases/cases_ShowOutputDocument?a=" . $appDocUid . "&v=" . $docVersion . "&ext=pdf" . "&random=" . rand() . "&p=1"; $downloadLink1 = ""; $downloadLabel = ".pdf"; $downloadLabel1 = ""; break; case "DOC": - $downloadLink = "../cases/cases_ShowOutputDocument?a=" . $appDocUid . "&v=" . $docVersion . "&ext=doc" . "&random=" . rand(); + $downloadLink = "../cases/cases_ShowOutputDocument?a=" . $appDocUid . "&v=" . $docVersion . "&ext=doc" . "&random=" . rand() . "&p=1"; $downloadLink1 = ""; $downloadLabel = ".doc"; $downloadLabel1 = ""; break; case "BOTH": - $downloadLink = "../cases/cases_ShowOutputDocument?a=" . $appDocUid . "&v=" . $docVersion . "&ext=pdf" . "&random=" . rand(); - $downloadLink1 = "../cases/cases_ShowOutputDocument?a=" . $appDocUid . "&v=" . $docVersion . "&ext=doc" . "&random=" . rand(); + $downloadLink = "../cases/cases_ShowOutputDocument?a=" . $appDocUid . "&v=" . $docVersion . "&ext=pdf" . "&random=" . rand() . "&p=1"; + $downloadLink1 = "../cases/cases_ShowOutputDocument?a=" . $appDocUid . "&v=" . $docVersion . "&ext=doc" . "&random=" . rand() . "&p=1"; $downloadLabel = ".pdf"; $downloadLabel1 = ".doc"; break; case "NOFILE": - $downloadLink = "../cases/cases_ShowDocument?a=" . $appDocUid . "&v=" . $docVersion; + $downloadLink = "../cases/cases_ShowDocument?a=" . $appDocUid . "&v=" . $docVersion . "&p=1"; $downloadLink1 = ""; $downloadLabel = G::LoadTranslation("ID_DOWNLOAD"); $downloadLabel1 = ""; @@ -709,56 +709,23 @@ class AppFolder extends BaseAppFolder $row4 = array (); $versioningEnabled = false; } - $downloadLink = "../cases/cases_ShowDocument?a=" . $appDocUid . "&v=" . $docVersion; + $downloadLink = "../cases/cases_ShowDocument?a=" . $appDocUid . "&v=" . $docVersion . "&p=1"; $downloadLink1 = ""; $downloadLabel = G::LoadTranslation( 'ID_DOWNLOAD' ); $downloadLabel1 = ""; } else { $row4 = array (); $versioningEnabled = false; - $downloadLink = "../cases/cases_ShowDocument?a=" . $appDocUid . "&v=" . $docVersion; + $downloadLink = "../cases/cases_ShowDocument?a=" . $appDocUid . "&v=" . $docVersion . "&p=1"; $downloadLink1 = ""; $downloadLabel = G::LoadTranslation( 'ID_DOWNLOAD' ); $downloadLabel1 = ""; } - - if (! empty( $row1["APP_DOC_PLUGIN"] )) { - $pluginRegistry = PluginRegistry::loadSingleton(); - $pluginName = $row1["APP_DOC_PLUGIN"]; - $fieldValue = ""; - - if (file_exists( PATH_PLUGINS . $pluginName . ".php" )) { - $pluginDetail = $pluginRegistry->getPluginDetails( $pluginName . ".php" ); - - if ($pluginDetail) { - if ($pluginDetail->isEnabled()) { - require_once (PATH_PLUGINS . $pluginName . ".php"); - $pluginNameClass = $pluginName . "Plugin"; - $objPluginClass = new $pluginNameClass( $pluginName ); - - if (isset( $objPluginClass->sMethodGetUrlDownload ) && ! empty( $objPluginClass->sMethodGetUrlDownload )) { - if (file_exists( PATH_PLUGINS . $pluginName . PATH_SEP . "class." . $pluginName . ".php" )) { - require_once (PATH_PLUGINS . $pluginName . PATH_SEP . "class." . $pluginName . ".php"); - $pluginNameClass = $pluginName . "Class"; - $objClass = new $pluginNameClass(); - - if (method_exists( $objClass, $objPluginClass->sMethodGetUrlDownload )) { - eval( "\$url = \$objClass->" . $objPluginClass->sMethodGetUrlDownload . "(\"" . $row1["APP_DOC_UID"] . "\");" ); - $downloadLink = $url; - $fieldValue = $row1["APP_DOC_PLUGIN"]; - } - } - } - } - } - } - $row1["APP_DOC_PLUGIN"] = $fieldValue; - } break; default: $row4 = array (); $versioningEnabled = false; - $downloadLink = "../cases/cases_ShowDocument?a=" . $appDocUid . "&v=" . $docVersion; + $downloadLink = "../cases/cases_ShowDocument?a=" . $appDocUid . "&v=" . $docVersion . "&p=1"; $downloadLink1 = ""; $downloadLabel = G::LoadTranslation( 'ID_DOWNLOAD' ); $downloadLabel1 = ""; diff --git a/workflow/engine/classes/model/AppNotes.php b/workflow/engine/classes/model/AppNotes.php index 4ecece2d9..6b58acae1 100644 --- a/workflow/engine/classes/model/AppNotes.php +++ b/workflow/engine/classes/model/AppNotes.php @@ -227,7 +227,7 @@ class AppNotes extends BaseAppNotes // Get the files related to the specific case note if ($noteId !== 0) { - $attachFileLinks = $this->getAttachedFilesFromTheCaseNote($noteId); + $attachFileLinks = $this->getAttachedFilesFromTheCaseNote($noteId, $appUid); } if (!empty($attachFileLinks)) { @@ -282,15 +282,16 @@ class AppNotes extends BaseAppNotes /** * Get attached files from a specific case note * @param int $docId + * @param string $appUid * @return array */ - public function getAttachedFilesFromTheCaseNote(int $docId): array + public function getAttachedFilesFromTheCaseNote(int $docId, string $appUid): array { $attachFileLinks = []; $url = System::getServerMainPath(); - $result = Documents::getFiles($docId); + $result = Documents::getFiles($docId, $appUid); foreach ($result as $item) { - $href = $url . "/cases/casesShowCaseNotes?a={$item['APP_DOC_UID']}&v={$item['DOC_VERSION']}"; + $href = $url . str_replace('../', '/', $item['LINK']); $attachFileLinks[] = "{$item['APP_DOC_FILENAME']}"; } diff --git a/workflow/engine/controllers/appProxy.php b/workflow/engine/controllers/appProxy.php index 339364ff3..9b3a7df36 100644 --- a/workflow/engine/controllers/appProxy.php +++ b/workflow/engine/controllers/appProxy.php @@ -128,7 +128,7 @@ class AppProxy extends HttpProxyController $iterator = 0; foreach ($response['notes'] as $value) { $response['notes'][$iterator]['NOTE_DATE'] = DateTime::convertUtcToTimeZone($value['NOTE_DATE']); - $response['notes'][$iterator]['attachments'] = $documents->getFiles($value['NOTE_ID']); + $response['notes'][$iterator]['attachments'] = $documents->getFiles($value['NOTE_ID'], $appUid); $iterator++; } // Get the total of cases notes by case diff --git a/workflow/engine/methods/tracker/tracker_Ajax.php b/workflow/engine/methods/tracker/tracker_Ajax.php index 85cdb8908..25638c2ac 100644 --- a/workflow/engine/methods/tracker/tracker_Ajax.php +++ b/workflow/engine/methods/tracker/tracker_Ajax.php @@ -185,8 +185,11 @@ try { $filesPluginArray = $oPluginRegistry->executeTriggers(PM_CASE_DOCUMENT_LIST_ARR, $aFields['APP_UID']); //Now search for the file, if exists the change the download URL foreach ($filesPluginArray as $file) { - if ($file->filename == $_POST['APP_DOC_UID']) { - $aFields['FILE2'] = $file->downloadScript; // The PDF is the only one uploaded to KT + if ($file->filename == $_POST['APP_DOC_UID'] && $file->type === 'DOC') { + $aFields['FILE1'] = $file->downloadScript; + } + if ($file->filename == $_POST['APP_DOC_UID'] && $file->type === 'PDF') { + $aFields['FILE2'] = $file->downloadScript; } } } diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php index 87eec46fe..006f33eb0 100755 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php @@ -2161,7 +2161,7 @@ class Cases $data[$iterator] = array_change_key_case($value, CASE_LOWER); $data[$iterator]['note_date'] = UtilDateTime::convertUtcToTimeZone($value['NOTE_DATE']); if ($files) { - $data[$iterator]['attachments'] = $documents->getFiles($value['NOTE_ID']); + $data[$iterator]['attachments'] = $documents->getFiles($value['NOTE_ID'], $appUid); } $iterator++; } diff --git a/workflow/engine/src/ProcessMaker/Model/Documents.php b/workflow/engine/src/ProcessMaker/Model/Documents.php index 443d7aab1..1106cf38f 100644 --- a/workflow/engine/src/ProcessMaker/Model/Documents.php +++ b/workflow/engine/src/ProcessMaker/Model/Documents.php @@ -3,7 +3,9 @@ namespace ProcessMaker\Model; use App\Factories\HasFactory; +use folderData; use Illuminate\Database\Eloquent\Model; +use ProcessMaker\Plugins\PluginRegistry; class Documents extends Model { @@ -131,18 +133,47 @@ class Documents extends Model * Return the documents related to the specific DOC_ID * * @param int $docId + * @param string $appUid * * @return array */ - public static function getFiles(int $docId) + public static function getFiles(int $docId, string $appUid) { + // Initializing variables + $elements = null; + + // Plugin Hook PM_CASE_DOCUMENT_LIST to get the files uploaded in the case notes + $pluginRegistry = PluginRegistry::loadSingleton(); + + // If the hook exists try to execute + if ($pluginRegistry->existsTrigger(PM_CASE_DOCUMENT_LIST) && class_exists('folderData')) { + // Build the required object + $folderData = new folderData(null, null, $appUid, null, null); + $folderData->PMType = self::DOC_TYPE_CASE_NOTE; + $folderData->returnList = true; + + // Get elements + $elements = $pluginRegistry->executeTriggers(PM_CASE_DOCUMENT_LIST, $folderData); + } + $query = Documents::query()->select(['APP_DOC_UID', 'APP_DOC_FILENAME', 'DOC_VERSION']); $query->docId($docId); $results = $query->get(); $documentList = []; - $results->each(function ($item, $key) use (&$documentList) { + $results->each(function ($item, $key) use (&$documentList, $elements) { $row = $item->toArray(); $row['LINK'] = "../cases/casesShowCaseNotes?a=" . $row["APP_DOC_UID"] . "&v=" . $row["DOC_VERSION"]; + + // If an element match, replace the link and exit from the loop + if (!empty($elements) && is_array($elements)) { + foreach ($elements as $element) { + if ($element->filename === $row['APP_DOC_UID']) { + $row['LINK'] = $element->downloadScript; + continue; + } + } + } + $documentList[] = $row; });