diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index 28869e76fca..337b0423826 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -2322,16 +2322,16 @@ class Commande extends CommonObject dol_syslog("CustomerOrder::delete error", LOG_ERR); $err++; } - + // On efface le repertoire de pdf provisoire $comref = dol_sanitizeFileName($this->ref); if ($conf->commande->dir_output) { $dir = $conf->commande->dir_output . "/" . $comref ; $file = $conf->commande->dir_output . "/" . $comref . "/" . $comref . ".pdf"; - if (file_exists($file)) + if (file_exists($file)) // We must delete all files before deleting directory { - commande_delete_preview($this->db, $this->id, $this->ref); + dol_delete_preview($object); if (!dol_delete_file($file)) { diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index f22692f6da0..cf17e5dc564 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1609,7 +1609,7 @@ abstract class CommonObject } else return 0; } - + // -------------------- // TODO: All functions here must be redesigned and moved as they are not business functions but output functions @@ -1619,6 +1619,7 @@ abstract class CommonObject /** * * Enter description here ... + * * @param unknown_type $objectid * @param unknown_type $objecttype * @param unknown_type $withpicto diff --git a/htdocs/includes/modules/commande/modules_commande.php b/htdocs/includes/modules/commande/modules_commande.php index dfd948bd4d8..65a7344ba09 100644 --- a/htdocs/includes/modules/commande/modules_commande.php +++ b/htdocs/includes/modules/commande/modules_commande.php @@ -201,8 +201,10 @@ function commande_pdf_create($db, $object, $modele, $outputlangs, $hidedetails=0 if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $hookmanager) > 0) { $outputlangs->charset_output=$sav_charset_output; - // on supprime l'image correspondant au preview - commande_delete_preview($db, $object->id); + + // we delete preview files + require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php"); + dol_delete_preview($object); // Appel des triggers include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); @@ -234,58 +236,4 @@ function commande_pdf_create($db, $object, $modele, $outputlangs, $hidedetails=0 return 0; } } - -/** - * Supprime l'image de previsualitation, pour le cas de regeneration de commande - * @param db data base object - * @param commandeid id de la commande a effacer - * @param commanderef reference de la commande si besoin - */ -function commande_delete_preview($db, $commandeid, $commanderef='') -{ - global $langs,$conf; - require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php"); - - if (!$commanderef) - { - $com = new Commande($db); - $com->fetch($commandeid); - $commanderef = $com->ref; - } - - if ($conf->commande->dir_output) - { - $comref = dol_sanitizeFileName($commanderef); - $dir = $conf->commande->dir_output . "/" . $comref ; - $file = $dir . "/" . $comref . ".pdf.png"; - $multiple = $file . "."; - - if ( file_exists( $file ) && is_writable( $file ) ) - { - if ( ! dol_delete_file($file,1) ) - { - $this->error=$langs->trans("ErrorFailedToOpenFile",$file); - return 0; - } - } - else - { - for ($i = 0; $i < 20; $i++) - { - $preview = $multiple.$i; - - if ( file_exists( $preview ) && is_writable( $preview ) ) - { - if ( ! dol_delete_file($preview,1) ) - { - $this->error=$langs->trans("ErrorFailedToOpenFile",$preview); - return 0; - } - } - } - } - } - - return 1; -} ?> diff --git a/htdocs/lib/files.lib.php b/htdocs/lib/files.lib.php index d94969be227..bda036d44b7 100644 --- a/htdocs/lib/files.lib.php +++ b/htdocs/lib/files.lib.php @@ -605,6 +605,7 @@ function dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disable /** * Remove a file or several files with a mask + * * @param file File to delete or mask of file to delete * @param disableglob Disable usage of glob like * * @param nophperrors Disable all PHP output errors @@ -658,6 +659,7 @@ function dol_delete_file($file,$disableglob=0,$nophperrors=0,$notrigger=0,$trigg /** * Remove a directory (not recursive, so content must be empty). * If directory is not empty, return false + * * @param dir Directory to delete * @param nophperrors Disable all PHP output errors * @return boolean True if success, false if error @@ -712,6 +714,54 @@ function dol_delete_dir_recursive($dir,$count=0,$nophperrors=0) return $count; } + +/** + * Delete all preview files linked to object instance + * + * @param Object $object Object to clean + * @return int 0 if error, 1 if OK + */ +function dol_delete_preview($object) +{ + global $langs,$conf; + require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php"); + + if ($object->element == 'commande') $dir = $conf->commande->dir_output; + if (empty($dir)) return 'ErrorObjectNoSupportedByFunction'; + + $refsan = dol_sanitizeFileName($object->ref); + $dir = $dir . "/" . $refsan ; + $file = $dir . "/" . $refsan . ".pdf.png"; + $multiple = $file . "."; + + if (file_exists($file) && is_writable($file)) + { + if ( ! dol_delete_file($file,1) ) + { + $this->error=$langs->trans("ErrorFailedToOpenFile",$file); + return 0; + } + } + else + { + for ($i = 0; $i < 20; $i++) + { + $preview = $multiple.$i; + + if (file_exists($preview) && is_writable($preview)) + { + if ( ! dol_delete_file($preview,1) ) + { + $this->error=$langs->trans("ErrorFailedToOpenFile",$preview); + return 0; + } + } + } + } + + return 1; +} + /** * Get and save an upload file (for example after submitting a new file a mail form).