From 49f8e676cf9c231613b8ff1ff230eca71220ff2d Mon Sep 17 00:00:00 2001
From: Alex Orluc <alex.orluc@maarch.org>
Date: Wed, 18 Oct 2017 12:37:40 +0200
Subject: [PATCH] FEAT #6023 fix button editing in progress + view pdf format

---
 .../indexing_searching/file_iframe.php        |  22 +++
 modules/attachments/attachments_content.php   |   1 +
 modules/attachments/choose_attachment.php     | 150 ++++++++----------
 .../content_management/checkEditingDoc.php    |   2 +-
 .../class_content_manager_tools_Abstract.php  |   1 -
 modules/content_management/js/functions.js    |   2 +
 .../save_attach_res_from_cm.php               |   6 +-
 7 files changed, 99 insertions(+), 85 deletions(-)

diff --git a/apps/maarch_entreprise/indexing_searching/file_iframe.php b/apps/maarch_entreprise/indexing_searching/file_iframe.php
index e20d9b5bce9..dba90d983e1 100755
--- a/apps/maarch_entreprise/indexing_searching/file_iframe.php
+++ b/apps/maarch_entreprise/indexing_searching/file_iframe.php
@@ -42,6 +42,7 @@ $the_ext = $extension[$count_level];
 $_SESSION['upfile']['format'] = $the_ext;
 
 $extList = $is->filetypes_showed_indexation();
