# Settings System Usage Guide

## Overview
The application now has a dynamic settings system that allows you to configure various options from the Admin Settings page without modifying code.

## How Settings Work

### 1. Saving Settings
- Navigate to **Admin Panel → Settings**
- Modify any setting values
- Click "Save Settings"
- Settings are stored in the `settings` database table

### 2. Using Settings in Code

Settings are automatically loaded and available throughout the application via the `Settings` class.

#### Get a String Setting
```php
$appName = Settings::get('app_name', 'Default App Name');
$email = Settings::get('app_email', 'admin@example.com');
```

#### Get an Integer Setting
```php
$itemsPerPage = Settings::getInt('items_per_page', 20);
```

#### Get a Boolean Setting
```php
$notificationsEnabled = Settings::getBool('enable_notifications', true);
$maintenanceMode = Settings::getBool('maintenance_mode', false);
```

#### Get All Settings
```php
$allSettings = Settings::all();
```

## Available Settings

### Application Information
- `app_name` - Application name (displayed in header and page titles)
- `app_email` - Contact email address
- `app_phone` - Contact phone number
- `app_address` - Business address

### Regional Settings
- `timezone` - Application timezone (UTC, America/New_York, Asia/Karachi, etc.)
- `date_format` - Date display format (Y-m-d, m/d/Y, d/m/Y, etc.)
- `time_format` - Time display format (H:i for 24-hour, h:i A for 12-hour)

### Display Settings
- `items_per_page` - Number of items to show per page in lists

### System Preferences
- `enable_notifications` - Enable/disable in-app notifications (1 or 0)
- `enable_email_alerts` - Enable/disable email notifications (1 or 0)
- `maintenance_mode` - Enable/disable maintenance mode (1 or 0)

## Where Settings Are Applied

### Currently Implemented
1. **Dashboard Header** - Shows `app_name` from settings
2. **Page Titles** - Includes `app_name` in browser tab title

### How to Add More Settings Usage

#### Example: Use Date Format in Display
```php
// In any page that displays dates
$dateFormat = Settings::get('date_format', 'Y-m-d');
$formattedDate = date($dateFormat, strtotime($task['scheduled_date']));
echo $formattedDate;
```

#### Example: Use Timezone
```php
// Set timezone from settings
$timezone = Settings::get('timezone', 'UTC');
date_default_timezone_set($timezone);
```

#### Example: Use Items Per Page
```php
// In pagination
$itemsPerPage = Settings::getInt('items_per_page', 20);
$stmt = $pdo->prepare("SELECT * FROM tasks LIMIT :limit");
$stmt->execute(['limit' => $itemsPerPage]);
```

#### Example: Check Maintenance Mode
```php
// At the top of public pages
if (Settings::getBool('maintenance_mode', false) && $user['user_type'] !== 'admin') {
    echo "Site is under maintenance. Please check back later.";
    exit;
}
```

## Adding New Settings

### 1. Add to Database
```php
$pdo = Database::pdo();
$stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value, setting_type) VALUES (?, ?, ?)");
$stmt->execute(['new_setting_key', 'default_value', 'text']);
```

### 2. Add to Settings Page Form
Edit `public/admin/settings.php` and add a new form field in the appropriate section.

### 3. Add to Save Logic
Update the POST handler in `settings.php` to save the new setting.

## Performance Note
Settings are cached in memory after the first load, so there's no performance penalty for calling `Settings::get()` multiple times.

To reload settings (e.g., after updating them):
```php
Settings::reload();
```

## Files Involved
- `includes/settings_helper.php` - Settings class
- `public/admin/settings.php` - Settings management page
- `database/settings` table - Settings storage
- `includes/dashboard_layout.php` - Uses app_name setting
