From ec6ceaea98d20bbf33c8acff2e8f4d72971e3033 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Tue, 28 Apr 2026 13:54:46 +0530 Subject: [PATCH] Add dedicated Save Settings button for user preferences - Add separate Save Settings button below User Settings card - Create myProfileSaveSettings controller method for dedicated settings save - Settings save endpoint: POST /my-profile/save-settings - Validates sidebar_auto_hide_seconds (0-60 range) - Provides immediate feedback: success/error message display - Separate from profile photo and password updates - Message auto-hides after 2 seconds on success --- app/Config/Routes.php | 1 + app/Controllers/Home.php | 56 +++++++++++++++++++++++++++++++ app/Views/account/my_profile.php | 57 +++++++++++++++++++++++++++++++- 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 1bd4305..8cde860 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -9,6 +9,7 @@ $routes->get('/', 'Home::index'); $routes->get('dashboard', 'Home::dashboard'); $routes->get('my-profile', 'Home::myProfile'); $routes->post('my-profile/save', 'Home::myProfileSave'); +$routes->post('my-profile/save-settings', 'Home::myProfileSaveSettings'); $routes->get('speech-test', 'Home::speechTest'); $routes->post('stt-proxy', 'Home::sttProxy'); $routes->get('stt-proxy/health', 'Home::sttProxyHealth'); diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index aa7965c..9e11cbb 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -630,6 +630,62 @@ class Home extends BaseController ]); } + public function myProfileSaveSettings() + { + if (! $this->request->is('post')) { + return $this->response->setStatusCode(405)->setJSON([ + 'success' => 0, + 'message' => 'Method not allowed.', + ]); + } + + $authUser = auth()->user(); + if (! $authUser) { + return $this->response->setStatusCode(401)->setJSON([ + 'success' => 0, + 'message' => 'Not authenticated.', + 'csrfName' => csrf_token(), + 'csrfHash' => csrf_hash(), + ]); + } + + $userId = (int) $authUser->id; + + // Process sidebar auto-hide setting + $sidebarAutoHideSecondsInput = trim((string) $this->request->getPost('sidebar_auto_hide_seconds')); + $userSettingsModel = model('UserSettings'); + + if ($sidebarAutoHideSecondsInput === '') { + // Empty means use hospital default + $userSettingsModel->deleteUserSetting($userId, 'SIDEBAR_AUTO_HIDE_SECONDS'); + $message = 'Settings saved. Using hospital default.'; + } else { + $sidebarAutoHideSeconds = (int) $sidebarAutoHideSecondsInput; + if ($sidebarAutoHideSeconds < 0) { + $sidebarAutoHideSeconds = 0; + } + if ($sidebarAutoHideSeconds > 60) { + $sidebarAutoHideSeconds = 60; + } + + if ($sidebarAutoHideSeconds > 0) { + $userSettingsModel->setUserSetting($userId, 'SIDEBAR_AUTO_HIDE_SECONDS', (string) $sidebarAutoHideSeconds); + $message = "Sidebar auto-hide set to {$sidebarAutoHideSeconds} seconds."; + } else { + // 0 means disable auto-hide + $userSettingsModel->setUserSetting($userId, 'SIDEBAR_AUTO_HIDE_SECONDS', '0'); + $message = 'Sidebar auto-hide disabled.'; + } + } + + return $this->response->setJSON([ + 'success' => 1, + 'message' => $message, + 'csrfName' => csrf_token(), + 'csrfHash' => csrf_hash(), + ]); + } + /** * @return array{phone_no:string, full_name:string, profile_photo:string} */ diff --git a/app/Views/account/my_profile.php b/app/Views/account/my_profile.php index 69492c7..8e8d381 100644 --- a/app/Views/account/my_profile.php +++ b/app/Views/account/my_profile.php @@ -80,7 +80,11 @@
How many seconds before sidebar auto-hides. Enter 0 to disable auto-hide. Leave blank to use hospital default.
- +
+ +
+ + @@ -212,5 +216,56 @@ setMsg('err', 'Unable to update profile'); }); }); + + $('#btn_settings_save').on('click', function() { + var sidebarAutoHideSeconds = ($('#sidebar_auto_hide_seconds').val() || '').trim(); + + // Validate input + if (sidebarAutoHideSeconds !== '') { + var val = parseInt(sidebarAutoHideSeconds, 10); + if (isNaN(val) || val < 0 || val > 60) { + $('#settings_error').text('Please enter a value between 0 and 60.').show(); + $('#settings_msg').hide(); + return; + } + } + + var payload = new window.FormData(); + payload.append('sidebar_auto_hide_seconds', sidebarAutoHideSeconds); + + var csrf = getCsrfPair(); + payload.append(csrf.name, csrf.value); + + var $btn = $(this); + $btn.prop('disabled', true).text('Saving...'); + + $.ajax({ + url: '', + method: 'POST', + data: payload, + processData: false, + contentType: false, + dataType: 'json' + }).done(function(data) { + updateCsrf(data); + $btn.prop('disabled', false).text('Save Settings'); + + if (parseInt(data.success || '0', 10) !== 1) { + $('#settings_error').text(data.message || 'Unable to save settings').show(); + $('#settings_msg').hide(); + return; + } + + $('#settings_msg').show(); + $('#settings_error').hide(); + setTimeout(function() { + $('#settings_msg').fadeOut(500); + }, 2000); + }).fail(function() { + $btn.prop('disabled', false).text('Save Settings'); + $('#settings_error').text('Unable to save settings').show(); + $('#settings_msg').hide(); + }); + }); })();