Added ability to export list of domain names, fixed #165

This commit is contained in:
Pinga 2024-09-20 16:11:42 +03:00
parent f497f7ada7
commit 7eff3e6c66
3 changed files with 59 additions and 0 deletions

View file

@ -6,6 +6,7 @@ use App\Models\RegistryTransaction;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Nyholm\Psr7\Stream;
class ReportsController extends Controller class ReportsController extends Controller
{ {
@ -17,4 +18,48 @@ class ReportsController extends Controller
return view($response,'admin/reports/index.twig'); 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);
}
} }

View file

@ -17,6 +17,18 @@
{{ __('Reports') }} {{ __('Reports') }}
</h2> </h2>
</div> </div>
<!-- Page title actions -->
<div class="col-auto ms-auto d-print-none">
<div class="btn-list">
<a href="{{route('exportDomains')}}" class="btn btn-info d-none d-sm-inline-block">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M4 6c0 1.657 3.582 3 8 3s8 -1.343 8 -3s-3.582 -3 -8 -3s-8 1.343 -8 3" /><path d="M4 6v6c0 1.657 3.582 3 8 3c1.118 0 2.183 -.086 3.15 -.241" /><path d="M20 12v-6" /><path d="M4 12v6c0 1.657 3.582 3 8 3c.157 0 .312 -.002 .466 -.005" /><path d="M16 19h6" /><path d="M19 16l3 3l-3 3" /></svg>
{{ __('Export Domains') }}
</a>
<a href="{{route('exportDomains')}}" class="btn btn-info d-sm-none btn-icon" aria-label="{{ __('Export Domains') }}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M4 6c0 1.657 3.582 3 8 3s8 -1.343 8 -3s-3.582 -3 -8 -3s-8 1.343 -8 3" /><path d="M4 6v6c0 1.657 3.582 3 8 3c1.118 0 2.183 -.086 3.15 -.241" /><path d="M20 12v-6" /><path d="M4 12v6c0 1.657 3.582 3 8 3c.157 0 .312 -.002 .466 -.005" /><path d="M16 19h6" /><path d="M19 16l3 3l-3 3" /></svg>
</a>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

View file

@ -103,7 +103,9 @@ $app->group('', function ($route) {
$route->get('/epphistory', LogsController::class .':view')->setName('epphistory'); $route->get('/epphistory', LogsController::class .':view')->setName('epphistory');
$route->get('/poll', LogsController::class .':poll')->setName('poll'); $route->get('/poll', LogsController::class .':poll')->setName('poll');
$route->get('/log', LogsController::class .':log')->setName('log'); $route->get('/log', LogsController::class .':log')->setName('log');
$route->get('/reports', ReportsController::class .':view')->setName('reports'); $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('/invoices', FinancialsController::class .':invoices')->setName('invoices');
$route->get('/invoice/{invoice}', FinancialsController::class . ':viewInvoice')->setName('viewInvoice'); $route->get('/invoice/{invoice}', FinancialsController::class . ':viewInvoice')->setName('viewInvoice');