This repository has been archived on 2026-07-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LegalAssistance/scripts/AfterInstall.php
T
chaim b3c219aa57 feat: add LegalAid entity — centralized legal aid data management
Replace scattered Case fields with a dedicated LegalAid entity that
holds all legal aid data (aid type, urgency, client contact, proceeding
details, legal analysis, recommendation, etc.). DirectAccessReport now
pulls from LegalAid for DOCX generation with automatic checkbox mapping.

- Add LegalAid entity with controller, layouts, i18n (he/en/fa)
- Remove cAidType, cUrgencyLevel, cAppointmentDate, cFilingDeadline,
  cLegalAidType from Case (migrated to LegalAid)
- Add legalAidType field to LegalAid (DirectAccess/Regular/Duty)
- Update DirectAccessReportService to use LegalAid entity
- Add update_legal_aid and get_legal_aid SmartAssistant tools
- Fix AfterInstall template entityType to LegalAid

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 15:27:23 +00:00

164 lines
5.6 KiB
PHP

<?php
/************************************************************************
* LegalAssistance Extension — AfterInstall
*
* 1. Deploy plugin files to ai-gateway plugins/ directory
* 2. Create DocumentTemplate for the direct access report
* 3. Clear cache
************************************************************************/
use Espo\Core\Container;
use Espo\Core\DataManager;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Log;
class AfterInstall
{
public function run(Container $container): void
{
/** @var EntityManager $entityManager */
$entityManager = $container->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. 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 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 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' => 'LegalAid',
'fileId' => $attachment->getId(),
]);
$entityManager->saveEntity($template);
$log->info("LegalAssistance: DocumentTemplate created: {$template->getId()}");
}
}