From 0fad3baab71353a90547d3a774343172ea10060c Mon Sep 17 00:00:00 2001
From: mahdahar <89adham@gmail.com>
Date: Wed, 11 Mar 2026 09:34:11 +0700
Subject: [PATCH] feat: add PDF batch generation page for superuser
- Add /superuser/pdf-batch page with textarea input for access numbers
- Create API endpoint /api/superuser/pdf-batch to process batch PDF generation
- Use report/{id}/pdf endpoint for authenticated PDF generation
- Show results table with success/failed status, language, and type (new/regen)
- Remove deprecated /batch/pdf endpoint
- Add PDF Batch menu item to superuser navigation
---
app/Config/Routes.php | 8 +-
app/Controllers/Pages/SuperuserController.php | 123 +++++++++++++
app/Views/shared/config.php | 1 +
app/Views/superuser/pdf_batch.php | 170 ++++++++++++++++++
4 files changed, 299 insertions(+), 3 deletions(-)
create mode 100644 app/Views/superuser/pdf_batch.php
diff --git a/app/Config/Routes.php b/app/Config/Routes.php
index bc3b95a..bc05ffd 100644
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -71,6 +71,11 @@ $routes->group('superuser', ['filter' => 'role:0'], function ($routes) {
$routes->get('', 'Pages\SuperuserController::index');
$routes->get('users', 'Pages\SuperuserController::users');
$routes->get('validate', 'Pages\SuperuserController::validatePage');
+ $routes->get('pdf-batch', 'Pages\SuperuserController::pdfBatch');
+});
+
+$routes->group('api', ['filter' => 'role:0'], function ($routes) {
+ $routes->post('superuser/pdf-batch', 'Pages\SuperuserController::processPdfBatch');
});
$routes->group('admin', ['filter' => 'role:1'], function ($routes) {
@@ -108,9 +113,6 @@ $routes->group('report', ['filter' => 'role:0,1,2,4'], function ($routes) {
$routes->get('report/status/(:any)', 'ReportController::checkPdfStatus/$1');
-// Batch PDF endpoint - no auth required (internal use only)
-$routes->get('batch/pdf/(:num)', 'ReportController::generatePdfNoAuth/$1');
-
// External PDF generator endpoint - no auth required
$routes->post('api/requests/(:any)/pdf', 'RequestsController::setPdfFlag/$1');
diff --git a/app/Controllers/Pages/SuperuserController.php b/app/Controllers/Pages/SuperuserController.php
index 329c8c7..5283cf9 100644
--- a/app/Controllers/Pages/SuperuserController.php
+++ b/app/Controllers/Pages/SuperuserController.php
@@ -30,4 +30,127 @@ class SuperuserController extends BaseController
return view('superuser/validate', ['roleConfig' => $config['superuser']]);
}
+ public function pdfBatch()
+ {
+ $config = require APPPATH . 'Views/shared/config.php';
+ return view('superuser/pdf_batch', ['roleConfig' => $config['superuser']]);
+ }
+
+ public function processPdfBatch()
+ {
+ $input = $this->request->getJSON(true);
+ $accessNumbers = $input['accessNumbers'] ?? [];
+
+ if (empty($accessNumbers)) {
+ return $this->response->setStatusCode(400)->setJSON([
+ 'success' => false,
+ 'message' => 'No access numbers provided'
+ ]);
+ }
+
+ $results = [];
+ $db = \Config\Database::connect();
+
+ foreach ($accessNumbers as $accessnumber) {
+ $accessnumber = trim($accessnumber);
+ if (empty($accessnumber)) {
+ continue;
+ }
+
+ try {
+ // Check if request exists
+ $sql = "SELECT ACCESSNUMBER, REPORT_LANG FROM GDC_CMOD.dbo.CM_REQUESTS WHERE ACCESSNUMBER = ?";
+ $row = $db->query($sql, [$accessnumber])->getRowArray();
+
+ if (!$row) {
+ $results[] = [
+ 'accessnumber' => $accessnumber,
+ 'success' => false,
+ 'error' => 'Access number not found',
+ 'lang' => null,
+ 'isRegen' => false
+ ];
+ continue;
+ }
+
+ // Call report/{accessnumber}/pdf endpoint internally
+ $response = $this->callReportPdfEndpoint($accessnumber);
+
+ if ($response['success']) {
+ $results[] = [
+ 'accessnumber' => $accessnumber,
+ 'success' => true,
+ 'error' => null,
+ 'lang' => $response['lang'] ?? 'Unknown',
+ 'isRegen' => $response['isRegen'] ?? false
+ ];
+ } else {
+ $results[] = [
+ 'accessnumber' => $accessnumber,
+ 'success' => false,
+ 'error' => $response['error'] ?? 'PDF generation failed',
+ 'lang' => null,
+ 'isRegen' => false
+ ];
+ }
+ } catch (\Throwable $e) {
+ $results[] = [
+ 'accessnumber' => $accessnumber,
+ 'success' => false,
+ 'error' => $e->getMessage(),
+ 'lang' => null,
+ 'isRegen' => false
+ ];
+ }
+ }
+
+ return $this->response->setJSON([
+ 'success' => true,
+ 'results' => $results,
+ 'total' => count($results),
+ 'successful' => count(array_filter($results, fn($r) => $r['success'])),
+ 'failed' => count(array_filter($results, fn($r) => !$r['success']))
+ ]);
+ }
+
+ private function callReportPdfEndpoint($accessnumber)
+ {
+ $db = \Config\Database::connect();
+
+ // Get language preference
+ $sql = "SELECT REPORT_LANG FROM GDC_CMOD.dbo.CM_REQUESTS WHERE ACCESSNUMBER = ?";
+ $row = $db->query($sql, [$accessnumber])->getRowArray();
+ $eng = (int) ($row['REPORT_LANG'] ?? 0);
+
+ // Load ReportController and call generatePdf
+ $reportController = new \App\Controllers\ReportController();
+
+ try {
+ // Temporarily override the response to capture it
+ $response = $reportController->generatePdf($accessnumber);
+
+ // Parse the response
+ $body = $response->getBody();
+ $data = json_decode($body, true);
+
+ if ($data && isset($data['success']) && $data['success']) {
+ return [
+ 'success' => true,
+ 'lang' => $data['lang'] ?? ($eng == 1 ? 'English' : 'Indonesian'),
+ 'isRegen' => $data['isRegen'] ?? false
+ ];
+ }
+
+ return [
+ 'success' => false,
+ 'error' => $data['error'] ?? 'Unknown error'
+ ];
+ } catch (\Throwable $e) {
+ return [
+ 'success' => false,
+ 'error' => $e->getMessage()
+ ];
+ }
+ }
+
}
diff --git a/app/Views/shared/config.php b/app/Views/shared/config.php
index 2c0c33c..59f3cd3 100644
--- a/app/Views/shared/config.php
+++ b/app/Views/shared/config.php
@@ -82,6 +82,7 @@ return [
['label' => 'Dashboard', 'href' => 'superuser', 'icon' => 'chart-bar'],
['label' => 'Validate', 'href' => 'superuser/validate', 'icon' => 'check-circle'],
['label' => 'Users', 'href' => 'superuser/users', 'icon' => 'users'],
+ ['label' => 'PDF Batch', 'href' => 'superuser/pdf-batch', 'icon' => 'file-pdf'],
['label' => 'Reports', 'href' => 'http://glenlis/report2/', 'icon' => 'book'],
],
],
diff --git a/app/Views/superuser/pdf_batch.php b/app/Views/superuser/pdf_batch.php
new file mode 100644
index 0000000..a741427
--- /dev/null
+++ b/app/Views/superuser/pdf_batch.php
@@ -0,0 +1,170 @@
+
+= $this->extend('shared/layout'); ?>
+
+= $this->section('content') ?>
+
+
+
+
+
+
+ PDF Batch Generation
+
+
+
+
+
+
+
+
+
+
+
+
+
Results
+
+
+
+ Success
+
+
+
+ Failed
+
+
+
+
+
+
+
+
+ | Status |
+ Access Number |
+ Language |
+ Type |
+ Error |
+
+
+
+
+
+ |
+
+ |
+ |
+ |
+
+
+ -
+ |
+ |
+
+
+
+
+
+
+
+
+
+
+= $this->endSection(); ?>
+
+= $this->section('script') ?>
+
+= $this->endSection(); ?>