Beispiel #1
0
 /**
  * Get data of Cases InputDocument
  *
  * @param string $applicationUid
  * @param string $taskUid
  * @param string $appDocComment
  * @param string $inputDocumentUid
  * @param string $userUid
  *
  * return array Return an array with data of an InputDocument
  */
 public function addCasesInputDocument($applicationUid, $taskUid, $appDocComment, $inputDocumentUid, $userUid)
 {
     try {
         if ((isset( $_FILES['form'] )) && ($_FILES['form']['error'] != 0)) {
             $code = $_FILES['form']['error'];
             switch ($code) {
                 case UPLOAD_ERR_INI_SIZE:
                     $message = \G::LoadTranslation( 'ID_UPLOAD_ERR_INI_SIZE' );
                     break;
                 case UPLOAD_ERR_FORM_SIZE:
                     $message = \G::LoadTranslation( 'ID_UPLOAD_ERR_FORM_SIZE' );
                     break;
                 case UPLOAD_ERR_PARTIAL:
                     $message = \G::LoadTranslation( 'ID_UPLOAD_ERR_PARTIAL' );
                     break;
                 case UPLOAD_ERR_NO_FILE:
                     $message = \G::LoadTranslation( 'ID_UPLOAD_ERR_NO_FILE' );
                     break;
                 case UPLOAD_ERR_NO_TMP_DIR:
                     $message = \G::LoadTranslation( 'ID_UPLOAD_ERR_NO_TMP_DIR' );
                     break;
                 case UPLOAD_ERR_CANT_WRITE:
                     $message = \G::LoadTranslation( 'ID_UPLOAD_ERR_CANT_WRITE' );
                     break;
                 case UPLOAD_ERR_EXTENSION:
                     $message = \G::LoadTranslation( 'ID_UPLOAD_ERR_EXTENSION' );
                     break;
                 default:
                     $message = \G::LoadTranslation( 'ID_UPLOAD_ERR_UNKNOWN' );
                     break;
             }
             \G::SendMessageText( $message, "ERROR" );
             $backUrlObj = explode( "sys" . SYS_SYS, $_SERVER['HTTP_REFERER'] );
             \G::header( "location: " . "/sys" . SYS_SYS . $backUrlObj[1] );
             die();
         }
         \G::LoadClass("case");
         $appDocUid = \G::generateUniqueID();
         $docVersion = '';
         $appDocType = 'INPUT';
         $case = new \Cases();
         $delIndex = \AppDelegation::getCurrentIndex($applicationUid);
         $case->thisIsTheCurrentUser($applicationUid, $delIndex, $userUid, "REDIRECT", "casesListExtJs");
         //Load the fields
         $arrayField = $case->loadCase($applicationUid);
         $arrayField["APP_DATA"] = array_merge($arrayField["APP_DATA"], \G::getSystemConstants());
         //Triggers
         $arrayTrigger = $case->loadTriggers($taskUid, "INPUT_DOCUMENT", $inputDocumentUid, "AFTER");
         //Add Input Document
         if (empty($_FILES)) {
             throw new \Exception(\G::LoadTranslation("ID_CASES_INPUT_FILENAME_DOES_NOT_EXIST"));
         }
         if (!$_FILES["form"]["error"]) {
             $_FILES["form"]["error"] = 0;
         }
         if (isset($_FILES) && isset($_FILES["form"]) && count($_FILES["form"]) > 0) {
             $appDocUid = $case->addInputDocument($inputDocumentUid,
                 $appDocUid,
                 $docVersion,
                 $appDocType,
                 $appDocComment,
                 '',
                 $applicationUid,
                 $delIndex,
                 $taskUid,
                 $userUid,
                 "xmlform",
                 $_FILES["form"]["name"],
                 $_FILES["form"]["error"],
                 $_FILES["form"]["tmp_name"]);
         }
         //Trigger - Execute after - Start
         $arrayField["APP_DATA"] = $case->executeTriggers ($taskUid,
             "INPUT_DOCUMENT",
             $inputDocumentUid,
             "AFTER",
             $arrayField["APP_DATA"]);
         //Trigger - Execute after - End
         //Save data
         $arrayData = array();
         $arrayData["APP_NUMBER"] = $arrayField["APP_NUMBER"];
         //$arrayData["APP_PROC_STATUS"] = $arrayField["APP_PROC_STATUS"];
         $arrayData["APP_DATA"]  = $arrayField["APP_DATA"];
         $arrayData["DEL_INDEX"] = $delIndex;
         $arrayData["TAS_UID"]   = $taskUid;
         $case->updateCase($applicationUid, $arrayData);
         return($this->getCasesInputDocument($applicationUid, $userUid, $appDocUid));
     } catch (\Exception $e) {
         throw $e;
     }
 }
    }
    G::SendMessageText($message, "ERROR");
    $backUrlObj = explode("sys" . SYS_SYS, $_SERVER['HTTP_REFERER']);
    G::header("location: " . "/sys" . SYS_SYS . $backUrlObj[1]);
    die;
}
G::LoadClass("case");
$inputDocumentUid = $_GET["UID"];
//$_POST["form"]["DOC_UID"]
$appDocUid = $_POST["form"]["APP_DOC_UID"];
$docVersion = intval($_POST["form"]["docVersion"]);
$appDocType = $_POST["form"]["APP_DOC_TYPE"];
$appDocComment = isset($_POST["form"]["APP_DOC_COMMENT"]) ? $_POST["form"]["APP_DOC_COMMENT"] : "";
$actionType = $_POST["form"]["actionType"];
$case = new Cases();
$case->thisIsTheCurrentUser($_SESSION["APPLICATION"], $_SESSION["INDEX"], $_SESSION["USER_LOGGED"], "REDIRECT", "casesListExtJs");
//Load the fields
$arrayField = $case->loadCase($_SESSION["APPLICATION"]);
$arrayField["APP_DATA"] = array_merge($arrayField["APP_DATA"], G::getSystemConstants());
//Triggers
$arrayTrigger = $case->loadTriggers($_SESSION["TASK"], "INPUT_DOCUMENT", $inputDocumentUid, "AFTER");
//Trigger debug routines
//Cleaning debug variables
$_SESSION["TRIGGER_DEBUG"]["ERRORS"] = array();
$_SESSION["TRIGGER_DEBUG"]["DATA"] = array();
$_SESSION["TRIGGER_DEBUG"]["TRIGGERS_NAMES"] = array();
$_SESSION["TRIGGER_DEBUG"]["TRIGGERS_VALUES"] = array();
$_SESSION["TRIGGER_DEBUG"]["NUM_TRIGGERS"] = count($arrayTrigger);
$_SESSION["TRIGGER_DEBUG"]["TIME"] = "AFTER";
if ($_SESSION["TRIGGER_DEBUG"]["NUM_TRIGGERS"] > 0) {
    $_SESSION["TRIGGER_DEBUG"]["TRIGGERS_NAMES"] = $case->getTriggerNames($arrayTrigger);
 /**
  * Get data of Cases OutputDocument
  *
  * @param string $applicationUid
  * @param string $outputDocumentUid
  * @param string $userUid
  *
  * return object Return an object with data of an OutputDocument
  */
 public function addCasesOutputDocument($applicationUid, $outputDocumentUid, $userUid)
 {
     try {
         $sApplication = $applicationUid;
         $index = \AppDelegation::getCurrentIndex($applicationUid);
         $sUserLogged = $userUid;
         $outputID = $outputDocumentUid;
         $g = new \G();
         $g->sessionVarSave();
         \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 );
         $sFilename = preg_replace( '[^A-Za-z0-9_]', '_', \G::replaceDataField( $aOD['OUT_DOC_FILENAME'], $Fields['APP_DATA'] ) );
         require_once (PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "AppFolder.php");
         require_once (PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "AppDocument.php");
         //Get the Custom Folder ID (create if necessary)
         $oFolder = new \AppFolder();
         $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::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,'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,'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,'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 . \G::getPathFromUID($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']
         );
         if (isset($aOD['OUT_DOC_REPORT_GENERATOR'])) {
             $aProperties['report_generator'] = $aOD['OUT_DOC_REPORT_GENERATOR'];
         }
         $this->generate( $outputID, $Fields['APP_DATA'], $pathOutput, $sFilename, $aOD['OUT_DOC_TEMPLATE'], (boolean) $aOD['OUT_DOC_LANDSCAPE'], $aOD['OUT_DOC_GENERATE'], $aProperties , $applicationUid);
         //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 . \G::getPathFromUID($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();
         $oAppDocument = \AppDocumentPeer::retrieveByPK( $aFields['APP_DOC_UID'], $lastDocVersion);
         if ($oAppDocument->getAppDocStatus() == 'DELETED') {
             $oAppDocument->setAppDocStatus('ACTIVE');
             $oAppDocument->save();
         }
         $response = $this->getCasesOutputDocument($applicationUid, $userUid, $aFields['APP_DOC_UID']);
         return $response;
     } catch (\Exception $e) {
         throw $e;
     }
 }
            $message = G::LoadTranslation('ID_UPLOAD_ERR_UNKNOWN');
            break;
    }
    G::SendMessageText($message, "ERROR");
    $backUrlObj = explode("sys" . SYS_SYS, $_SERVER['HTTP_REFERER']);
    G::header("location: " . "/sys" . SYS_SYS . $backUrlObj[1]);
    die;
}
$docUid = $_POST['form']['DOC_UID'];
$appDocUid = $_POST['form']['APP_DOC_UID'];
$docVersion = $_POST['form']['docVersion'];
$actionType = $_POST['form']['actionType'];
//load the variables
G::LoadClass('case');
$oCase = new Cases();
$oCase->thisIsTheCurrentUser($_SESSION['APPLICATION'], $_SESSION['INDEX'], $_SESSION['USER_LOGGED'], 'REDIRECT', 'cases_List');
$Fields = $oCase->loadCase($_SESSION['APPLICATION']);
$Fields['APP_DATA'] = array_merge($Fields['APP_DATA'], G::getSystemConstants());
#trigger debug routines...
//cleaning debug variables
$_SESSION['TRIGGER_DEBUG']['ERRORS'] = array();
$_SESSION['TRIGGER_DEBUG']['DATA'] = array();
$_SESSION['TRIGGER_DEBUG']['TRIGGERS_NAMES'] = array();
$_SESSION['TRIGGER_DEBUG']['TRIGGERS_VALUES'] = array();
$triggers = $oCase->loadTriggers($_SESSION['TASK'], 'INPUT_DOCUMENT', $_GET['UID'], 'AFTER');
$_SESSION['TRIGGER_DEBUG']['NUM_TRIGGERS'] = count($triggers);
$_SESSION['TRIGGER_DEBUG']['TIME'] = 'AFTER';
if ($_SESSION['TRIGGER_DEBUG']['NUM_TRIGGERS'] != 0) {
    $_SESSION['TRIGGER_DEBUG']['TRIGGERS_NAMES'] = $oCase->getTriggerNames($triggers);
    $_SESSION['TRIGGER_DEBUG']['TRIGGERS_VALUES'] = $triggers;
}
/**
 *
 * @method
 *
 * Generates an Output Document
 *
 * @name PMFGenerateOutputDocument
 * @label PMF Generate Output Document
 *
 * @param string(32) | $outputID | Output ID | Output Document ID
 * @return none | $none | None | None
 *
 */
