15602a976f
Legal tools (generate_initial_report) are now built into shira-hermes (mcp_server/tools/legal_tools.py). The AfterInstall no longer needs to deploy tools.json and prompts.json to ai-gateway's plugins/ directory. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
153 lines
5.6 KiB
PHP
153 lines
5.6 KiB
PHP
<?php
|
|
/************************************************************************
|
|
* LegalAssistance Extension — AfterUninstall
|
|
*
|
|
* 1. Drop LegalAid and DirectAccessReport DB tables (with warning log)
|
|
* 2. Remove LegalAssistance-specific columns from Case table
|
|
* 3. Remove DocumentTemplate records created by this extension
|
|
* 4. Clear cache
|
|
*
|
|
* Note: ai-gateway plugin cleanup removed — legal tools are now
|
|
* built into shira-hermes since Phase 3.
|
|
************************************************************************/
|
|
|
|
use Espo\Core\Container;
|
|
use Espo\Core\DataManager;
|
|
use Espo\Core\ORM\EntityManager;
|
|
use Espo\Core\Utils\Log;
|
|
|
|
class AfterUninstall
|
|
{
|
|
public function run(Container $container): void
|
|
{
|
|
/** @var Log $log */
|
|
$log = $container->getByClass(Log::class);
|
|
/** @var EntityManager $entityManager */
|
|
$entityManager = $container->getByClass(EntityManager::class);
|
|
|
|
$log->info('LegalAssistance: Running AfterUninstall...');
|
|
|
|
// 1. Drop tables created by this extension
|
|
$this->dropTables($entityManager, $log);
|
|
|
|
// 2. Remove LegalAssistance-specific columns from Case
|
|
$this->cleanCaseColumns($entityManager, $log);
|
|
|
|
// 3. Remove DocumentTemplate records
|
|
$this->removeDocumentTemplates($entityManager, $log);
|
|
|
|
// 4. Remove custom i18n files
|
|
$this->removeCustomI18n($log);
|
|
|
|
// 5. 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: AfterUninstall completed.');
|
|
}
|
|
|
|
private function dropTables(EntityManager $entityManager, Log $log): void
|
|
{
|
|
$pdo = $entityManager->getPDO();
|
|
|
|
$tables = ['legal_aid', 'direct_access_report'];
|
|
|
|
foreach ($tables as $table) {
|
|
try {
|
|
$check = $pdo->query("SHOW TABLES LIKE '{$table}'")->rowCount();
|
|
if ($check > 0) {
|
|
$count = $pdo->query("SELECT COUNT(*) FROM `{$table}`")->fetchColumn();
|
|
$log->warning("LegalAssistance: Dropping table '{$table}' ({$count} rows).");
|
|
$pdo->exec("DROP TABLE `{$table}`");
|
|
$log->info("LegalAssistance: Table '{$table}' dropped.");
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$log->warning("LegalAssistance: Failed to drop table '{$table}': " . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
private function cleanCaseColumns(EntityManager $entityManager, Log $log): void
|
|
{
|
|
$pdo = $entityManager->getPDO();
|
|
|
|
// Only columns that LegalAssistance module added to Case
|
|
$columns = [
|
|
'c_legal_aid_type',
|
|
'c_aid_type',
|
|
'c_urgency_level',
|
|
'c_appointment_date',
|
|
'c_filing_deadline',
|
|
];
|
|
|
|
try {
|
|
$stmt = $pdo->query("SHOW COLUMNS FROM `case`");
|
|
$existing = array_column($stmt->fetchAll(\PDO::FETCH_ASSOC), 'Field');
|
|
|
|
foreach ($columns as $col) {
|
|
if (in_array($col, $existing)) {
|
|
$pdo->exec("ALTER TABLE `case` DROP COLUMN `{$col}`");
|
|
$log->info("LegalAssistance: Dropped column case.{$col}");
|
|
}
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$log->warning("LegalAssistance: Failed to clean Case columns: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function removeDocumentTemplates(EntityManager $entityManager, Log $log): void
|
|
{
|
|
try {
|
|
$template = $entityManager
|
|
->getRDBRepository('DocumentTemplate')
|
|
->where(['name' => 'דוח גישה ישירה'])
|
|
->findOne();
|
|
|
|
if ($template) {
|
|
$fileId = $template->get('fileId');
|
|
$entityManager->removeEntity($template);
|
|
$log->info("LegalAssistance: Removed DocumentTemplate 'דוח גישה ישירה'");
|
|
|
|
// Remove the attachment file
|
|
if ($fileId) {
|
|
$attachment = $entityManager->getEntityById('Attachment', $fileId);
|
|
if ($attachment) {
|
|
$filePath = 'data/upload/' . $fileId;
|
|
if (file_exists($filePath)) {
|
|
unlink($filePath);
|
|
}
|
|
$entityManager->removeEntity($attachment);
|
|
$log->info("LegalAssistance: Removed template attachment file");
|
|
}
|
|
}
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$log->warning("LegalAssistance: Failed to remove DocumentTemplate: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function removeCustomI18n(Log $log): void
|
|
{
|
|
$files = [
|
|
'custom/Espo/Custom/Resources/i18n/fa_IR/DirectAccessReport.json',
|
|
'custom/Espo/Custom/Resources/i18n/fa_IR/LegalAid.json',
|
|
'custom/Espo/Custom/Resources/i18n/he_IL/DirectAccessReport.json',
|
|
'custom/Espo/Custom/Resources/i18n/he_IL/LegalAid.json',
|
|
'custom/Espo/Custom/Resources/i18n/en_US/DirectAccessReport.json',
|
|
'custom/Espo/Custom/Resources/i18n/en_US/LegalAid.json',
|
|
];
|
|
|
|
foreach ($files as $file) {
|
|
if (file_exists($file)) {
|
|
unlink($file);
|
|
$log->info("LegalAssistance: Removed {$file}");
|
|
}
|
|
}
|
|
}
|
|
}
|