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.
#815865
Hi,

I would like to achieve the following scenario:
Having a grid which has a file field where user upload a file for each grid's row.
Those documents are uploaded but are not associated with any INPUT DOCUMENT as there's no way to do it from the interface.
I want to upload those files from the grid and then put them in a document structure inside the tree (Home -> Documents - myinputfolder-><documents from the grid>.

How do I do that?

Thanks in advance.
#815871
Ed,
In theory the PMFAssociateUploadedFilesWithInputFile() function should do this for you, but I can't seem to get it to work with MultipleFile fields in grids.

I got it to work with this trigger code:
Code: Select all
//set to ID of Input Document:
$inputDocId = '5745341015b88b760d23e70093092905';
$gridVarName = 'productsList'; //set to name of grid's variable
$fileFieldId = 'photoFiles'; //set to the ID of the MultipleFile field in the grid

$folderName = "Photo_Files"; //set to the name of the folder under Home > Documents
$parentFolder = '/'; //set to UID of the parent folder or '/' if the root folder

if (empty(@=productsList)) {
	goto endTrigger;
}

//doesn't seem to work:
PMFAssociateUploadedFilesWithInputFile($inputDocId, @=productsList, $fileFieldId, @@APPLICATION, 
									   @@USER_LOGGED, @@INDEX);

//do this instead:
require_once 'classes/model/AppFolder.php';
$oFolder = new AppFolder();

require_once 'classes/model/AppDocument.php';
$oDoc = new AppDocument();

// Create if the folders if they don't yet exist:
//array createFolder($folderName, $folderParent = "/", $action = "createifnotexists")

$aFolderInfo = $oFolder->createFolder($folderName, $parentFolder, true);
$folderId = $aFolderInfo['folderUID'];


foreach (@=productsList as $aRow) {
	
	foreach ($aRow['photoFiles'] as $aFile) { 
		$aDocInfo = $oDoc->load($aFile['appDocUid'], $aFile['version']);
		$aDocInfo['FOLDER_UID'] = $folderId;
		$aDocInfo["DOC_UID"] = $inputDocId;
		$aDocInfo["APP_DOC_TYPE"] = 'INPUT';
		$oDoc->update($aDocInfo);		  
	}
}				  

endTrigger:
You can test it:
(27.04 KiB) Downloaded 222 times
Tomorrow, I will look at the source code to figure out what is the problem with the PMFAssociateUploadedFilesWithInputFile() function.
#815880
Ed,
I figured out how to fix the function to work with both file and MultipleFile fields in grids.

Edit workflow/engine/classes/class.pmFunctions.php, and change the definition of PMFAssociateUploadedFilesWithInputFile() (which starts on line 3493 in version 3.2.3) to:
Code: Select all
function PMFAssociateUploadedFilesWithInputFile($inputDocumentUid, $gridVariableName, $fileVariableName, $caseUid, $userUid, $currentDelIndex)
{
    try {
        require_once 'classes/model/AppDocument.php';

        $appDocument = new AppDocument();

        //First step: get all the documents from this case and this grid that are not associated
        $criteria = new Criteria('workflow');

        $criteria->add(AppDocumentPeer::APP_UID, $caseUid, Criteria::EQUAL);
        $criteria->add(AppDocumentPeer::APP_DOC_TYPE, 'ATTACHED', Criteria::EQUAL);
        
        //doesn't work for MultipleFile fields:
        //$criteria->add(AppDocumentPeer::APP_DOC_FIELDNAME, $gridVariableName . '_%_' . $fileVariableName, Criteria::LIKE);
        
        //ABB edit: Find both file and multipleFile fields in grids:
        $criteria->add(
            $criteria->getNewCriterion(AppDocumentPeer::APP_DOC_FIELDNAME, $gridVariableName .'\_%\_'. $fileVariableName, Criteria::LIKE)->addOr(
               $criteria->getNewCriterion(AppDocumentPeer::APP_DOC_FIELDNAME, "[$gridVariableName][%][$fileVariableName]", Criteria::LIKE)
            )
        );
        //ABB edit end
        
        $criteria->add(AppDocumentPeer::APP_DOC_STATUS, 'ACTIVE', Criteria::EQUAL);
        $criteria->add(AppDocumentPeer::DOC_UID, '-1', Criteria::EQUAL);
        $criteria->addAscendingOrderByColumn(AppDocumentPeer::APP_DOC_INDEX);

        $numRecTotal = AppDocumentPeer::doCount($criteria);

        //If we have an error in the result set, do not continue
        if ($numRecTotal == 0) {
            //Return
            return false;
        }
        
        //ABB edit: Get the Input Document's folder UID (and create it if necessary):
        require_once 'classes/model/InputDocument.php';
        require_once 'classes/model/AppFolder.php';
		$oInpDoc = new InputDocument();
		$oFolder = new AppFolder();
		
		$aInpDocInfo = $oInpDoc->load($inputDocumentUid);
		$folderUid = $oFolder->createFromPath( $aInpDocInfo["INP_DOC_DESTINATION_PATH"] );
		//ABB edit end

        //Query
        $rsCriteria = AppDocumentPeer::doSelectRS($criteria);
        $rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC);

        //Search file in the process, associate with the Input document
        while ($rsCriteria->next()) {
            $row = $rsCriteria->getRow();

            //Load the file by its unique identifier, its name, its extension and the path where it's stored
            $file = $appDocument->load($row['APP_DOC_UID']);
            $ext = pathinfo($file['APP_DOC_FILENAME'], PATHINFO_EXTENSION);
            $fileName = $file['APP_DOC_UID'] . '_' . $file['DOC_VERSION'] . '.' . $ext;
            $pathFile = PATH_DOCUMENT . G::getPathFromUID($caseUid) . PATH_SEP . $fileName;
            $comment = $file['APP_DOC_COMMENT']; //ABB edit

            //Delete this file so it can't be uploaded again (Includes Mark Database Record)
            $appDocument->remove($file['APP_DOC_UID'], $file['DOC_VERSION']);

            $fields = array (
                'APP_UID'             => $caseUid,
                'DEL_INDEX'           => $currentDelIndex,
                'USR_UID'             => $userUid,
                'DOC_UID'             => $inputDocumentUid,
                'APP_DOC_TYPE'        => 'INPUT',
                'APP_DOC_CREATE_DATE' => date('Y-m-d H:i:s'),
                'APP_DOC_COMMENT'     => $comment,
                'APP_DOC_TITLE'       => $file['APP_DOC_TITLE'],
                'APP_DOC_FILENAME'    => $file['APP_DOC_FILENAME'],
                'APP_DOC_FIELDNAME'   => '',
                'FOLDER_UID'          => $folderUid      //ABB edit
            );

            $result = $appDocument->create($fields);

            //Copy the file
            $appUid = $appDocument->getAppUid();
            $appDocUid = $appDocument->getAppDocUid();
            $docVersion = $appDocument->getDocVersion();
            $info = pathinfo( $appDocument->getAppDocFilename() );
            $extAux = (isset( $info['extension'] )) ? $info['extension'] : '';

            //Save the file
            $pathName = PATH_DOCUMENT . G::getPathFromUID($appUid) . PATH_SEP;
            $pathFileName = $appDocUid . '_' . $docVersion . '.' . $extAux;

            copy($pathFile, $pathName . $pathFileName);

            //Update
            $result = $appDocument->update([
                'APP_DOC_UID' => $appDocUid,
                'DOC_VERSION' => $docVersion,
                'APP_DOC_TAGS' => 'INPUT',
                'APP_DOC_FIELDNAME' => $file['APP_DOC_FIELDNAME']
            ]);

            //Remove the old submitted file completely from file system
            unlink($pathFile);
        }
    } catch (Exception $e) {
        throw $e;
    }
}
#815881
After you make the above change in the code, then you can associate the files in the grid with an Input Document, by calling this function in a trigger
Code: Select all
PMFAssociateUploadedFilesWithInputFile($inputDocId, "productsList", "photoFiles", @@APPLICATION, 
									   @@USER_LOGGED, @@INDEX);
Where "productsList" is the variable associated with the grid and "photoFiles" is the ID of the file/MultipleFile field in the grid.

PS: How's Mexico City? Still living there?

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

Experience heightened pleasure with Cenforce 100 M[…]

Get an instant solution to move emails to MBOX for[…]

Most Demanding OST to PST Converter

The most demanding OST to PST Converter is TrijaT[…]