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();
// 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();
$url = base_url("report/{$accessnumber}/pdf");
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
];
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$cookieString = '';
foreach ($_COOKIE as $name => $value) {
$cookieString .= $name . '=' . $value . '; ';
}
curl_setopt($ch, CURLOPT_COOKIE, $cookieString);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
return [
'success' => false,
'error' => $data['error'] ?? 'Unknown error'
];
} catch (\Throwable $e) {
return [
'success' => false,
'error' => $e->getMessage()
'error' => 'cURL error: ' . $error
];
}
$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"
];
}
}