From a602bf555ddb54926560c4696946966f3d072780 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Mon, 22 Jun 2026 02:07:49 +0530 Subject: [PATCH] feat(discharge): unify history sections into single risk profile panel --- app/Controllers/Ipd_discharge.php | 126 ++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 5 deletions(-) diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index 027e806..a746ee9 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -695,6 +695,7 @@ class Ipd_discharge extends BaseController 'PAIN_MEASUREMENT_SCALE' => $painMeasurement, 'DRUG_ALLERGY_ADR' => $drugAllergyAdr, 'CO_MORBIDITIES' => $coMorbidities, + 'CLINICAL_HISTORY_RISK_PROFILE' => $this->buildClinicalHistoryRiskProfileSection($coMorbidities, $drugAllergyAdr, $personalHistory), 'SIGNATURE_BLOCK' => '', // Legacy style aliases to ease migration from CI3-style template variables. 'FinalDiagnosis' => $finalDiagnosis, @@ -767,6 +768,103 @@ class Ipd_discharge extends BaseController return trim(substr($html, $sliceStart, $sliceEnd - $sliceStart)); } + private function sectionHtmlToItems(string $html, array $labelHints = []): array + { + $value = trim($html); + if ($value === '') { + return []; + } + + $value = (string) preg_replace('/<\s*br\s*\/?>/i', "\n", $value); + $value = (string) preg_replace('/<\s*\/\s*(?:p|div|li|tr|h[1-6])\s*>/i', "\n", $value); + $text = html_entity_decode(strip_tags($value), ENT_QUOTES | ENT_HTML5, 'UTF-8'); + $text = (string) preg_replace('/\r\n?|\t/u', "\n", $text); + $text = trim($text); + + if ($text === '') { + return []; + } + + $parts = preg_split('/[\n,;\|]+/u', $text) ?: []; + $items = []; + foreach ($parts as $part) { + $item = trim((string) $part, " \t\n\r\0\x0B:-"); + if ($item === '') { + continue; + } + + foreach ($labelHints as $labelHint) { + $pattern = '/^' . preg_quote((string) $labelHint, '/') . '\s*:?\s*/iu'; + $item = (string) preg_replace($pattern, '', $item); + } + + $item = trim($item, " \t\n\r\0\x0B:-"); + if ($item === '') { + continue; + } + + $key = $this->normalizeHistoryItemKey($item); + if ($key === '') { + continue; + } + + $items[$key] = $item; + } + + return array_values($items); + } + + private function normalizeHistoryItemKey(string $item): string + { + $value = strtolower(trim($item)); + if ($value === '') { + return ''; + } + + $value = (string) preg_replace('/[^a-z0-9]+/i', '', $value); + + return $value; + } + + private function buildClinicalHistoryRiskProfileSection(string $coMorbiditiesHtml, string $drugAllergyHtml, string $personalHistoryHtml): string + { + $coMorbidityItems = $this->sectionHtmlToItems($coMorbiditiesHtml, ['Co-Morbidities']); + $allergyItems = $this->sectionHtmlToItems($drugAllergyHtml, ['Drug Allergy / ADR', 'Drug Allergy', 'ADR History']); + $personalItems = $this->sectionHtmlToItems($personalHistoryHtml, ['Personal History']); + + $occupiedKeys = []; + foreach (array_merge($coMorbidityItems, $allergyItems) as $item) { + $occupiedKeys[$this->normalizeHistoryItemKey($item)] = true; + } + + $filteredPersonal = []; + foreach ($personalItems as $item) { + $key = $this->normalizeHistoryItemKey($item); + if ($key === '' || isset($occupiedKeys[$key])) { + continue; + } + + $filteredPersonal[] = $item; + } + + if (empty($coMorbidityItems) && empty($allergyItems) && empty($filteredPersonal)) { + return ''; + } + + $html = '

Clinical History and Risk Profile

'; + if (! empty($coMorbidityItems)) { + $html .= '
Comorbidities: ' . esc(implode(', ', $coMorbidityItems)) . '
'; + } + if (! empty($allergyItems)) { + $html .= '
Drug Allergy and ADR History: ' . esc(implode(', ', $allergyItems)) . '
'; + } + if (! empty($filteredPersonal)) { + $html .= '
Lifestyle and Personal History: ' . esc(implode(', ', $filteredPersonal)) . '
'; + } + + return $html; + } + private function normalizeLegacyDischargeTemplate(string $html): string { $out = str_replace(["\r\n", "\r"], "\n", $html); @@ -892,6 +990,29 @@ class Ipd_discharge extends BaseController $tokenVars = $this->buildDischargeTemplateTokenVars($panelData, $content); + $historyPanelHtml = trim((string) ($tokenVars['CLINICAL_HISTORY_RISK_PROFILE'] ?? '')); + if ($historyPanelHtml !== '') { + $historyTokens = ['PERSONAL_HISTORY', 'CO_MORBIDITIES', 'DRUG_ALLERGY_ADR']; + $firstToken = null; + $firstPos = null; + foreach ($historyTokens as $token) { + if (preg_match('/\{\{?\s*' . preg_quote($token, '/') . '\s*\}?\}/i', $templateHtml, $m, PREG_OFFSET_CAPTURE) === 1) { + $pos = (int) ($m[0][1] ?? 0); + if ($firstPos === null || $pos < $firstPos) { + $firstPos = $pos; + $firstToken = $token; + } + } + } + + if ($firstToken !== null) { + foreach ($historyTokens as $token) { + $tokenVars[$token] = ''; + } + $tokenVars[$firstToken] = $historyPanelHtml; + } + } + $templateHasMainSummaryToken = preg_match('/\{\{\s*DISCHARGE_SUMMARY\s*\}\}|\{\s*DISCHARGE_SUMMARY\s*\}/i', $templateHtml) === 1; if ($templateHasMainSummaryToken) { $tokenVars['DISCHARGE_ADVICE'] = ''; @@ -2186,11 +2307,6 @@ class Ipd_discharge extends BaseController 'is_alcohol' => 'Alcohol', 'is_drug_abuse' => 'Drug abuse', 'is_tobacoo' => 'Tobacco', - 'is_hypertesion' => 'Hypertension', - 'is_niddm' => 'Type 2 diabetes mellitus (DM)', - 'is_hbsag' => 'HBsAg', - 'is_hcv' => 'HCV', - 'is_hiv_I_II' => 'HIV I & II', 'Others' => 'Others', ];