From fdb7c15f85773d10c05fdb593b9fe53defe9c50b Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Fri, 5 Jun 2026 07:12:58 +0530 Subject: [PATCH] Fix discharge template page edit flow and AJAX save/delete --- app/Config/Routes.php | 1 + app/Controllers/Setting/Template.php | 89 ++++- .../Setting/Template/discharge_templates.php | 309 +++++++++++++++++- 3 files changed, 385 insertions(+), 14 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 2c019f1..5114e6e 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -863,6 +863,7 @@ $routes->post('Lab_Admin/pathology_master_add_component', 'Setting\Template::pat $routes->get('Lab_Admin/pathology_bridge_debug', 'Setting\Template::pathologyBridgeDebug'); $routes->match(['get', 'post'], 'setting/template/discharge_templates', 'Setting\Template::discharge_templates'); +$routes->get('setting/template/discharge_template_get/(:num)', 'Setting\Template::discharge_template_get/$1'); $routes->get('setting/template/discharge_templates/delete/(:num)', 'Setting\Template::discharge_template_delete/$1'); $routes->match(['get', 'post'], 'setting/template/ipd_document_templates', 'Setting\Template::ipd_document_templates'); $routes->get('setting/template/ipd_document_templates/delete/(:num)', 'Setting\Template::ipd_document_template_delete/$1'); diff --git a/app/Controllers/Setting/Template.php b/app/Controllers/Setting/Template.php index ebba3f5..e969273 100644 --- a/app/Controllers/Setting/Template.php +++ b/app/Controllers/Setting/Template.php @@ -2787,6 +2787,15 @@ HTML; if ($templateName === '' || trim($templateHtml) === '') { $notice = 'Template name and template HTML are required.'; $noticeType = 'danger'; + + if ($this->request->isAJAX()) { + return $this->response->setJSON([ + 'update' => 0, + 'error_text' => $notice, + 'csrfName' => csrf_token(), + 'csrfHash' => csrf_hash(), + ]); + } } else { $table = $this->db->table('ipd_discharge_templates'); $data = [ @@ -2808,20 +2817,33 @@ HTML; 'status' => $status, ]; + $ok = false; if ($id > 0) { - $table->where('id', $id)->update($data); - $notice = 'Template updated.'; + $ok = (bool) $table->where('id', $id)->update($data); + $notice = $ok ? 'Template updated.' : 'Unable to update template.'; } else { - $table->insert($data); + $ok = (bool) $table->insert($data); $id = (int) $this->db->insertID(); - $notice = 'Template created.'; + $notice = $ok ? 'Template created.' : 'Unable to create template.'; } + $noticeType = $ok ? 'success' : 'danger'; + if ($isDefault === 1 && $id > 0) { $this->db->table('ipd_discharge_templates') ->where('id <>', $id) ->update(['is_default' => 0]); } + + if ($this->request->isAJAX()) { + return $this->response->setJSON([ + 'update' => $ok ? 1 : 0, + 'id' => $id, + 'error_text' => $notice, + 'csrfName' => csrf_token(), + 'csrfHash' => csrf_hash(), + ]); + } } } @@ -2848,6 +2870,53 @@ HTML; ]); } + public function discharge_template_get(int $id) + { + if ($resp = $this->requireAnyPermission(['template.discharge'])) { + return $resp; + } + + if (! $this->request->isAJAX()) { + return $this->response->setStatusCode(400)->setJSON([ + 'update' => 0, + 'error_text' => 'Invalid request', + 'csrfName' => csrf_token(), + 'csrfHash' => csrf_hash(), + ]); + } + + $id = (int) $id; + if ($id <= 0 || ! $this->db->tableExists('ipd_discharge_templates')) { + return $this->response->setJSON([ + 'update' => 0, + 'error_text' => 'Template not found', + 'csrfName' => csrf_token(), + 'csrfHash' => csrf_hash(), + ]); + } + + $row = $this->db->table('ipd_discharge_templates') + ->where('id', $id) + ->get(1) + ->getRowArray() ?? []; + + if (empty($row)) { + return $this->response->setJSON([ + 'update' => 0, + 'error_text' => 'Template not found', + 'csrfName' => csrf_token(), + 'csrfHash' => csrf_hash(), + ]); + } + + return $this->response->setJSON([ + 'update' => 1, + 'row' => $row, + 'csrfName' => csrf_token(), + 'csrfHash' => csrf_hash(), + ]); + } + public function discharge_template_delete(int $id) { if ($resp = $this->requireAnyPermission(['template.discharge'])) { @@ -2855,8 +2924,18 @@ HTML; } $id = (int) $id; + $ok = false; if ($id > 0 && $this->db->tableExists('ipd_discharge_templates')) { - $this->db->table('ipd_discharge_templates')->where('id', $id)->delete(); + $ok = (bool) $this->db->table('ipd_discharge_templates')->where('id', $id)->delete(); + } + + if ($this->request->isAJAX()) { + return $this->response->setJSON([ + 'update' => $ok ? 1 : 0, + 'error_text' => $ok ? 'Template deleted.' : 'Unable to delete template.', + 'csrfName' => csrf_token(), + 'csrfHash' => csrf_hash(), + ]); } return redirect()->to(base_url('setting/template/discharge_templates')); diff --git a/app/Views/Setting/Template/discharge_templates.php b/app/Views/Setting/Template/discharge_templates.php index 3d56caf..c20d91c 100644 --- a/app/Views/Setting/Template/discharge_templates.php +++ b/app/Views/Setting/Template/discharge_templates.php @@ -66,9 +66,11 @@ $status = (int) ($edit['status'] ?? 1); +
+
- +
@@ -155,10 +157,8 @@ $status = (int) ($edit['status'] ?? 1);
- - 0): ?> - Cancel Edit - + +
@@ -184,8 +184,8 @@ $status = (int) ($edit['status'] ?? 1); - Edit - Delete + + @@ -200,15 +200,205 @@ $status = (int) ($edit['status'] ?? 1);