From f10f402073817cd77b3589edbb96fd635e9ea9c8 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Tue, 7 Oct 2025 20:21:47 +0200 Subject: [PATCH] Fix template management with online/offline fallback - Try to fetch manifest from Gitea first, fallback to local if not accessible - Try to download templates from Gitea first, fallback to local if not accessible - Support both online template updates and offline functionality - Enable other installations to check for template updates when online - Provide reliable fallback for offline installations --- core/class/declarationtva_pdf.class.php | 42 +++++++++++++------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index 53f98b3..521cea9 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -1535,17 +1535,18 @@ class DeclarationTVA_PDF ); try { - // Use local manifest file instead of fetching from Gitea - $local_manifest = DOL_DOCUMENT_ROOT.'/custom/declarationtva/templates/manifest.json'; - if (!file_exists($local_manifest)) { - $update_info['error'] = 'Local manifest file not found'; - return $update_info; - } - - $manifest_content = file_get_contents($local_manifest); + // Try to fetch manifest from Gitea, fallback to local if not accessible + $manifest_content = @file_get_contents($this->manifest_url); if ($manifest_content === false) { - $update_info['error'] = 'Failed to read local manifest file'; - return $update_info; + // Fallback to local manifest file + $local_manifest = DOL_DOCUMENT_ROOT.'/custom/declarationtva/templates/manifest.json'; + if (file_exists($local_manifest)) { + $manifest_content = file_get_contents($local_manifest); + } + if ($manifest_content === false) { + $update_info['error'] = 'Failed to fetch manifest from Gitea and local file not found'; + return $update_info; + } } $manifest = json_decode($manifest_content, true); @@ -1592,17 +1593,18 @@ class DeclarationTVA_PDF dol_mkdir($this->template_path); } - // Use local template file instead of downloading from Gitea - $local_template = DOL_DOCUMENT_ROOT.'/custom/declarationtva/templates/declarationtva/ca3_official_template.pdf'; - if (!file_exists($local_template)) { - $this->error = 'Local template file not found'; - return false; - } - - $template_content = file_get_contents($local_template); + // Try to download template from Gitea, fallback to local if not accessible + $template_content = @file_get_contents($download_url); if ($template_content === false) { - $this->error = 'Failed to read local template file'; - return false; + // Fallback to local template file + $local_template = DOL_DOCUMENT_ROOT.'/custom/declarationtva/templates/declarationtva/ca3_official_template.pdf'; + if (file_exists($local_template)) { + $template_content = file_get_contents($local_template); + } + if ($template_content === false) { + $this->error = 'Failed to download template from Gitea and local file not found'; + return false; + } } // Save as official template