From 3c134f00d8db506eaaa105dab6abbfa33f6d8c0d Mon Sep 17 00:00:00 2001 From: loviuz Date: Fri, 25 Feb 2022 16:41:05 +0100 Subject: [PATCH] Fix esportazione file in bulk con stesso nome generato --- modules/fatture/bulk.php | 2 +- modules/interventi/bulk.php | 2 +- src/Prints.php | 38 ++++++++++++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/modules/fatture/bulk.php b/modules/fatture/bulk.php index fde0ad2d8..9f585c9ff 100755 --- a/modules/fatture/bulk.php +++ b/modules/fatture/bulk.php @@ -53,7 +53,7 @@ switch (post('op')) { foreach ($fatture as $r) { $print = Prints::getModulePredefinedPrint($id_module); - Prints::render($print['id'], $r['id'], $dir.'tmp/'); + Prints::render($print['id'], $r['id'], $dir.'tmp/', false, false); } // Creazione zip diff --git a/modules/interventi/bulk.php b/modules/interventi/bulk.php index 6f89edf82..fd23a3b2f 100755 --- a/modules/interventi/bulk.php +++ b/modules/interventi/bulk.php @@ -56,7 +56,7 @@ switch (post('op')) { foreach ($interventi as $r) { $print = Prints::getModulePredefinedPrint($id_module); - Prints::render($print['id'], $r['id'], $dir.'tmp/'); + Prints::render($print['id'], $r['id'], $dir.'tmp/', false, false); } $dir = slashes($dir); diff --git a/src/Prints.php b/src/Prints.php index 5c4d43884..d07155913 100755 --- a/src/Prints.php +++ b/src/Prints.php @@ -139,7 +139,7 @@ class Prints * @param string $directory * @param bool $return_string */ - public static function render($print, $id_record, $directory = null, $return_string = false) + public static function render($print, $id_record, $directory = null, $return_string = false, $overwrite = true) { //ob_end_clean(); // Compatibilità con versioni vecchie delle stampe $dbo = $database = database(); @@ -186,9 +186,9 @@ class Prints if (self::isCompletelyCustom($print)) { return self::customLoader($infos['id'], $id_record, $directory, $return_string); } elseif (self::isOldStandard($print)) { - return self::oldLoader($infos['id'], $id_record, $directory, $return_string); + return self::oldLoader($infos['id'], $id_record, $directory, $return_string, $overwrite); } else { - return self::loader($infos['id'], $id_record, $directory, $return_string); + return self::loader($infos['id'], $id_record, $directory, $return_string, $overwrite); } } @@ -354,7 +354,7 @@ class Prints * @param string $directory * @param bool $return_string */ - protected static function oldLoader($id_print, $id_record, $directory = null, $return_string = false) + protected static function oldLoader($id_print, $id_record, $directory = null, $return_string = false, $overwrite = true) { $format = 'A4'; @@ -415,6 +415,20 @@ class Prints $html2pdf->writeHTML($report); $html2pdf->pdf->setTitle($title); + // Ridenominazione file se l'impostazione è disattivata + if (!$overwrite) { + $index = 1; + + $file_parts = pathinfo($path); + $filename_no_extension = $file_parts['filename']; + + while (file_exists($directory.'/'.$file_parts['basename'])) { + $path = $file_parts['dirname'].'/'.$filename_no_extension.'_'.$index++.'.'.$file_parts['extension']; + + $file_parts = pathinfo($path); + } + } + $pdf = $html2pdf->output($path, $mode); $file['pdf'] = $pdf; }else{ @@ -458,7 +472,7 @@ class Prints * @param string $directory * @param bool $return_string */ - protected static function loader($id_print, $id_record, $directory = null, $return_string = false) + protected static function loader($id_print, $id_record, $directory = null, $return_string = false, $overwrite = true) { $infos = self::get($id_print); $options = self::readOptions($infos['options']); @@ -650,6 +664,20 @@ class Prints $title = $file['name']; $path = $file['path']; + // Ridenominazione file se l'impostazione è disattivata + if (!$overwrite) { + $index = 1; + + $file_parts = pathinfo($path); + $filename_no_extension = $file_parts['filename']; + + while (file_exists($directory.'/'.$file_parts['basename'])) { + $path = $file_parts['dirname'].'/'.$filename_no_extension.'_'.$index++.'.'.$file_parts['extension']; + + $file_parts = pathinfo($path); + } + } + // Impostazione del titolo del PDF $mpdf->SetTitle($title);