浏览代码

Download Document

stany.ferer 1 年之前
父节点
当前提交
7cf5246b1d
共有 4 个文件被更改,包括 32 次插入3 次删除
  1. 21 2
      core/class/document.class.php
  2. 9 0
      core/javascript/cms.document.php
  3. 1 0
      maj/sql/maj.sql
  4. 1 1
      public-cms/document.php

+ 21 - 2
core/class/document.class.php

@@ -307,6 +307,20 @@ class document
         }
     }
 
+    static public function getFile(string $_id) {
+        $filePatch = file::download($_id, DIR_DATAS_DOCS);
+        
+        if (file_exists($filePatch) && is_readable($filePatch)) {
+            $file_info = new finfo(FILEINFO_MIME_TYPE);
+            $mime_type = $file_info->file($filePatch);
+            header('Content-Type: ' . $mime_type);
+            header('Content-Length: ' . filesize($filePatch));
+            readfile($filePatch);
+        } else {
+            echo "Le fichier n'a pas été trouvé ou n'est pas lisible.";
+        }
+    }
+
     static public function get(float $_id){
         db::query("SELECT "
             . "" . DB_T_DOCUMENTS . ".id, "
@@ -457,8 +471,12 @@ class document
                         <div><span class="fw-bold">'.$principal["name"].'</span> ('.core::convertBytes($principal["size"]).')</div>
                         Chargé le '.core::convertDate($principal["creer"]).' par '.$principal["user"].'
                         <div id="select-attach-'.$principal["id"].'" style="color:red;"></div>
-                    </div><button type="button" title="Voir le document" class="btn btn btn-outline-secondary" onclick="window.open(\'/document.php?id=' . $principal["id"] . '\', \'_blank\')">' . icon::getFont(["icon" => "bi bi-eye-fill"]) . '</button>
-                </li>';
+                    </div>
+                        <div class="btn-group">
+                            <button type="button" title="Voir le document" class="btn btn btn-outline-secondary" onclick="window.open(\'/document.php?id=' . $principal["id"] . '\', \'_blank\')">' . icon::getFont(["icon" => "bi bi-eye-fill"]) . '</button>
+                            <button type="button" title="Télécharger le document" class="btn btn btn-outline-secondary" onclick="downloadFile(\'/document.php?id=' . $principal["id"] . '&download=1\', \''.$principal["name"].'\')">' . icon::getFont(["icon" => "bi bi-arrow-down-square-fill"]) . '</button>
+                        </div>
+                            </li>';
         foreach ($_attachs as $key => $attach) {
                 if($key != "principal"){
                     echo '  <li class="list-group-item d-flex justify-content-between align-items-start" id="attach-'.$attach["id"].'">
@@ -468,6 +486,7 @@ class document
                                     <div id="select-attach-'.$attach["id"].'"></div>
                                 </div><div class="btn-group">
                                     <button type="button" title="Voir le document" class="btn btn btn-outline-secondary" onclick="window.open(\'/document.php?id=' . $attach["id"] . '\', \'_blank\')">' . icon::getFont(["icon" => "bi bi-eye-fill"]) . '</button>
+                                    <button type="button" title="Télécharger le document" class="btn btn btn-outline-secondary" onclick="downloadFile(\'/document.php?id=' . $attach["id"] . '&download=1\', \''.$attach["name"].'\')">' . icon::getFont(["icon" => "bi bi-arrow-down-square-fill"]) . '</button>
                                     <button type="button" title="Document mis en avant" class="btn btn btn-outline-primary" onclick="defaultAttachment(\''.$attach["id"].'\')" id="button-default-'.$attach["id"].'">' . icon::getFont(["icon" => "bi bi-star-fill"]) . '</button>
                                     <button type="button" title="Supprimer ce document" class="btn btn-outline-danger" onclick="deleteAttachment(\''.$attach["id"].'\')" id="button-delete-'.$attach["id"].'">' . icon::getFont(["icon" => "bi bi-trash"]) . '</button>
                                 </div>

+ 9 - 0
core/javascript/cms.document.php

@@ -117,4 +117,13 @@
             max: 5
         });
     });
+
+    function downloadFile(url, nomFichier) {
+        const lien = document.createElement('a');
+        lien.href = url;
+        lien.download = nomFichier;
+        document.body.appendChild(lien);
+        lien.click();
+        document.body.removeChild(lien);
+    }
 </script>

+ 1 - 0
maj/sql/maj.sql

@@ -1 +1,2 @@
 
+UPDATE `access` SET `access` = 'document*\r\ndocuments*\r\nadd-document' WHERE `access`.`id` = 16; 

+ 1 - 1
public-cms/document.php

@@ -8,4 +8,4 @@ require_once "../conf.inc.php";
 require_once DIR_PHP_LAYOUTS . "header.php";
 require_once DIR_PHP_LAYOUTS . "cms.session.php";
 
-document::printFile(core::getGet("id"));
+core::ifGet("download") ? document::getFile(core::getGet("id")) : document::printFile(core::getGet("id"));