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}"); } } } }