Questions and discussion about using ProcessMaker 2: user interface, running cases and functionality

Moderator: amosbatto

Forum rules: Please search to see if a question has already been asked before posting. Please don't ask the same question in multiple forums.
#782938
The following amended function will return the UID of the generated output document, and also enable PDF security (which is presently missing):
Code: Select all
function PMFGenerateOutputDocument ($outputID, $sApplication = null, $index = null, $sUserLogged = null, $aPDFSecurity = array())
{
    $g = new G();

    $g->sessionVarSave();

    if ($sApplication) {
        $_SESSION["APPLICATION"] = $sApplication;
    } else {
        $sApplication = $_SESSION["APPLICATION"];
    }

    if ($index) {
        $_SESSION["INDEX"] = $index;
    } else {
        $index = $_SESSION["INDEX"];
    }

    if ($sUserLogged) {
        $_SESSION["USER_LOGGED"] = $sUserLogged;
    } else {
        $sUserLogged = $_SESSION["USER_LOGGED"];
    }

    G::LoadClass( 'case' );
    $oCase = new Cases();
    $oCase->thisIsTheCurrentUser( $sApplication, $index, $sUserLogged, '', 'casesListExtJs' );

    //require_once 'classes/model/OutputDocument.php';
    $oOutputDocument = new OutputDocument();
    $aOD = $oOutputDocument->load( $outputID );
    $Fields = $oCase->loadCase( $sApplication );
    //The $_GET['UID'] variable is used when a process executes.
    //$_GET['UID']=($aOD['OUT_DOC_VERSIONING'])?$_GET['UID']:$aOD['OUT_DOC_UID'];
    //$sUID = ($aOD['OUT_DOC_VERSIONING'])?$_GET['UID']:$aOD['OUT_DOC_UID'];
    $sFilename = preg_replace( '[^A-Za-z0-9_]', '_', G::replaceDataField( $aOD['OUT_DOC_FILENAME'], $Fields['APP_DATA'] ) );
    require_once 'classes/model/AppFolder.php';
    require_once 'classes/model/AppDocument.php';

    //Get the Custom Folder ID (create if necessary)
    $oFolder = new AppFolder();
    //$aOD['OUT_DOC_DESTINATION_PATH'] = ($aOD['OUT_DOC_DESTINATION_PATH']=='')?PATH_DOCUMENT
    //      . $_SESSION['APPLICATION'] . PATH_SEP . 'outdocs'. PATH_SEP:$aOD['OUT_DOC_DESTINATION_PATH'];
    $folderId = $oFolder->createFromPath( $aOD['OUT_DOC_DESTINATION_PATH'], $sApplication );
    //Tags
    $fileTags = $oFolder->parseTags( $aOD['OUT_DOC_TAGS'], $sApplication );

    //Get last Document Version and apply versioning if is enabled
    $oAppDocument = new AppDocument();
    $lastDocVersion = $oAppDocument->getLastDocVersion( $outputID, $sApplication );

    $oCriteria = new Criteria( 'workflow' );
    $oCriteria->add( AppDocumentPeer::APP_UID, $sApplication );
    //$oCriteria->add(AppDocumentPeer::DEL_INDEX,    $index);
    $oCriteria->add( AppDocumentPeer::DOC_UID, $outputID );
    $oCriteria->add( AppDocumentPeer::DOC_VERSION, $lastDocVersion );
    $oCriteria->add( AppDocumentPeer::APP_DOC_TYPE, 'OUTPUT' );
    $oDataset = AppDocumentPeer::doSelectRS( $oCriteria );
    $oDataset->setFetchmode( ResultSet::FETCHMODE_ASSOC );
    $oDataset->next();

    if (($aOD['OUT_DOC_VERSIONING']) && ($lastDocVersion != 0)) {
        //Create new Version of current output
        $lastDocVersion ++;
        if ($aRow = $oDataset->getRow()) {
            $aFields = array (  'APP_DOC_UID' => $aRow['APP_DOC_UID'],
                                'APP_UID' => $sApplication,
                                'DEL_INDEX' => $index,
                                'DOC_UID' => $outputID,
                                'DOC_VERSION' => $lastDocVersion + 1,
                                'USR_UID' => $sUserLogged,
                                'APP_DOC_TYPE' => 'OUTPUT',
                                'APP_DOC_CREATE_DATE' => date( 'Y-m-d H:i:s' ),
                                'APP_DOC_FILENAME' => $sFilename,
                                'APP_DOC_TITLE' => $aOD['OUT_DOC_TITLE'],                                 'FOLDER_UID' => $folderId,
                                'APP_DOC_TAGS' => $fileTags
            );
            $oAppDocument = new AppDocument();
            $oAppDocument->create( $aFields );
            $sDocUID = $aRow['APP_DOC_UID'];
        }
    } else {
        ////No versioning so Update a current Output or Create new if no exist
        if ($aRow = $oDataset->getRow()) {
            //Update
            $aFields = array (  'APP_DOC_UID' => $aRow['APP_DOC_UID'],
                                'APP_UID' => $sApplication,
                                'DEL_INDEX' => $index,
                                'DOC_UID' => $outputID,
                                'DOC_VERSION' => $lastDocVersion,
                                'USR_UID' => $sUserLogged,
                                'APP_DOC_TYPE' => 'OUTPUT',
                                'APP_DOC_CREATE_DATE' => date( 'Y-m-d H:i:s' ),
                                'APP_DOC_FILENAME' => $sFilename,
                                'APP_DOC_TITLE' => $aOD['OUT_DOC_TITLE'],
                                'FOLDER_UID' => $folderId,
                                'APP_DOC_TAGS' => $fileTags
            );
            $oAppDocument = new AppDocument();
            $oAppDocument->update( $aFields );
            $sDocUID = $aRow['APP_DOC_UID'];
        } else {
            //we are creating the appdocument row
            //create
            if ($lastDocVersion == 0) {
                $lastDocVersion ++;
            }
            $aFields = array (  'APP_UID' => $sApplication,
                                'DEL_INDEX' => $index,
                                'DOC_UID' => $outputID,
                                'DOC_VERSION' => $lastDocVersion,
                                'USR_UID' => $sUserLogged,
                                'APP_DOC_TYPE' => 'OUTPUT',
                                'APP_DOC_CREATE_DATE' => date( 'Y-m-d H:i:s' ),
                                'APP_DOC_FILENAME' => $sFilename,
                                'APP_DOC_TITLE' => $aOD['OUT_DOC_TITLE'],
                                'FOLDER_UID' => $folderId,
                                'APP_DOC_TAGS' => $fileTags
            );
            $oAppDocument = new AppDocument();
            $aFields['APP_DOC_UID'] = $sDocUID = $oAppDocument->create( $aFields );
        }
    }
    $sFilename = $aFields['APP_DOC_UID'] . "_" . $lastDocVersion;

    $pathOutput = PATH_DOCUMENT . $sApplication . PATH_SEP . 'outdocs' . PATH_SEP; //G::pr($sFilename);die;
    G::mk_dir( $pathOutput );

    $aProperties = array ();

    if (! isset( $aOD['OUT_DOC_MEDIA'] )) {
        $aOD['OUT_DOC_MEDIA'] = 'Letter';
    }
    if (! isset( $aOD['OUT_DOC_LEFT_MARGIN'] )) {
        $aOD['OUT_DOC_LEFT_MARGIN'] = '15';
    }
    if (! isset( $aOD['OUT_DOC_RIGHT_MARGIN'] )) {
        $aOD['OUT_DOC_RIGHT_MARGIN'] = '15';
    }
    if (! isset( $aOD['OUT_DOC_TOP_MARGIN'] )) {
        $aOD['OUT_DOC_TOP_MARGIN'] = '15';
    }
    if (! isset( $aOD['OUT_DOC_BOTTOM_MARGIN'] )) {
        $aOD['OUT_DOC_BOTTOM_MARGIN'] = '15';
    }

    $aProperties['media'] = $aOD['OUT_DOC_MEDIA'];
    $aProperties['margins'] = array ('left' => $aOD['OUT_DOC_LEFT_MARGIN'],'right' => $aOD['OUT_DOC_RIGHT_MARGIN'],'top' => $aOD['OUT_DOC_TOP_MARGIN'],'bottom' => $aOD['OUT_DOC_BOTTOM_MARGIN']);
    $aProperties['title'] = $aOD['OUT_DOC_TITLE'];
    $aProperties['description'] = $aOD['OUT_DOC_DESCRIPTION'];

    if ($aPDFSecurity['enabled'] == 1) {
        $aProperties['pdfSecurity'] = $aPDFSecurity;
    } else {
        $aProperties['pdfSecurity'] = array (
                                                'enabled'       => $aOD['OUT_DOC_PDF_SECURITY_ENABLED'],
                                                'openPassword'  => G::decrypt($aOD['OUT_DOC_PDF_SECURITY_OPEN_PASSWORD'], $outputID),
                                                'ownerPassword' => G::decrypt($aOD['OUT_DOC_PDF_SECURITY_OWNER_PASSWORD'], $outputID),
                                                'permissions'   => $aOD['OUT_DOC_PDF_SECURITY_PERMISSIONS'],
                                            );
    }

    if (isset($aOD['OUT_DOC_REPORT_GENERATOR'])) {
        $aProperties['report_generator'] = $aOD['OUT_DOC_REPORT_GENERATOR'];
    }
    $oOutputDocument->generate( $outputID, $Fields['APP_DATA'], $pathOutput, $sFilename, $aOD['OUT_DOC_TEMPLATE'], (boolean) $aOD['OUT_DOC_LANDSCAPE'], $aOD['OUT_DOC_GENERATE'], $aProperties );

    //Plugin Hook PM_UPLOAD_DOCUMENT for upload document
    //G::LoadClass('plugin');
    $oPluginRegistry = & PMPluginRegistry::getSingleton();
    if ($oPluginRegistry->existsTrigger( PM_UPLOAD_DOCUMENT ) && class_exists( 'uploadDocumentData' )) {
        $triggerDetail = $oPluginRegistry->getTriggerInfo( PM_UPLOAD_DOCUMENT );
        $aFields['APP_DOC_PLUGIN'] = $triggerDetail->sNamespace;

        $oAppDocument1 = new AppDocument();
        $oAppDocument1->update( $aFields );

        $sPathName = PATH_DOCUMENT . $sApplication . PATH_SEP;

        $oData['APP_UID'] = $sApplication;
        $oData['ATTACHMENT_FOLDER'] = true;
        switch ($aOD['OUT_DOC_GENERATE']) {
            case "BOTH":
                $documentData = new uploadDocumentData( $sApplication, $sUserLogged, $pathOutput . $sFilename . '.pdf', $sFilename . '.pdf', $sDocUID, $oAppDocument->getDocVersion() );

                $documentData->sFileType = "PDF";
                $documentData->bUseOutputFolder = true;
                $uploadReturn = $oPluginRegistry->executeTriggers( PM_UPLOAD_DOCUMENT, $documentData );
                if ($uploadReturn) {
                    //Only delete if the file was saved correctly
                    unlink( $pathOutput . $sFilename . '.pdf' );
                }

                $documentData = new uploadDocumentData( $sApplication, $sUserLogged, $pathOutput . $sFilename . '.doc', $sFilename . '.doc', $sDocUID, $oAppDocument->getDocVersion() );

                $documentData->sFileType = "DOC";
                $documentData->bUseOutputFolder = true;
                $uploadReturn = $oPluginRegistry->executeTriggers( PM_UPLOAD_DOCUMENT, $documentData );
                if ($uploadReturn) {
                    //Only delete if the file was saved correctly
                    unlink( $pathOutput . $sFilename . '.doc' );
                }

                break;
            case "PDF":
                $documentData = new uploadDocumentData( $sApplication, $sUserLogged, $pathOutput . $sFilename . '.pdf', $sFilename . '.pdf', $sDocUID, $oAppDocument->getDocVersion() );

                $documentData->sFileType = "PDF";
                $documentData->bUseOutputFolder = true;
                $uploadReturn = $oPluginRegistry->executeTriggers( PM_UPLOAD_DOCUMENT, $documentData );
                if ($uploadReturn) {
                    //Only delete if the file was saved correctly
                    unlink( $pathOutput . $sFilename . '.pdf' );
                }
                break;
            case "DOC":
                $documentData = new uploadDocumentData( $sApplication, $sUserLogged, $pathOutput . $sFilename . '.doc', $sFilename . '.doc', $sDocUID, $oAppDocument->getDocVersion() );

                $documentData->sFileType = "DOC";
                $documentData->bUseOutputFolder = true;
                $uploadReturn = $oPluginRegistry->executeTriggers( PM_UPLOAD_DOCUMENT, $documentData );
                if ($uploadReturn) {
                    //Only delete if the file was saved correctly
                    unlink( $pathOutput . $sFilename . '.doc' );
                }
                break;
        }
    }
    $g->sessionVarRestore();
    return $aFields['APP_DOC_UID'];
}

Thanks!

Hi, This user API https://wiki.processmaker.com/[…]

display pdf in panel control

Is there any solution?