function PMFGenerateOutputDocument($outputID, $sApplication = null, $index = null, $sUserLogged = null)
{
    $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, '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, '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, '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 . G::getPathFromUID($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']);
    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'], (bool) $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 . G::getPathFromUID($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();
}
 /**
  * Get data of Cases OutputDocument
  *
  * @param string $applicationUid
  * @param string $outputDocumentUid
  * @param string $userUid
  *
  * return object Return an object with data of an OutputDocument
  */
 public function addCasesOutputDocument($applicationUid, $outputDocumentUid, $userUid)
 {
     try {
         $sApplication = $applicationUid;
         $index = \AppDelegation::getCurrentIndex($applicationUid);
         $sUserLogged = $userUid;
         $outputID = $outputDocumentUid;
         $g = new \G();
         $g->sessionVarSave();
         \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);
         $sFilename = preg_replace('[^A-Za-z0-9_]', '_', \G::replaceDataField($aOD['OUT_DOC_FILENAME'], $Fields['APP_DATA']));
         require_once PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "AppFolder.php";
         require_once PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes" . PATH_SEP . "model" . PATH_SEP . "AppDocument.php";
         //Get the Custom Folder ID (create if necessary)
         $oFolder = new \AppFolder();
         $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::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, '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, '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, '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 . \G::getPathFromUID($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']);
         if (isset($aOD['OUT_DOC_REPORT_GENERATOR'])) {
             $aProperties['report_generator'] = $aOD['OUT_DOC_REPORT_GENERATOR'];
         }
         $this->generate($outputID, $Fields['APP_DATA'], $pathOutput, $sFilename, $aOD['OUT_DOC_TEMPLATE'], (bool) $aOD['OUT_DOC_LANDSCAPE'], $aOD['OUT_DOC_GENERATE'], $aProperties, $applicationUid);
         /*----------------------------------********---------------------------------*/
         $licensedFeatures =& \PMLicensedFeatures::getSingleton();
         if ($licensedFeatures->verifyfeature('7qhYmF1eDJWcEdwcUZpT0k4S0xTRStvdz09')) {
             \G::LoadClass("pmDrive");
             $pmDrive = new \PMDrive();
             if ($pmDrive->getStatusService()) {
                 $app = new \Application();
                 $user = new \Users();
                 $dataUser = $user->load($userUid);
                 $pmDrive->setDriveUser($dataUser['USR_EMAIL']);
                 $appData = $app->Load($applicationUid);
                 if ($appData['APP_DRIVE_FOLDER_UID'] == null) {
                     $process = new \Process();
                     $process->setProUid($appData['PRO_UID']);
                     $result = $pmDrive->createFolder($process->getProTitle() . ' - ' . G::LoadTranslation("ID_CASE") . ' #' . $appData['APP_NUMBER'], $pmDrive->getFolderIdPMDrive($userUid));
                     $appData['APP_DRIVE_FOLDER_UID'] = $result->id;
                     $app->update($appData);
                 }
                 $fileIdDriveDoc = '';
                 $fileIdDrivePdf = '';
                 $sFilenameOriginal = preg_replace('[^A-Za-z0-9_]', '_', \G::replaceDataField($aOD['OUT_DOC_FILENAME'], $Fields['APP_DATA']));
                 switch ($aOD['OUT_DOC_GENERATE']) {
                     case "BOTH":
                         $result = $pmDrive->uploadFile('application/pdf', $pathOutput . $sFilename . '.pdf', $sFilenameOriginal . '.pdf', $appData['APP_DRIVE_FOLDER_UID']);
                         $oAppDocument->setDriveDownload('OUTPUT_PDF', $result->webContentLink);
                         $fileIdDrivePdf = $result->id;
                         $result = $pmDrive->uploadFile('application/doc', $pathOutput . $sFilename . '.doc', $sFilenameOriginal . '.doc', $appData['APP_DRIVE_FOLDER_UID']);
                         $oAppDocument->setDriveDownload('OUTPUT_DOC', $result->webContentLink);
                         $fileIdDriveDoc = $result->id;
                         break;
                     case "PDF":
                         $result = $pmDrive->uploadFile('application/pdf', $pathOutput . $sFilename . '.pdf', $sFilenameOriginal . '.pdf', $appData['APP_DRIVE_FOLDER_UID']);
                         $oAppDocument->setDriveDownload('OUTPUT_PDF', $result->webContentLink);
                         $fileIdDrivePdf = $result->id;
                         break;
                     case "DOC":
                         $result = $pmDrive->uploadFile('application/doc', $pathOutput . $sFilename . '.doc', $sFilenameOriginal . '.doc', $appData['APP_DRIVE_FOLDER_UID']);
                         $oAppDocument->setDriveDownload('OUTPUT_DOC', $result->webContentLink);
                         $fileIdDriveDoc = $result->id;
                         break;
                 }
                 $aFields['DOC_VERSION'] = $oAppDocument->getDocVersion();
                 // $docVersion;
                 $aFields['APP_DOC_UID'] = $oAppDocument->getAppDocUid();
                 //$appDocUid;
                 $appDocUid = $aFields['APP_DOC_UID'];
                 $oAppDocument->update($aFields);
                 //$option = 'pmDrive';
                 //add permissions
                 $criteria = new \Criteria('workflow');
                 $criteria->addSelectColumn(\ApplicationPeer::PRO_UID);
                 $criteria->addSelectColumn(\TaskUserPeer::TAS_UID);
                 $criteria->addSelectColumn(\TaskUserPeer::USR_UID);
                 $criteria->addSelectColumn(\TaskUserPeer::TU_RELATION);
                 $criteria->add(\ApplicationPeer::APP_UID, $applicationUid);
                 $criteria->addJoin(\ApplicationPeer::PRO_UID, \TaskPeer::PRO_UID, \Criteria::LEFT_JOIN);
                 $criteria->addJoin(\TaskPeer::TAS_UID, \TaskUserPeer::TAS_UID, \Criteria::LEFT_JOIN);
                 $dataset = \ApplicationPeer::doSelectRS($criteria);
                 $dataset->setFetchmode(\ResultSet::FETCHMODE_ASSOC);
                 $userPermission = array();
                 $case = new \Cases();
                 while ($dataset->next()) {
                     $row = $dataset->getRow();
                     if ($row['TU_RELATION'] == 1) {
                         //users
                         $dataUser = $user->load($row['USR_UID']);
                         if (array_search($dataUser['USR_EMAIL'], $userPermission) === false) {
                             $objectPermissions = $case->getAllObjects($row['PRO_UID'], $applicationUid, $row['TAS_UID'], $row['USR_UID']);
                             if (array_search($appDocUid, $objectPermissions['OUTPUT_DOCUMENTS']) !== false) {
                                 $userPermission[] = $dataUser['USR_EMAIL'];
                             }
                         }
                     } else {
                         //Groups
                         $criteria = new \Criteria('workflow');
                         $criteria->addSelectColumn(\UsersPeer::USR_EMAIL);
                         $criteria->addSelectColumn(\UsersPeer::USR_UID);
                         $criteria->add(\GroupUserPeer::GRP_UID, $row['USR_UID']);
                         $criteria->addJoin(\GroupUserPeer::USR_UID, \UsersPeer::USR_UID, \Criteria::LEFT_JOIN);
                         $oDataset = \AppDelegationPeer::doSelectRS($criteria);
                         $oDataset->setFetchmode(\ResultSet::FETCHMODE_ASSOC);
                         while ($oDataset->next()) {
                             $aRow = $oDataset->getRow();
                             if (array_search($aRow['USR_EMAIL'], $userPermission) === false) {
                                 $objectPermissions = $case->getAllObjects($row['PRO_UID'], $applicationUid, $row['TAS_UID'], $aRow['USR_UID']);
                                 if (array_search($appDocUid, $objectPermissions['OUTPUT_DOCUMENTS']) !== false) {
                                     $userPermission[] = $aRow['USR_EMAIL'];
                                 }
                             }
                         }
                     }
                 }
                 $userPermission = array_unique($userPermission);
                 foreach ($userPermission as $key => $val) {
                     $pmDrive->setPermission($appData['APP_DRIVE_FOLDER_UID'], $val, 'user', 'writer');
                     $pmDrive->setPermission($fileIdDrivePdf, $val);
                     $pmDrive->setPermission($fileIdDriveDoc, $val);
                 }
             }
         }
         /*----------------------------------********---------------------------------*/
         //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 . \G::getPathFromUID($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();
         $oAppDocument = \AppDocumentPeer::retrieveByPK($aFields['APP_DOC_UID'], $lastDocVersion);
         if ($oAppDocument->getAppDocStatus() == 'DELETED') {
             $oAppDocument->setAppDocStatus('ACTIVE');
             $oAppDocument->save();
         }
         $response = $this->getCasesOutputDocument($applicationUid, $userUid, $aFields['APP_DOC_UID']);
         return $response;
     } catch (\Exception $e) {
         throw $e;
     }
 }