From 7eff3e6c6626b726d72d0fb5ec1f5867cc38e134 Mon Sep 17 00:00:00 2001 From: Pinga <121483313+getpinga@users.noreply.github.com> Date: Fri, 20 Sep 2024 16:11:42 +0300 Subject: [PATCH] Added ability to export list of domain names, fixed #165 --- cp/app/Controllers/ReportsController.php | 45 +++++++++++++++++++++ cp/resources/views/admin/reports/index.twig | 12 ++++++ cp/routes/web.php | 2 + 3 files changed, 59 insertions(+) diff --git a/cp/app/Controllers/ReportsController.php b/cp/app/Controllers/ReportsController.php index c27bd7a..c4dd62a 100644 --- a/cp/app/Controllers/ReportsController.php +++ b/cp/app/Controllers/ReportsController.php @@ -6,6 +6,7 @@ use App\Models\RegistryTransaction; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Container\ContainerInterface; +use Nyholm\Psr7\Stream; class ReportsController extends Controller { @@ -17,4 +18,48 @@ class ReportsController extends Controller return view($response,'admin/reports/index.twig'); } + + public function exportDomains(Request $request, Response $response) + { + if ($_SESSION["auth_roles"] != 0) { + return $response->withHeader('Location', '/dashboard')->withStatus(302); + } + + // Fetch domain data from the database + $db = $this->container->get('db'); + $domains = $db->select("SELECT name, crdate, exdate FROM domain"); + + // Create a temporary memory file for the CSV data + $csvFile = fopen('php://memory', 'w'); + + // Define CSV headers + $headers = ['Domain Name', 'Creation Date', 'Expiration Date']; + + // Write the headers to the CSV file + fputcsv($csvFile, $headers); + + // Write the domain data to the CSV file + foreach ($domains as $domain) { + fputcsv($csvFile, [ + $domain['name'], + $domain['crdate'], + $domain['exdate'] + ]); + } + + // Rewind the file pointer to the beginning of the file + fseek($csvFile, 0); + + // Create a stream from the in-memory file + $stream = Stream::create($csvFile); + + // Prepare the response headers for CSV file download + $response = $response->withHeader('Content-Type', 'text/csv') + ->withHeader('Content-Disposition', 'attachment; filename="domains_export.csv"') + ->withHeader('Pragma', 'no-cache') + ->withHeader('Expires', '0'); + + // Output the CSV content to the response body + return $response->withBody($stream); + } } \ No newline at end of file diff --git a/cp/resources/views/admin/reports/index.twig b/cp/resources/views/admin/reports/index.twig index 44bba6a..7ecab6f 100644 --- a/cp/resources/views/admin/reports/index.twig +++ b/cp/resources/views/admin/reports/index.twig @@ -17,6 +17,18 @@ {{ __('Reports') }} + +
+
+ + + {{ __('Export Domains') }} + + + + +
+
diff --git a/cp/routes/web.php b/cp/routes/web.php index 6b28815..7ce14fb 100644 --- a/cp/routes/web.php +++ b/cp/routes/web.php @@ -103,7 +103,9 @@ $app->group('', function ($route) { $route->get('/epphistory', LogsController::class .':view')->setName('epphistory'); $route->get('/poll', LogsController::class .':poll')->setName('poll'); $route->get('/log', LogsController::class .':log')->setName('log'); + $route->get('/reports', ReportsController::class .':view')->setName('reports'); + $route->get('/export', ReportsController::class .':exportDomains')->setName('exportDomains'); $route->get('/invoices', FinancialsController::class .':invoices')->setName('invoices'); $route->get('/invoice/{invoice}', FinancialsController::class . ':viewInvoice')->setName('viewInvoice');