fix: use cURL instead of direct controller instantiation for PDF batch

Fixes 'Call to a member function getIPAddress() on null' error by calling
report/{id}/pdf endpoint via HTTP request instead of instantiating
ReportController directly, which ensures proper request initialization.
This commit is contained in:
mahdahar 2026-03-11 09:36:37 +07:00
parent 0fad3baab7
commit eab11dba95

View File

@ -117,40 +117,49 @@ class SuperuserController extends BaseController
{ {
$db = \Config\Database::connect(); $db = \Config\Database::connect();
// Get language preference
$sql = "SELECT REPORT_LANG FROM GDC_CMOD.dbo.CM_REQUESTS WHERE ACCESSNUMBER = ?"; $sql = "SELECT REPORT_LANG FROM GDC_CMOD.dbo.CM_REQUESTS WHERE ACCESSNUMBER = ?";
$row = $db->query($sql, [$accessnumber])->getRowArray(); $row = $db->query($sql, [$accessnumber])->getRowArray();
$eng = (int) ($row['REPORT_LANG'] ?? 0); $eng = (int) ($row['REPORT_LANG'] ?? 0);
// Load ReportController and call generatePdf $url = base_url("report/{$accessnumber}/pdf");
$reportController = new \App\Controllers\ReportController();
try { $ch = curl_init($url);
// Temporarily override the response to capture it curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = $reportController->generatePdf($accessnumber); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// Parse the response
$body = $response->getBody(); $cookieString = '';
$data = json_decode($body, true); foreach ($_COOKIE as $name => $value) {
$cookieString .= $name . '=' . $value . '; ';
if ($data && isset($data['success']) && $data['success']) { }
return [ curl_setopt($ch, CURLOPT_COOKIE, $cookieString);
'success' => true,
'lang' => $data['lang'] ?? ($eng == 1 ? 'English' : 'Indonesian'), $response = curl_exec($ch);
'isRegen' => $data['isRegen'] ?? false $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
]; $error = curl_error($ch);
} curl_close($ch);
if ($error) {
return [ return [
'success' => false, 'success' => false,
'error' => $data['error'] ?? 'Unknown error' 'error' => 'cURL error: ' . $error
];
} catch (\Throwable $e) {
return [
'success' => false,
'error' => $e->getMessage()
]; ];
} }
$data = json_decode($response, true);
if ($httpCode === 200 && $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'] ?? "HTTP {$httpCode}: Failed to generate PDF"
];
} }
} }