fix: resolve template path and PHPWord autoload in DirectAccessReportGenerator

- Fix resolveTemplatePath to check data/document-templates/ prefix
- Add PHPWord autoloader fallback when not in composer autoload
- Fix Document-Case linking (use Case.documents relation side)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-07 19:24:17 +00:00
parent 21cf570cbc
commit cf523cbd86
@@ -106,10 +106,10 @@ class DirectAccessReportGenerator
]);
$this->entityManager->saveEntity($document);
// Link Document to Case
$this->entityManager->getRDBRepository('Document')
->getRelation($document, 'cases')
->relate($case);
// Link Document to Case (via Case side of relationship)
$this->entityManager->getRDBRepository('Case')
->getRelation($case, 'documents')
->relate($document);
$this->log->info("LegalAssistance: Report generated — Document {$document->getId()} attached to Case {$caseId}");
@@ -230,32 +230,31 @@ class DirectAccessReportGenerator
private function resolveTemplatePath(): string
{
// Look up template via DocumentTemplate entity
$template = $this->entityManager
->getRDBRepository('DocumentTemplate')
->where(['name' => self::TEMPLATE_NAME])
->findOne();
if ($template) {
$fileId = $template->get('fileId');
if ($fileId) {
$filePath = 'data/upload/' . $fileId;
if (file_exists($filePath)) {
return $filePath;
}
}
// Try templatePath field
$templatePath = $template->get('templatePath');
if ($templatePath && file_exists($templatePath)) {
if ($templatePath) {
// Try as-is (absolute or relative)
if (file_exists($templatePath)) {
return $templatePath;
}
// Try under data/document-templates/
$resolved = 'data/document-templates/' . ltrim($templatePath, '/');
if (file_exists($resolved)) {
return $resolved;
}
}
}
// Fallback: look in known locations
// Fallback: known locations
$fallbackPaths = [
'data/document-templates/direct-access-report.docx',
'custom/Espo/Modules/LegalAssistance/templates/direct-access-report.docx',
__DIR__ . '/../../../../../../templates/direct-access-report.docx',
];
foreach ($fallbackPaths as $path) {
@@ -269,6 +268,15 @@ class DirectAccessReportGenerator
private function processTemplate(string $inputPath, string $outputPath, array $data): void
{
// Ensure PHPWord autoloader is registered
if (!class_exists(TemplateProcessor::class, false)) {
$autoloader = 'vendor/phpoffice/phpword/src/PhpWord/Autoloader.php';
if (file_exists($autoloader)) {
require_once $autoloader;
\PhpOffice\PhpWord\Autoloader::register();
}
}
$processor = new TemplateProcessor($inputPath);
foreach ($data as $key => $value) {