Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
By daBoomKing
#790560
Hi All

If a reviewer rejects a case and sends it back to the reviewer, I want to completely remove the uploaded file so the original submitter can attach a new file. Currently it holds the old file. Is there a proper way to completely clear it?

Thanks a lot
Andrew
User avatar
By amosbatto
#790594
The trigger example for AppDocument::remove() shows how to remove the file from the server's file system using the unlink() function.

The trigger example deletes all files for the current case, but you can adapt the code to delete files whose status is set to 'DELETED' like this:
Code: Select all
$caseId = @@APPLICATION;
$query = "SELECT * FROM APP_DOCUMENT WHERE APP_UID='$caseId' AND APP_DOC_STATUS='DELETED'";
$aDocs = executeQuery($query);
if (!is_array($aDocs)) {
   throw new Exception("Error in query: $query");
}

$d = new AppDocument();

foreach ($aDocs as $aDoc) {
   $aFileInfo = $d->Load($aDoc['APP_DOC_UID'], $aDoc['DOC_VERSION']);
   
   //If Output Document file:
   if ($aFileInfo['APP_DOC_TYPE'] == 'OUTPUT') {
      $path = PATH_DOCUMENT . G::getPathFromUID($caseId) . PATH_SEP .'outdocs'. PATH_SEP .
         $aDoc['APP_DOC_UID'] . '_' . $aDoc['DOC_VERSION'];
      if (file_exists($path . '.doc'))
          unlink($path . '.doc');
      if (file_exists($path . '.pdf'))
          unlink($path . '.pdf');
   }
   else { //if Input Document or attached file:
      $ext = pathinfo($aFileInfo['APP_DOC_FILENAME'], PATHINFO_EXTENSION);
      $path = PATH_DOCUMENT . G::getPathFromUID($caseId) . PATH_SEP .
         $aDoc['APP_DOC_UID'] . '_' . $aDoc['DOC_VERSION'] . '.' .$ext;
      unlink($path);
   }
}
Set this trigger to fire before the next step or before assignment in the task containing the DynaForm with file fields. Do not set it to fire immediately after the DynaForm, because at that point the files have not yet been saved in the ProcessMaker server.
By daBoomKing
#790657
Hi Amosbatto

Thanks a lot. I tried using the code from the wiki (modified only to add filtering for the field name with "AND APP_DOC_FIELDNAME ='c_InstallerInvoice'"), as the files are not yet deleted (and wont be done manually by the file manager interface).

After running the trigger, the files are gone from the database (good!), but the file field still shows the previous file links there.

Whats the correct way to also remove the deleted files from being seen within dynaforms? I guess its something like: @@variableName="";

?

But I'm sure its not that simple right?

Thanks a lot
Andrew
User avatar
By amosbatto
#790692
I misunderstood what you want to do. I thought you were just trying to remove files that are already marked as deleted, but now I see that you want to both remove the record for the file from APP_DOCUMENT table and delete the file from the server. For that you can use this code:
Code: Select all
$caseId = @@APPLICATION;
$query = "SELECT * FROM APP_DOCUMENT WHERE APP_UID='$caseId' AND APP_DOC_FIELDNAME='c_InstallerInvoice'";
$aDocs = executeQuery($query);
if (!is_array($aDocs)) {
   throw new Exception("Error in query: $query");
}

$d = new AppDocument();

foreach ($aDocs as $aDoc) {
   $aFileInfo = $d->Load($aDoc['APP_DOC_UID'], $aDoc['DOC_VERSION']);
   //If Output Document file:
   if ($aFileInfo['APP_DOC_TYPE'] == 'OUTPUT') {
      $path = PATH_DOCUMENT . G::getPathFromUID($caseId) . PATH_SEP .'outdocs'. PATH_SEP .
         $aDoc['APP_DOC_UID'] . '_' . $aDoc['DOC_VERSION'];
      if (file_exists($path . '.doc'))
          unlink($path . '.doc');
      if (file_exists($path . '.pdf'))
          unlink($path . '.pdf');
   }
   else { //if Input Document or attached file:
      $ext = pathinfo($aFileInfo['APP_DOC_FILENAME'], PATHINFO_EXTENSION);
      $path = PATH_DOCUMENT . G::getPathFromUID($caseId) . PATH_SEP .
         $aDoc['APP_DOC_UID'] . '_' . $aDoc['DOC_VERSION'] . '.' .$ext;
      unlink($path);
   }
   $query = "DELETE FROM APP_DOCUMENT WHERE APP_UID='$caseId' AND APP_DOC_FIELDNAME='c_InstallerInvoice'";
   executeQuery($query);
}
If you just want to mark a file as deleted in the database, you can use this line:
Code: Select all
   $aFileInfo = $d->Remove($aDoc['APP_DOC_UID'], $aDoc['DOC_VERSION']);
You probably want to execute this code after clicking on a submit button in a form. See this code example: http://wiki.processmaker.com/3.0/Submit ... ancel_Case
User avatar
By Ohisttel
#790775
I've been looking for it recently! Thank a lot, this works great for me!
amosbatto wrote:
Code: Select all
$caseId = @@APPLICATION;
$query = "SELECT * FROM APP_DOCUMENT WHERE APP_UID='$caseId' AND APP_DOC_FIELDNAME='c_InstallerInvoice'";
$aDocs = executeQuery($query);
if (!is_array($aDocs)) {
   throw new Exception("Error in query: $query");
}

$d = new AppDocument();

foreach ($aDocs as $aDoc) {
   $aFileInfo = $d->Load($aDoc['APP_DOC_UID'], $aDoc['DOC_VERSION']);
   //If Output Document file:
   if ($aFileInfo['APP_DOC_TYPE'] == 'OUTPUT') {
      $path = PATH_DOCUMENT . G::getPathFromUID($caseId) . PATH_SEP .'outdocs'. PATH_SEP .
         $aDoc['APP_DOC_UID'] . '_' . $aDoc['DOC_VERSION'];
      if (file_exists($path . '.doc'))
          unlink($path . '.doc');
      if (file_exists($path . '.pdf'))
          unlink($path . '.pdf');
   }
   else { //if Input Document or attached file:
      $ext = pathinfo($aFileInfo['APP_DOC_FILENAME'], PATHINFO_EXTENSION);
      $path = PATH_DOCUMENT . G::getPathFromUID($caseId) . PATH_SEP .
         $aDoc['APP_DOC_UID'] . '_' . $aDoc['DOC_VERSION'] . '.' .$ext;
      unlink($path);
   }
   $query = "DELETE FROM APP_DOCUMENT WHERE APP_UID='$caseId' AND APP_DOC_FIELDNAME='c_InstallerInvoice'";
   executeQuery($query);
}
If you just want to mark a file as deleted in the database, you can use this line:
Code: Select all
   $aFileInfo = $d->Remove($aDoc['APP_DOC_UID'], $aDoc['DOC_VERSION']);
You probably want to execute this code after clicking on a submit button in a form. See this code example: http://wiki.processmaker.com/3.0/Submit ... ancel_Case
Want to create your own meme coin?

In the world of cryptocurrencies, a unique and exc[…]

The market for cryptocurrencies is demonstrating a[…]

What's SAP FICO?

Embarking on a dissertation can be one of the most[…]

Hello. For rental housing, there are software solu[…]