getByClass(EntityManager::class); /** @var Log $log */ $log = $container->getByClass(Log::class); $log->info('LegalAssistance: Running AfterInstall...'); // 1. Deploy ai-gateway plugin files $this->deployPluginFiles($log); // 2. Create DocumentTemplate $this->createDocumentTemplate($entityManager, $log); // 3. Clear cache try { $dataManager = $container->getByClass(DataManager::class); $dataManager->clearCache(); $log->info('LegalAssistance: Cache cleared.'); } catch (\Throwable $e) { $log->warning('LegalAssistance: Cache clear failed: ' . $e->getMessage()); } $log->info('LegalAssistance: AfterInstall completed.'); } private function deployPluginFiles(Log $log): void { // Source: extension's plugins/ directory (relative to EspoCRM root after install) $sourceDir = 'custom/Espo/Modules/LegalAssistance/../../../../../../plugins/legal-assistance'; // Try common ai-gateway locations $gatewayPaths = [ '/home/chaim/ai-gateway/plugins/legal-assistance', '/opt/ai-gateway/plugins/legal-assistance', ]; // Find actual source files within the extension package $extPluginDir = __DIR__ . '/../plugins/legal-assistance'; if (!is_dir($extPluginDir)) { $log->warning("LegalAssistance: Plugin source directory not found at {$extPluginDir}"); return; } foreach ($gatewayPaths as $targetDir) { $parentDir = dirname($targetDir); if (!is_dir($parentDir)) { continue; } if (!is_dir($targetDir)) { mkdir($targetDir, 0755, true); } $files = ['tools.json', 'prompts.json']; foreach ($files as $file) { $src = $extPluginDir . '/' . $file; $dst = $targetDir . '/' . $file; if (file_exists($src)) { copy($src, $dst); $log->info("LegalAssistance: Deployed {$file} to {$targetDir}"); } } $log->info("LegalAssistance: Plugin files deployed to {$targetDir}"); return; } $log->warning('LegalAssistance: Could not find ai-gateway plugins directory. Deploy manually.'); } private function createDocumentTemplate(EntityManager $entityManager, Log $log): void { // Check if template already exists $existing = $entityManager ->getRDBRepository('DocumentTemplate') ->where(['name' => 'דוח גישה ישירה']) ->findOne(); if ($existing) { $log->info('LegalAssistance: DocumentTemplate already exists, skipping.'); return; } // Find the template DOCX file $templateFile = __DIR__ . '/../templates/direct-access-report.docx'; if (!file_exists($templateFile)) { $log->warning("LegalAssistance: Template file not found at {$templateFile}"); return; } // Create attachment for the template file $contents = file_get_contents($templateFile); $attachment = $entityManager->getNewEntity('Attachment'); $attachment->set([ 'name' => 'direct-access-report.docx', 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'role' => 'Attachment', 'size' => strlen($contents), ]); $entityManager->saveEntity($attachment); // Write file to storage $filePath = 'data/upload/' . $attachment->getId(); file_put_contents($filePath, $contents); // Create DocumentTemplate entity $template = $entityManager->getNewEntity('DocumentTemplate'); $template->set([ 'name' => 'דוח גישה ישירה', 'entityType' => 'Case', 'fileId' => $attachment->getId(), ]); $entityManager->saveEntity($template); $log->info("LegalAssistance: DocumentTemplate created: {$template->getId()}"); } }