diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index d035579..aa7965c 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -234,13 +234,23 @@ class Home extends BaseController } $user = service('auth')->user(); - $meta = $this->getCurrentUserProfileMeta((int) ($user->id ?? 0)); + $userId = (int) ($user->id ?? 0); + $meta = $this->getCurrentUserProfileMeta($userId); + + // Load user settings (use hospital defaults if not set) + $userSettingsModel = model('UserSettings'); + $sidebarAutoHideSecondsUser = $userSettingsModel->getUserSetting( + $userId, + 'SIDEBAR_AUTO_HIDE_SECONDS', + hospital_setting_value('SIDEBAR_AUTO_HIDE_SECONDS', '7') + ); return view('account/my_profile', [ 'user' => $user, 'phone_no' => $meta['phone_no'] ?? '', 'person_name' => $meta['full_name'] ?? '', 'profile_photo_url' => $this->buildUserProfilePhotoUrl((string) ($meta['profile_photo'] ?? '')), + 'sidebar_auto_hide_seconds' => $sidebarAutoHideSecondsUser, ]); } @@ -579,6 +589,23 @@ class Home extends BaseController $photoProvided ? $profilePhotoFileName : null ); + // Save user settings + $sidebarAutoHideSeconds = (int) trim((string) $this->request->getPost('sidebar_auto_hide_seconds')); + if ($sidebarAutoHideSeconds < 0) { + $sidebarAutoHideSeconds = 0; + } + if ($sidebarAutoHideSeconds > 60) { + $sidebarAutoHideSeconds = 60; + } + + $userSettingsModel = model('UserSettings'); + if ($sidebarAutoHideSeconds > 0) { + $userSettingsModel->setUserSetting($userId, 'SIDEBAR_AUTO_HIDE_SECONDS', (string) $sidebarAutoHideSeconds); + } else { + // Delete if zero to use hospital default + $userSettingsModel->deleteUserSetting($userId, 'SIDEBAR_AUTO_HIDE_SECONDS'); + } + $metaAfterUpdate = $this->getCurrentUserProfileMeta($userId); $displayName = trim((string) ($user->username ?? '')); diff --git a/app/Database/Migrations/2026-04-28-000104_CreateUserSettingsTable.php b/app/Database/Migrations/2026-04-28-000104_CreateUserSettingsTable.php new file mode 100644 index 0000000..ea89b56 --- /dev/null +++ b/app/Database/Migrations/2026-04-28-000104_CreateUserSettingsTable.php @@ -0,0 +1,53 @@ +forge->addField([ + 'id' => [ + 'type' => 'INT', + 'constraint' => 11, + 'auto_increment' => true, + ], + 'user_id' => [ + 'type' => 'INT', + 'constraint' => 11, + 'null' => false, + ], + 'setting_key' => [ + 'type' => 'VARCHAR', + 'constraint' => 64, + 'null' => false, + ], + 'setting_value' => [ + 'type' => 'TEXT', + 'null' => true, + ], + 'created_at' => [ + 'type' => 'DATETIME', + 'null' => true, + ], + 'updated_at' => [ + 'type' => 'DATETIME', + 'null' => true, + ], + ]); + + $this->forge->addPrimaryKey('id'); + $this->forge->addUniqueKey(['user_id', 'setting_key'], 'uk_user_setting'); + $this->forge->addKey('user_id'); + $this->forge->addKey('setting_key'); + + $this->forge->createTable('user_settings', true); + } + + public function down() + { + $this->forge->dropTable('user_settings', true); + } +} diff --git a/app/Models/UserSettings.php b/app/Models/UserSettings.php new file mode 100644 index 0000000..db4a05d --- /dev/null +++ b/app/Models/UserSettings.php @@ -0,0 +1,99 @@ +where('user_id', $userId) + ->where('setting_key', $settingKey) + ->first(); + + if ($row && isset($row['setting_value'])) { + return (string) $row['setting_value']; + } + + return $default; + } + + /** + * Set user setting value. + */ + public function setUserSetting(int $userId, string $settingKey, string $value): bool + { + if ($userId <= 0 || $settingKey === '') { + return false; + } + + $existing = $this->where('user_id', $userId) + ->where('setting_key', $settingKey) + ->first(); + + if ($existing) { + return (bool) $this->update($existing['id'], ['setting_value' => $value]); + } + + return (bool) $this->insert([ + 'user_id' => $userId, + 'setting_key' => $settingKey, + 'setting_value' => $value, + ]); + } + + /** + * Get all settings for a user. + */ + public function getUserSettings(int $userId): array + { + if ($userId <= 0) { + return []; + } + + $rows = $this->where('user_id', $userId)->findAll(); + $result = []; + + foreach ($rows as $row) { + $result[$row['setting_key']] = $row['setting_value']; + } + + return $result; + } + + /** + * Delete a user setting. + */ + public function deleteUserSetting(int $userId, string $settingKey): bool + { + if ($userId <= 0 || $settingKey === '') { + return false; + } + + return (bool) $this->where('user_id', $userId) + ->where('setting_key', $settingKey) + ->delete(); + } +} diff --git a/app/Views/account/my_profile.php b/app/Views/account/my_profile.php index 934d4bc..69492c7 100644 --- a/app/Views/account/my_profile.php +++ b/app/Views/account/my_profile.php @@ -68,6 +68,21 @@
Update your email, phone, and password here.
+ +
+
User Settings
+
+
+ +
+ + Hospital default: s +
+
How many seconds before sidebar auto-hides. Enter 0 to disable auto-hide. Leave blank to use hospital default.
+
+ +
+
@@ -141,6 +156,7 @@ payload.append('phone_no', ($('#profile_phone_no').val() || '').trim()); payload.append('password', ($('#profile_password').val() || '').toString()); payload.append('password_confirm', ($('#profile_password_confirm').val() || '').toString()); + payload.append('sidebar_auto_hide_seconds', ($('#sidebar_auto_hide_seconds').val() || '').trim()); var photoInput = document.getElementById('profile_photo'); if (photoInput && photoInput.files && photoInput.files[0]) { diff --git a/app/Views/welcome_message.php b/app/Views/welcome_message.php index f16a9bd..c9805ac 100644 --- a/app/Views/welcome_message.php +++ b/app/Views/welcome_message.php @@ -123,7 +123,24 @@ loggedIn()) { + $authUser = auth()->user(); + if ($authUser && isset($authUser->id)) { + $userSettingsModel = model('App\Models\UserSettings'); + $userSidebarSetting = $userSettingsModel->getUserSetting( + (int) $authUser->id, + 'SIDEBAR_AUTO_HIDE_SECONDS', + '' + ); + if ($userSidebarSetting !== '') { + $sidebarAutoHideSeconds = (int) $userSidebarSetting; + } + } + } + if ($sidebarAutoHideSeconds < 0) { $sidebarAutoHideSeconds = 0; }