From 015586a9df5acce95d13fc29998dd7e810d0dcfc Mon Sep 17 00:00:00 2001 From: Chaim Date: Thu, 9 Apr 2026 13:41:26 +0000 Subject: [PATCH] fix: resolve report template from WebDAV when not found locally DirectAccessReportGenerator now downloads the template from NetworkStorage (WebDAV) if it's not found in data/document-templates/. This means template updates via WebDAV are picked up automatically without manual file copy. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../Tools/DirectAccessReportGenerator.php | 51 +++++++++++++++++++ manifest.json | 4 +- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/files/custom/Espo/Modules/LegalAssistance/SmartAssistant/Tools/DirectAccessReportGenerator.php b/files/custom/Espo/Modules/LegalAssistance/SmartAssistant/Tools/DirectAccessReportGenerator.php index 2896f70..7f89895 100644 --- a/files/custom/Espo/Modules/LegalAssistance/SmartAssistant/Tools/DirectAccessReportGenerator.php +++ b/files/custom/Espo/Modules/LegalAssistance/SmartAssistant/Tools/DirectAccessReportGenerator.php @@ -83,6 +83,11 @@ class DirectAccessReportGenerator $content = file_get_contents($outputPath); @unlink($outputPath); + // Clean up temp template if downloaded from storage + if (str_starts_with($templatePath, self::TEMP_DIR)) { + @unlink($templatePath); + } + // Create Attachment $attachment = $this->entityManager->getNewEntity('Attachment'); $attachment->set([ @@ -248,6 +253,12 @@ class DirectAccessReportGenerator if (file_exists($resolved)) { return $resolved; } + + // Download from NetworkStorage (WebDAV) + $downloaded = $this->downloadTemplateFromStorage($templatePath); + if ($downloaded) { + return $downloaded; + } } } @@ -266,6 +277,46 @@ class DirectAccessReportGenerator throw new Error("Template '" . self::TEMPLATE_NAME . "' not found."); } + private function downloadTemplateFromStorage(string $templatePath): ?string + { + try { + $nds = $this->injectableFactory->create( + \Espo\Modules\NetworkStorageIntegration\Services\NetworkDocumentService::class + ); + $client = $nds->getClient(); + + // Try Templates/{templatePath} on the storage + $integration = $this->entityManager->getEntityById('Integration', 'NetworkStorage'); + $templatesFolderPath = 'Templates'; + if ($integration && $integration->get('enabled')) { + $data = (array) ($integration->get('data') ?? []); + $templatesFolderPath = $data['templatesFolderPath'] ?? 'Templates'; + } + + $storagePath = $templatesFolderPath . '/' . $templatePath; + + if (!$client->exists($storagePath)) { + $this->log->debug("LegalAssistance: Template not found on storage: {$storagePath}"); + return null; + } + + $content = $client->downloadFile($storagePath); + + // Save locally for this request + if (!is_dir(self::TEMP_DIR)) { + mkdir(self::TEMP_DIR, 0775, true); + } + $localPath = self::TEMP_DIR . 'template_' . uniqid() . '.docx'; + file_put_contents($localPath, $content); + + $this->log->debug("LegalAssistance: Template downloaded from storage: {$storagePath} → {$localPath}"); + return $localPath; + } catch (\Exception $e) { + $this->log->warning("LegalAssistance: Failed to download template from storage: " . $e->getMessage()); + return null; + } + } + private function processTemplate(string $inputPath, string $outputPath, array $data): void { // Ensure PHPWord autoloader is registered diff --git a/manifest.json b/manifest.json index 88cecd6..6ee0472 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,9 @@ { "name": "LegalAssistance", - "version": "2.0.1", + "version": "2.0.2", "acceptableVersions": [">=8.0.0"], "php": [">=8.1"], - "releaseDate": "2026-04-07", + "releaseDate": "2026-04-09", "author": "Marcus-Law", "description": "סיוע משפטי — יצירת דוח גישה ישירה (JSON ישירות ל-DOCX), אינטגרציה עם שירה", "scripts": {