From d683944fedeb828469365a36b02632292d69fd0a Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Wed, 12 Sep 2018 20:59:49 +0200
Subject: [PATCH] add auto convert document when uploaded

---
 apps/maarch_entreprise/actions/process.php    |  4 +-
 .../indexing_searching/file_iframe.php        | 28 ++++++++++++
 .../controllers/ConvertPdfController.php      |  7 ++-
 .../resource/controllers/ResController.php    | 43 +++++++++++++++++--
 src/app/resource/models/ResModelAbstract.php  | 20 +++++++++
 5 files changed, 95 insertions(+), 7 deletions(-)

diff --git a/apps/maarch_entreprise/actions/process.php b/apps/maarch_entreprise/actions/process.php
index ad807708274..e4d045f3978 100755
--- a/apps/maarch_entreprise/actions/process.php
+++ b/apps/maarch_entreprise/actions/process.php
@@ -766,9 +766,7 @@ function get_form_txt($values, $path_manage_action, $id_action, $table, $module,
         $watermark_outgoing = 'false';
     }
 
-    $frm_str .= '<iframe src="'.$_SESSION['config']['businessappurl']
-        .'index.php?display=true&dir=indexing_searching&page=view_resource_controler&id='
-        .$res_id.'&watermark_outgoing='.$watermark_outgoing.'" name="viewframe" id="viewframe" scrolling="auto" frameborder="0" width="100%" style="width:100% !important;"></iframe>';
+    $frm_str .= '<iframe src="../../rest/res/'.$res_id.'/content" name="viewframe" id="viewframe" scrolling="auto" frameborder="0" width="100%" style="width:100% !important;"></iframe>';
 
     $frm_str .= '</div>';
 
diff --git a/apps/maarch_entreprise/indexing_searching/file_iframe.php b/apps/maarch_entreprise/indexing_searching/file_iframe.php
index ee55f826fff..659e28a27c4 100755
--- a/apps/maarch_entreprise/indexing_searching/file_iframe.php
+++ b/apps/maarch_entreprise/indexing_searching/file_iframe.php
@@ -155,6 +155,7 @@ if (isset($_GET['num'])) {
         }
     }
 } else {
+    
     $extension = explode('.', $_SESSION['upfile']['name']);
     $count_level = count($extension) - 1;
     $the_ext = $extension[$count_level];
@@ -215,6 +216,33 @@ if (isset($_GET['num'])) {
         && !empty($_SESSION['upfile']['format'])
         && $_SESSION['upfile']['error'] != 1
     ) {
+        $tmpFilename = pathinfo($_SESSION['upfile']['local_path']);
+        if ($tmpFilename['extension'] != 'pdf') {
+            $return = \Convert\controllers\ConvertPdfController::tmpConvert([
+                'fullFilename'     => $_SESSION['upfile']['local_path'],
+            ]);
+
+            if (empty($return['errors'])) {
+                $mimeType = $is->get_mime_type('pdf');
+                //print_r($_SESSION['upfile']);exit;
+                header('Pragma: public');
+                header('Expires: 0');
+                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
+                header('Cache-Control: public');
+                header('Content-Description: File Transfer');
+                header('Content-Type: '.$mimeType);
+                header(
+                    'Content-Disposition: inline; filename='.basename('maarch').'.'
+                    .$ext.';'
+                );
+                header('Content-Transfer-Encoding: binary');
+
+                $loc = $return['fullFilename'];
+                readfile($loc);
+
+                exit();
+            }
+        }
         if ($showFile) {
             $mimeType = $is->get_mime_type($_SESSION['upfile']['format']);
             //print_r($_SESSION['upfile']);exit;
diff --git a/src/app/convert/controllers/ConvertPdfController.php b/src/app/convert/controllers/ConvertPdfController.php
index 5de3b2f62d1..5e6bc402ece 100644
--- a/src/app/convert/controllers/ConvertPdfController.php
+++ b/src/app/convert/controllers/ConvertPdfController.php
@@ -58,7 +58,12 @@ class ConvertPdfController
         ValidatorModel::intVal($aArgs, ['resId']);
         ValidatorModel::boolType($aArgs, ['isVersion']);
 
-        $resource = AttachmentModel::getById(['id' => $aArgs['resId'], 'isVersion' => $aArgs['isVersion'], 'select' => ['docserver_id', 'path', 'filename']]);
+        if ($aArgs['collId'] == 'letterbox_coll') {
+            $resource = ResModel::getById(['resId' => $aArgs['resId'], 'select' => ['docserver_id', 'path', 'filename']]);
+        } else {
+            $resource = AttachmentModel::getById(['id' => $aArgs['resId'], 'isVersion' => $aArgs['isVersion'], 'select' => ['docserver_id', 'path', 'filename']]);
+        }
+        
         
         if (empty($resource)) {
             return ['errors' => '[ConvertPdf] Resource does not exist'];
diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index 19bc01c59cc..4219d796a8f 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -39,6 +39,7 @@ use SrcCore\controllers\PreparedClauseController;
 use User\models\UserModel;
 use Docserver\models\ResDocserverModel;
 use Resource\models\ChronoModel;
+use Convert\controllers\ConvertPdfController;
 
 class ResController
 {
@@ -277,15 +278,51 @@ class ResController
                 $attachmentTodisplay = $attachment[0];
                 $id = (empty($attachmentTodisplay['res_id']) ? $attachmentTodisplay['res_id_version'] : $attachmentTodisplay['res_id']);
                 $isVersion = empty($attachmentTodisplay['res_id']);
-
-                $convertedAttachment = AttachmentModel::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'id' => $id, 'isVersion' => $isVersion]);
-                if (!empty($convertedAttachment)) {
+                if ($isVersion) {
+                    $collId = "attachments_version_coll";
+                } else {
+                    $collId = "attachments_coll";
+                }
+                $convertedAttachment = AttachmentModel::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $id, 'isVersion' => $isVersion]);
+                if (empty($convertedAttachment)) {
+                    ConvertPdfController::convert([
+                        'resId'     => $id,
+                        'collId'    => $collId,
+                        'isVersion' => $isVersion,
+                    ]);
+        
+                    $convertedAttachment = AttachmentModel::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $id, 'isVersion' => $isVersion]);
+                    
+                    if (!empty($convertedAttachment)) {
+                        $attachmentTodisplay = $convertedAttachment;
+                    }
+                } else {
                     $attachmentTodisplay = $convertedAttachment;
                 }
                 $document['docserver_id'] = $attachmentTodisplay['docserver_id'];
                 $document['path'] = $attachmentTodisplay['path'];
                 $document['filename'] = $attachmentTodisplay['filename'];
             }
