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/AfterUninstall.php
T
chaim 21cf570cbc feat!: v2.0.0 — replace LegalAid entity with direct JSON-to-DOCX generation
Breaking change: LegalAid entity removed entirely. Reports are now
generated directly from JSON params to DOCX without any intermediate
entity or database table.

New:
- DirectAccessReportGenerator: takes JSON from Shira, generates DOCX
  via PHPWord, attaches as Document to Case (single PHP file)
- AfterUninstall: full cleanup script (drops tables, columns, templates)
- docs/examples/entity-reference-legalaid.md: full reference for future
  entity creation

Removed:
- LegalAid entity (entityDefs, scopes, clientDefs, layouts, i18n, controller)
- GenerateInitialReport tool (replaced by DirectAccessReportGenerator)
- Custom JS field view for cLegalAidNumber
- update_legal_aid and get_legal_aid tools
- Case link to legalAids

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

175 lines
6.2 KiB
PHP

<?php
/************************************************************************
* LegalAssistance Extension — AfterUninstall
*
* 1. Remove plugin files from ai-gateway
* 2. Drop LegalAid and DirectAccessReport DB tables (with warning log)
* 3. Remove LegalAssistance-specific columns from Case table
* 4. Remove DocumentTemplate records created by this extension
* 5. Clear cache
************************************************************************/
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. Remove ai-gateway plugin files
$this->removePluginFiles($log);
// 2. Drop tables created by this extension
$this->dropTables($entityManager, $log);
// 3. Remove LegalAssistance-specific columns from Case
$this->cleanCaseColumns($entityManager, $log);
// 4. Remove DocumentTemplate records
$this->removeDocumentTemplates($entityManager, $log);
// 5. Remove custom i18n files
$this->removeCustomI18n($log);
// 6. 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 removePluginFiles(Log $log): void
{
$gatewayPaths = [
'/home/chaim/ai-gateway/plugins/legal-assistance',
'/opt/ai-gateway/plugins/legal-assistance',
];
foreach ($gatewayPaths as $dir) {
if (is_dir($dir)) {
$files = glob($dir . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
rmdir($dir);
$log->info("LegalAssistance: Removed plugin files from {$dir}");
}
}
}
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}");
}
}
}
}