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" + ]; } }