From eab11dba95b23ebba794d343ecf6cb98703a381c Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Wed, 11 Mar 2026 09:36:37 +0700 Subject: [PATCH] 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. --- app/Controllers/Pages/SuperuserController.php | 59 +++++++++++-------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/app/Controllers/Pages/SuperuserController.php b/app/Controllers/Pages/SuperuserController.php index 5283cf9..a33b7a5 100644 --- a/app/Controllers/Pages/SuperuserController.php +++ b/app/Controllers/Pages/SuperuserController.php @@ -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" + ]; } }