+        } else {
+            $convertedDocument = ResModel::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $aArgs['resId']]);
+
+            if (empty($convertedDocument)) {
+                ConvertPdfController::convert([
+                    'resId'     => $aArgs['resId'],
+                    'collId'    => 'letterbox_coll',
+                ]);
+    
+                $convertedDocument = ResModel::getConvertedPdfById(['select' => ['docserver_id', 'path', 'filename'], 'resId' => $aArgs['resId']]);
+                
+                if (!empty($convertedDocument)) {
+                    $documentTodisplay = $convertedDocument;
+                }
+            } else {
+                $documentTodisplay = $convertedDocument;
+            }
+            $document['docserver_id'] = $documentTodisplay['docserver_id'];
+            $document['path'] = $documentTodisplay['path'];
+            $document['filename'] = $documentTodisplay['filename'];
         }
 
         $docserver = DocserverModel::getByDocserverId(['docserverId' => $document['docserver_id'], 'select' => ['path_template']]);
diff --git a/src/app/resource/models/ResModelAbstract.php b/src/app/resource/models/ResModelAbstract.php
index 61529c26970..48ac92d8187 100644
--- a/src/app/resource/models/ResModelAbstract.php
+++ b/src/app/resource/models/ResModelAbstract.php
@@ -433,4 +433,24 @@ abstract class ResModelAbstract
 
         return $natures;
     }
+
+    public static function getConvertedPdfById(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['resId']);
+        ValidatorModel::intVal($aArgs, ['resId']);
+        ValidatorModel::arrayType($aArgs, ['select']);
+
+        $document = DatabaseModel::select([
+            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
+            'table'     => ['adr_letterbox'],
+            'where'     => ['res_id = ?', 'type = ?'],
+            'data'      => [$aArgs['resId'], 'PDF'],
+        ]);
+
+        if (empty($document[0])) {
+            return [];
+        }
+
+        return $document[0];
+    }
 }
-- 
GitLab