fix(discharge-template): resolve placeholders with flexible token matching

This commit is contained in:
DevSoftTech Server 2026-07-31 17:51:01 +05:30
parent 49a2027778
commit fb7ea94157

View File

@ -942,13 +942,34 @@ class Ipd_discharge extends BaseController
$templateHtml = $this->normalizeLegacyDischargeTemplate($templateHtml); $templateHtml = $this->normalizeLegacyDischargeTemplate($templateHtml);
$templateHtml = $this->normalizeDischargeTemplatePlaceholders($templateHtml); $templateHtml = $this->normalizeDischargeTemplatePlaceholders($templateHtml);
$replace = []; $rendered = (string) preg_replace_callback(
foreach ($vars as $key => $value) { '/\{\{\s*([A-Za-z0-9_]+)\s*\}\}|\{\s*([A-Za-z0-9_]+)\s*\}/',
$replace['{{' . $key . '}}'] = (string) $value; static function (array $m) use ($vars): string {
$replace['{' . $key . '}'] = (string) $value; $token = (string) (($m[1] ?? '') !== '' ? $m[1] : ($m[2] ?? ''));
} if ($token === '') {
return (string) ($m[0] ?? '');
}
return strtr($templateHtml, $replace); $lower = strtolower($token);
$candidates = [
$token,
strtoupper($token),
$lower,
ucfirst($lower),
];
foreach ($candidates as $candidate) {
if (array_key_exists($candidate, $vars)) {
return (string) ($vars[$candidate] ?? '');
}
}
return (string) ($m[0] ?? '');
},
$templateHtml
);
return $rendered;
} }
private function normalizeDischargeTemplatePlaceholders(string $templateHtml): string private function normalizeDischargeTemplatePlaceholders(string $templateHtml): string
@ -1146,14 +1167,30 @@ class Ipd_discharge extends BaseController
return false; return false;
} }
foreach (array_keys($tokenVars) as $tokenName) { $matches = [];
$tokenName = (string) $tokenName; $matchCount = preg_match_all('/\{\{\s*([A-Za-z0-9_]+)\s*\}\}|\{\s*([A-Za-z0-9_]+)\s*\}/', $templateHtml, $matches, PREG_SET_ORDER);
if ($matchCount === false || $matchCount === 0) {
return false;
}
foreach ($matches as $match) {
$tokenName = (string) (($match[1] ?? '') !== '' ? $match[1] : ($match[2] ?? ''));
if ($tokenName === '') { if ($tokenName === '') {
continue; continue;
} }
if (strpos($templateHtml, '{{' . $tokenName . '}}') !== false || strpos($templateHtml, '{' . $tokenName . '}') !== false) { $lower = strtolower($tokenName);
return true; $candidates = [
$tokenName,
strtoupper($tokenName),
$lower,
ucfirst($lower),
];
foreach ($candidates as $candidate) {
if (array_key_exists($candidate, $tokenVars)) {
return true;
}
} }
} }
@ -1182,11 +1219,7 @@ class Ipd_discharge extends BaseController
return false; return false;
} }
$templateHasMeta = strpos($templateHtml, '{{PATIENT_NAME}}') !== false $templateHasMeta = preg_match('/\{\{\s*(PATIENT_NAME|UHID|IPD_CODE|ADMIT_DATE|DISCHARGE_DATE)\s*\}\}|\{\s*(PATIENT_NAME|UHID|IPD_CODE|ADMIT_DATE|DISCHARGE_DATE)\s*\}/i', $templateHtml) === 1;
|| strpos($templateHtml, '{{UHID}}') !== false
|| strpos($templateHtml, '{{IPD_CODE}}') !== false
|| strpos($templateHtml, '{{ADMIT_DATE}}') !== false
|| strpos($templateHtml, '{{DISCHARGE_DATE}}') !== false;
if (! $templateHasMeta) { if (! $templateHasMeta) {
return false; return false;