From 6d72a5ec04c9d8d3ac300f8ade021b72bfbc453a Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Sat, 6 Jun 2026 12:09:57 +0530 Subject: [PATCH] Correct fix: Remove patient table from auto-generated content, add as token OLD CI3 BEHAVIOR (correct): - Patient demographic table was IN the template file - Content variables contained ONLY clinical sections - Template controlled entire layout NEW CI4 FIX (matching CI3): - Patient table removed from buildAutoDischargeContent completely - Added {{PATIENT_INFO_TABLE}} token for templates to use if needed - Auto-generated content now contains ONLY clinical sections - Blank template = blank output (no patient table forced) - Templates can add patient table using token or individual tokens Changes: - Removed includeSummaryTable parameter from buildAutoDischargeContent - Removed templateHasDemographicTokens helper (no longer needed) - Added PATIENT_INFO_TABLE token in buildDischargeTemplateTokenVars - Simplified show_discharge and preview_discharge_report (no template detection) - Added comment explaining CI3 compatibility This matches the old CI3 chamunda_discharge_template_html.php pattern where: - Template had hardcoded patient table structure - Variables like \, \ were injected - Content did NOT include patient demographics by default --- app/Controllers/Ipd_discharge.php | 109 +++--------------------------- 1 file changed, 10 insertions(+), 99 deletions(-) diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index 76f21e7..79ab50a 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -372,6 +372,9 @@ class Ipd_discharge extends BaseController 'hospital_email' => esc($hEmail), ]; + // Add patient info table as a token (not included in CONTENT by default) + $tokens['PATIENT_INFO_TABLE'] = $this->buildAutoDischargeSummaryTable($panelData); + $summaryTokens = $this->buildDischargeSummaryTokenVars($panelData); foreach ($summaryTokens as $key => $value) { $tokens[$key] = $value; @@ -909,34 +912,6 @@ class Ipd_discharge extends BaseController return 'user-' . (string) ($user->id ?? 0); } - private function templateHasDemographicTokens(string $templateHtml): bool - { - $templateHtml = trim($templateHtml); - if ($templateHtml === '' || $templateHtml === '{{CONTENT}}') { - return false; - } - - $demographicTokens = [ - '{{PATIENT_NAME}}', - '{{UHID}}', - '{{IPD_CODE}}', - '{{AGE_GENDER}}', - '{{ADMIT_DATE}}', - '{{DISCHARGE_DATE}}', - '{{GUARDIAN}}', - '{{PATIENT_ADDRESS}}', - '{{DEPARTMENT}}', - ]; - - foreach ($demographicTokens as $token) { - if (stripos($templateHtml, $token) !== false) { - return true; - } - } - - return false; - } - private function saveDischargeContent(int $ipdId, string $content): bool { try { @@ -1446,7 +1421,7 @@ class Ipd_discharge extends BaseController return '

' . esc($title) . ' :
' . implode('
', $lines) . '

'; } - private function buildAutoDischargeContent(int $ipdId, array $panelData, bool $includeSummaryTable = true): string + private function buildAutoDischargeContent(int $ipdId, array $panelData): string { $ipd = $panelData['ipd_info'] ?? null; $person = $panelData['person_info'] ?? null; @@ -1478,13 +1453,9 @@ class Ipd_discharge extends BaseController $sections = []; - // Only include summary table if explicitly requested - if ($includeSummaryTable) { - $summaryTable = $this->buildAutoDischargeSummaryTable($panelData); - if ($summaryTable !== '') { - $sections[] = $summaryTable; - } - } + // NOTE: Patient demographic table is NOT included in auto-generated content. + // It is available as {{PATIENT_INFO_TABLE}} token for templates to use if needed. + // This matches CI3 behavior where template controlled the patient info display. $complaints = $this->byIpdRows('ipd_discharge_complaint', ['comp_report', 'comp_remark'], 'id ASC', $ipdId); $complaintRemark = $this->firstRowByIpd('ipd_discharge_complaint_remark', $ipdId); @@ -5401,38 +5372,8 @@ class Ipd_discharge extends BaseController $shouldRegenerate = (int) ($this->request->getGet('regen') ?? 0) === 1; $requestedTemplateId = (int) ($this->request->getGet('tpl') ?? 0); - // Get requested template to check if it has demographic tokens - $templates = $this->getDischargeTemplateRows(); - $selectedTemplate = null; - - if ($requestedTemplateId > 0) { - foreach ($templates as $row) { - if ((int) ($row['id'] ?? 0) === $requestedTemplateId) { - $selectedTemplate = $row; - break; - } - } - } - - if ($selectedTemplate === null) { - foreach ($templates as $row) { - if ((int) ($row['is_default'] ?? 0) === 1) { - $selectedTemplate = $row; - break; - } - } - } - - if ($selectedTemplate === null && ! empty($templates)) { - $selectedTemplate = $templates[0]; - } - - $templateHtml = (string) ($selectedTemplate['template_html'] ?? '{{CONTENT}}'); - $templateHasDemographics = $this->templateHasDemographicTokens($templateHtml); - if ($shouldRegenerate || trim(strip_tags($content)) === '') { - // Skip auto-summary table if template has demographic tokens - $generated = $this->buildAutoDischargeContent($ipdId, $panelData, !$templateHasDemographics); + $generated = $this->buildAutoDischargeContent($ipdId, $panelData); if (trim(strip_tags($generated)) !== '') { $content = $generated; if ($this->saveDischargeContent($ipdId, $content)) { @@ -5483,45 +5424,15 @@ class Ipd_discharge extends BaseController return $this->response->setStatusCode(404)->setBody('IPD not found'); } - // Get requested template first to check if it has demographic tokens - $requestedTemplateId = (int) ($this->request->getGet('tpl') ?? 0); - $templates = $this->getDischargeTemplateRows(); - $selectedTemplate = null; - - if ($requestedTemplateId > 0) { - foreach ($templates as $row) { - if ((int) ($row['id'] ?? 0) === $requestedTemplateId) { - $selectedTemplate = $row; - break; - } - } - } - - if ($selectedTemplate === null) { - foreach ($templates as $row) { - if ((int) ($row['is_default'] ?? 0) === 1) { - $selectedTemplate = $row; - break; - } - } - } - - if ($selectedTemplate === null && ! empty($templates)) { - $selectedTemplate = $templates[0]; - } - - $templateHtml = (string) ($selectedTemplate['template_html'] ?? '{{CONTENT}}'); - $templateHasDemographics = $this->templateHasDemographicTokens($templateHtml); - $content = $this->getDischargeContent($ipdId); if (trim(strip_tags($content)) === '') { - // Skip auto-summary table if template has demographic tokens - $content = $this->buildAutoDischargeContent($ipdId, $panelData, !$templateHasDemographics); + $content = $this->buildAutoDischargeContent($ipdId, $panelData); if (trim(strip_tags($content)) !== '') { $this->saveDischargeContent($ipdId, $content); } } + $requestedTemplateId = (int) ($this->request->getGet('tpl') ?? 0); $templatePack = $this->applyDischargeTemplate($content, $panelData, $requestedTemplateId > 0 ? $requestedTemplateId : null); $templateSettings = is_array($templatePack['selected_template_settings'] ?? null) ? $templatePack['selected_template_settings']