getByClass(EntityManager::class); /** @var Log $log */ $log = $container->getByClass(Log::class); $log->info('LegalAssistance: Running AfterInstall...'); // 0. Ensure custom i18n directories exist for all supported locales $config = $container->getByClass(Config::class); $this->ensureI18nDirectories($config, $log); // 1. Create DocumentTemplate $this->createDocumentTemplate($entityManager, $log); // 2. 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 ensureI18nDirectories(Config $config, Log $log): void { $basePath = 'custom/Espo/Custom/Resources/i18n'; $language = $config->get('language', 'en_US'); $locales = array_unique(['en_US', 'fa_IR', 'he_IL', $language]); foreach ($locales as $locale) { $dir = $basePath . '/' . $locale; if (!is_dir($dir)) { if (mkdir($dir, 0775, true)) { $log->info("LegalAssistance: Created i18n directory: {$dir}"); } else { $log->warning("LegalAssistance: Failed to create i18n directory: {$dir}"); } } } } 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()}"); } }