+
 if (isset($_SESSION['upfile']['format'])) {
     $showFile = $is->show_index_frame($_SESSION['upfile']['format']);
     $ext = strtolower($_SESSION['upfile']['format']);
@@ -74,6 +75,27 @@ if ($_SESSION['origin'] == "scan") {
 		echo "<br/>PROBLEM DURING FILE SEND";
 	}
 	exit();
+}else if (!empty($_SESSION['upfile']['fileNamePdfOnTmp'])) {
+	$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");
+	$ext = strtolower('pdf');
+	if (file_exists($_SESSION['upfile']['local_path'])) {
+		$loc = $_SESSION['config']['tmppath'] . $_SESSION['upfile']['fileNamePdfOnTmp'];
+		readfile($loc);
+	}
+	exit();
+
 } else if (isset($_SESSION['upfile']['mime'])
     && ! empty($_SESSION['upfile']['mime'])
     && isset($_SESSION['upfile']['format'])
diff --git a/modules/attachments/attachments_content.php b/modules/attachments/attachments_content.php
index 77797f82935..560d4dc04ea 100755
--- a/modules/attachments/attachments_content.php
+++ b/modules/attachments/attachments_content.php
@@ -1532,6 +1532,7 @@ if (isset($_REQUEST['id'])) {
     $_SESSION['upfile']['format']        = $viewResourceArr['ext'];
     $fileNameOnTmp                       = str_replace($viewResourceArr['tmp_path'].DIRECTORY_SEPARATOR, '', $viewResourceArr['file_path']);
     $_SESSION['upfile']['fileNameOnTmp'] = $fileNameOnTmp;
+    $_SESSION['upfile']['fileNamePdfOnTmp'] = $viewResourceArr['filenamePdf'];
 
 } else {
     $_SESSION['targetAttachment'] = 'add';
diff --git a/modules/attachments/choose_attachment.php b/modules/attachments/choose_attachment.php
index 7c6605c1efd..c619cab54dc 100755
--- a/modules/attachments/choose_attachment.php
+++ b/modules/attachments/choose_attachment.php
@@ -1,30 +1,12 @@
 <?php
-
-/*
-*   Copyright 2008-2015 Maarch
-*
-*   This file is part of Maarch Framework.
-*
-*   Maarch Framework is free software: you can redistribute it and/or modify
-*   it under the terms of the GNU General Public License as published by
-*   the Free Software Foundation, either version 3 of the License, or
-*   (at your option) any later version.
-*
-*   Maarch Framework is distributed in the hope that it will be useful,
-*   but WITHOUT ANY WARRANTY; without even the implied warranty of
-*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-*   GNU General Public License for more details.
-*
-*   You should have received a copy of the GNU General Public License
-*   along with Maarch Framework.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
 /**
-* @brief  Frame to choose a file to index
-*
-* @file choose_attachment.php
-* @date $date$
-* @version $Revision$
+* Copyright Maarch since 2008 under licence GPLv3.
+* See LICENCE.txt file at the root folder for more details.
+* This file is part of Maarch software.
+
+* @brief   choose_attachment
+* @author  dev <dev@maarch.org>
+* @ingroup core
 */
 
 $core_tools = new core_tools();
@@ -34,18 +16,18 @@ $core_tools->load_html();
 $core_tools->load_header('', true, false);
 $upFileOK = false;
 ?>
-    <body>
+    
     <?php
     $_SESSION['upfile']['error'] = 0;
     if (isset($_FILES['file']['error']) && $_FILES['file']['error'] == 1) {
         $_SESSION['upfile']['error'] = $_FILES['file']['error'];
         if ($_SESSION['upfile']['error'] == 1) {
- 			$_SESSION['error'] = 'La taille du fichier telecharge excede la valeur de upload_max_filesize';
+            $_SESSION['error'] = 'La taille du fichier telecharge excede la valeur de upload_max_filesize';
         }
     } elseif (!empty($_FILES['file']['tmp_name']) && $_FILES['file']['error'] <> 1) {
         $_SESSION['error'] = '';
-    	$_SESSION['upfile']['tmp_name'] = $_FILES['file']['tmp_name'];
-        $extension = explode(".",$_FILES['file']['name']);
+        $_SESSION['upfile']['tmp_name'] = $_FILES['file']['tmp_name'];
+        $extension = explode(".", $_FILES['file']['name']);
         $name_without_ext = substr($_FILES['file']['name'], 0, strrpos($_FILES['file']['name'], "."));
         echo '<script>window.parent.document.getElementById(\'title\').value=\''.$name_without_ext.'\';</script>';
         $count_level = count($extension)-1;
@@ -61,7 +43,7 @@ $upFileOK = false;
                     . $_SESSION['config']['adminmail'] . "\">"
                     . $_SESSION['config']['adminname'] . "</a>)";
         } else {
-            require_once 'core/docservers_tools.php';
+            include_once 'core/docservers_tools.php';
             $arrayIsAllowed = array();
             $arrayIsAllowed = Ds_isFileTypeAllowed($_FILES['file']['tmp_name'], strtolower($the_ext));
             if ($arrayIsAllowed['status'] == false) {
@@ -80,11 +62,14 @@ $upFileOK = false;
                 $_SESSION['upfile']['name'] = $fileNameOnTmp;
                 $_SESSION['upfile']['format'] = $the_ext;
                 $upFileOK = true;
-                echo '<script>window.parent.document.getElementById(\'viewframevalid_attachment\').src=\''.$_SESSION['config']['businessappurl'].'index.php?display=true&dir=indexing_searching&page=file_iframe&#navpanes=0'.$_SESSION['upfile']['local_path'].'\';</script>';
+                if (strtolower($_SESSION['upfile']['format']) == 'pdf') {
+                    echo '<script>window.parent.document.getElementById(\'viewframevalid_attachment\').src=\''.$_SESSION['config']['businessappurl'].'index.php?display=true&dir=indexing_searching&page=file_iframe&#navpanes=0'.$_SESSION['upfile']['local_path'].'\';</script>';
+                }
             }
         }
     }
     ?>
+    <body>
     <form name="select_file_form" id="select_file_form" method="get" enctype="multipart/form-data" action="<?php
         echo $_SESSION['config']['businessappurl'];
         ?>index.php?display=true&module=attachments&page=choose_attachment" class="forms">
@@ -92,54 +77,7 @@ $upFileOK = false;
         <input type="hidden" name="dir" value="indexing_searching" />
         <input type="hidden" name="page" value="choose_attachment" />
         <?php
-            if (!empty($_SESSION['upfile']['local_path']) && empty($_SESSION['error'])) { 
-                //launch auto convert in PDF
-                if (
-                    strtolower($_SESSION['upfile']['format']) == 'odt' ||
-                    strtolower($_SESSION['upfile']['format']) == 'docx'
-                ) {
-                    require_once 'modules/content_management/class/class_content_manager_tools.php';
-                    $cM = new content_management_tools();
-                    if (
-                        file_exists('custom'.DIRECTORY_SEPARATOR. $_SESSION['custom_override_id']
-                                    . DIRECTORY_SEPARATOR . 'modules'. DIRECTORY_SEPARATOR . 'content_management'
-                                    . DIRECTORY_SEPARATOR . 'applet_controller.php'
-                        )
-                    ) {
-                        $path = 'custom/'. $_SESSION['custom_override_id'] .'/modules/content_management/applet_controller.php';
-                    } else {
-                        $path = 'modules/content_management/applet_controller.php';
-                    }
-                    $path_appli = explode('/', $_SESSION['config']['coreurl']);
-                    if(count($path_appli) <> 5){
-                        $path_appli = array_slice($path_appli, 0, 4);
-                        $path_appli = implode('/', $path_appli);
-                    }else{
-                        $path_appli = implode('/', $path_appli);
-                    }
-                    // require_once 'core/class/class_db_pdo.php';
-                    // $database = new Database();
-                    // $query = "select template_id from templates where template_type = 'OFFICE' and template_target = 'attachments'";
-                    // $stmt = $database->query($query);
-                    // $aTemplateId = $stmt->fetchObject()->template_id;
-                    $cookieKey = $_SESSION['sessionName'] . '=' . $_COOKIE[$_SESSION['sessionName']];
-
-                    $onlyConvert = "true";
-                    $cM->generateJNLP(
-                        $path_appli,
-                        $path_appli . '/' . $path,
-                        'newAttachment',
-                        'res_letterbox',
-                        //$aTemplateId,
-                        $_SESSION['doc_id'],
-                        '',
-                        $cookieKey,
-                        $_SESSION['user']['UserId'],
-                        $_SESSION['clientSideCookies'], 
-                        $_SESSION['modules_loaded']['attachments']['convertPdf'],
-                        $onlyConvert
-                    );
-                }
+        if (!empty($_SESSION['upfile']['local_path']) && empty($_SESSION['error'])) { 
 
                 ?>
                 <i class="fa fa-check-square fa-2x" title="<?php echo _DOWNLOADED_FILE; ?>"></i>
@@ -163,11 +101,61 @@ $upFileOK = false;
         </p>
         <p style="display:none">
             <div align="center">
-            	<input type="radio" name="with_file" id="with_file" value="false" onclick="this.form.method = 'post';this.form.submit();" />
+                <input type="radio" name="with_file" id="with_file" value="false" onclick="this.form.method = 'post';this.form.submit();" />
             </div>
         </p>
     </form>
-    <?php $core_tools->load_js();?>
+    <?php 
+    if (!empty($_SESSION['upfile']['local_path']) && empty($_SESSION['error'])) { 
+        //launch auto convert in PDF
+        if (in_array(strtolower($_SESSION['upfile']['format']), array('odt','docx','doc','docm'))) {
+        //if (strtolower($_SESSION['upfile']['format']) == 'odt' || strtolower($_SESSION['upfile']['format']) == 'docx') {
+            echo "<script>window.parent.$('add').value='Edition en cours ...';window.parent.editingDoc('superadmin');window.parent.$('add').disabled='disabled';window.parent.$('add').style.opacity='0.5';</script>";
+            include_once 'modules/content_management/class/class_content_manager_tools.php';
+
+            $cM = new content_management_tools();
+            if (
+                file_exists('custom'.DIRECTORY_SEPARATOR. $_SESSION['custom_override_id']
+                            . DIRECTORY_SEPARATOR . 'modules'. DIRECTORY_SEPARATOR . 'content_management'
+                            . DIRECTORY_SEPARATOR . 'applet_controller.php'
+                )
+            ) {
+                $path = 'custom/'. $_SESSION['custom_override_id'] .'/modules/content_management/applet_controller.php';
+            } else {
+                $path = 'modules/content_management/applet_controller.php';
+            }
+            $path_appli = explode('/', $_SESSION['config']['coreurl']);
+            if (count($path_appli) <> 5) {
+                $path_appli = array_slice($path_appli, 0, 4);
+                $path_appli = implode('/', $path_appli);
+            } else {
+                $path_appli = implode('/', $path_appli);
+            }
+            // require_once 'core/class/class_db_pdo.php';
+            // $database = new Database();
+            // $query = "select template_id from templates where template_type = 'OFFICE' and template_target = 'attachments'";
+            // $stmt = $database->query($query);
+            // $aTemplateId = $stmt->fetchObject()->template_id;
+            $cookieKey = $_SESSION['sessionName'] . '=' . $_COOKIE[$_SESSION['sessionName']];
+
+            $onlyConvert = "true";
+            $cM->generateJNLP(
+                $path_appli,
+                $path_appli . '/' . $path,
+                'newAttachment',
+                'res_letterbox',
+                //$aTemplateId,
+                $_SESSION['doc_id'],
+                '',
+                $cookieKey,
+                $_SESSION['user']['UserId'],
+                $_SESSION['clientSideCookies'], 
+                $_SESSION['modules_loaded']['attachments']['convertPdf'],
+                $onlyConvert
+            );
+        }
+    }
+    $core_tools->load_js();?>
     </body>
 </html>
 !
\ No newline at end of file
diff --git a/modules/content_management/checkEditingDoc.php b/modules/content_management/checkEditingDoc.php
index 9754736f451..8a7037e42b4 100755
--- a/modules/content_management/checkEditingDoc.php
+++ b/modules/content_management/checkEditingDoc.php
@@ -12,5 +12,5 @@
 if (!empty($_SESSION['cm_applet'][$_SESSION['user']['UserId']])) {
     echo "{\"status\" : 1, \"status_txt\" : \"LCK FOUND !\"}";
 } else {
-    echo "{\"status\" : 0, \"status_txt\" : \"LCK NOT FOUND !\"}";
+    echo "{\"status\" : 0, \"status_txt\" : \"LCK NOT FOUND !\", \"pdf_version\" : \"".$_SESSION['config']['tmppath'].$_SESSION['upfile']['fileNamePdfOnTmp']."\"}";
 }
\ No newline at end of file
diff --git a/modules/content_management/class/class_content_manager_tools_Abstract.php b/modules/content_management/class/class_content_manager_tools_Abstract.php
index 2fc8664fd13..f1d230352cf 100755
--- a/modules/content_management/class/class_content_manager_tools_Abstract.php
+++ b/modules/content_management/class/class_content_manager_tools_Abstract.php
@@ -598,7 +598,6 @@ abstract class content_management_tools_Abstract
 
         //echo '<a id="jnlp_file" href="'.$file.'" onclick="window.location.href=\''.$file.'\';self.close();"></a>';
         echo '<script>window.location.href=\''.$file.'\';if($(\'CMApplet\')) {destroyModal(\'CMApplet\');};if($(\'CMApplet\')) {destroyModal(\'CMApplet\');};</script>';
-        exit();
         /*echo '<a id="jnlp_file" href="'.$_SESSION['config']['businessappurl'].'index.php?page=get_jnlp_file&module=content_management&display=true&filename='.$_SESSION['user']['UserId'].'_maarchCM"></a>';
         echo '<script>setTimeout(function() {this.window.close();}, 5000);document.getElementById("jnlp_file").click();</script>';
         exit();*/
diff --git a/modules/content_management/js/functions.js b/modules/content_management/js/functions.js
index 523daae7643..4318a8ba75c 100755
--- a/modules/content_management/js/functions.js
+++ b/modules/content_management/js/functions.js
@@ -226,6 +226,8 @@ function checkEditingDoc(userId) {
 
                 //END OF CHECKING APPLET
                 console.log('clearInterval');
+                document.getElementById('viewframevalid_attachment').src='index.php?display=true&dir=indexing_searching&page=file_iframe&#navpanes=0'+response.pdf_version;
+                //console.log(response.pdf_version);
                 clearInterval(editing);
             } else {
                 console.log('lck found! Editing in progress !');
diff --git a/modules/content_management/save_attach_res_from_cm.php b/modules/content_management/save_attach_res_from_cm.php
index 32e99678fc9..e3f4c66c2bd 100755
--- a/modules/content_management/save_attach_res_from_cm.php
+++ b/modules/content_management/save_attach_res_from_cm.php
@@ -1,7 +1,7 @@
 <?php
 
 //FOR ADD NEW ATTACHMENTS
-
+$sec = new security();
 // case onlyConvert
 if (
     $_SESSION['modules_loaded']['attachments']['convertPdf'] == true &&
@@ -19,6 +19,9 @@ if (isset($_SESSION['cm']['resMaster']) && $_SESSION['cm']['resMaster'] <> '') {
 
 $_SESSION['cm']['resMaster'] = '';
 
+if (empty($_SESSION['cm']['collId'])) {
+    $_SESSION['cm']['collId'] = $sec->retrieve_coll_id_from_table($objectTable);
+}
 $docserverControler = new docservers_controler();
 $docserver = $docserverControler->getDocserverToInsert(
     $_SESSION['cm']['collId']
@@ -161,7 +164,6 @@ if (empty($docserver)) {
             } else {
                 if ($_SESSION['history']['attachadd'] == "true") {
                     $hist = new history();
-                    $sec = new security();
                     $view = $sec->retrieve_view_from_coll_id(
                         $collId
                     );
-- 
GitLab