Add user-based sidebar auto-hide setting, movable from hospital default

- Create user_settings table to store individual user preferences
- UserSettings model for get/set/delete user setting values
- Home::myProfile loads user settings with hospital defaults fallback
- Home::myProfileSave persists user settings to DB
- my_profile.php adds Sidebar Auto-Hide Delay field (0-60 seconds)
- welcome_message.php checks user settings first, then hospital default
- Setting value 0 = auto-hide disabled; blank = use hospital default
- Extensible table design for future user settings (color theme, etc)
This commit is contained in:
DevSoftTech Server 2026-04-28 13:47:46 +05:30
parent 0aa0edbbce
commit 5717763496
5 changed files with 213 additions and 1 deletions

View File

@ -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 ?? ''));

View File

@ -0,0 +1,53 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateUserSettingsTable extends Migration
{
public function up()
{
$this->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);
}
}

View File

@ -0,0 +1,99 @@
<?php
namespace App\Models;
use CodeIgniter\Model;
class UserSettings extends Model
{
protected $table = 'user_settings';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $allowedFields = ['user_id', 'setting_key', 'setting_value'];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
/**
* Get user setting value by key.
*/
public function getUserSetting(int $userId, string $settingKey, string $default = ''): string
{
if ($userId <= 0 || $settingKey === '') {
return $default;
}
$row = $this->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();
}
}

View File

@ -68,6 +68,21 @@
<div class="small text-muted mt-2" id="profile_msg">Update your email, phone, and password here.</div>
</div>
</div>
<div class="card mt-3">
<div class="card-header"><strong>User Settings</strong></div>
<div class="card-body">
<div class="mb-3">
<label class="form-label">Sidebar Auto-Hide Delay (Seconds)</label>
<div class="input-group input-group-sm">
<input type="number" class="form-control" id="sidebar_auto_hide_seconds" min="0" max="60" step="1" value="<?= esc((string) ($sidebar_auto_hide_seconds ?? '7')) ?>" placeholder="0-60">
<span class="input-group-text"><small class="text-muted">Hospital default: <?= esc((string) hospital_setting_value('SIDEBAR_AUTO_HIDE_SECONDS', '7')) ?>s</small></span>
</div>
<div class="small text-muted mt-1">How many seconds before sidebar auto-hides. Enter 0 to disable auto-hide. Leave blank to use hospital default.</div>
</div>
<div class="small text-success" id="settings_msg" style="display:none;">Settings auto-saved.</div>
</div>
</div>
</div>
</div>
</section>
@ -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]) {

View File

@ -123,7 +123,24 @@
<script src="<?= base_url('assets/vendor/php-email-form/validate.js') ?>"></script>
<?php
// Get user settings (use hospital defaults as fallback)
$sidebarAutoHideSeconds = (int) hospital_setting_value('SIDEBAR_AUTO_HIDE_SECONDS', '7');
if (function_exists('auth') && auth()->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;
}