# Program Templates Management System

This system allows administrators to manage program templates for progress reports through a web interface instead of hardcoding them in PHP files.

## Features

- **Database Storage**: Program templates are now stored in a SQL table instead of hardcoded arrays
- **Admin Panel**: Web-based interface for managing templates (add, edit, delete)
- **Admin Access Only**: Only users with admin privileges can access the management panel
- **Fallback Support**: If database fails, the system falls back to original hardcoded templates
- **Phase Support**: Supports both programs with phases (SBP, AM, DV, SPCP) and without phases (ACP, CRCP, etc.)

## Database Schema

### Table: `program_templates`

```sql
CREATE TABLE `program_templates` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `program_code` varchar(10) NOT NULL,
  `phase` int(11) DEFAULT NULL,
  `template_data` text NOT NULL,
  `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `program_phase_unique` (`program_code`, `phase`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```

**Fields:**
- `id`: Auto-incrementing primary key
- `program_code`: Program code (e.g., 'SBP', 'AM', 'DV')
- `phase`: Phase number (0, 1, 2, etc.) or NULL for programs without phases
- `template_data`: The actual template text content
- `created_date`: When the template was created
- `updated_date`: When the template was last modified

## Installation

1. **Run the SQL script** to create the table and populate with initial data:
   ```bash
   mysql -u username -p database_name < sql/program_templates.sql
   ```

2. **Access the admin panel** at:
   ```
   /interface/others/program_templates.php
   ```

3. **Menu access**: The "Program Templates" option is available in the "Others" section of the main menu for admin users.

## Usage

### For Administrators

1. Navigate to **Others > Program Templates** in the main menu
2. **Add new templates**:
   - Enter program code (e.g., 'SBP', 'AM')
   - Enter phase number (leave empty for programs without phases)
   - Enter template text
   - Click "Add Template"

3. **Edit existing templates**:
   - Click "Edit" button next to any template
   - Modify the fields as needed
   - Click "Update Template"

4. **Delete templates**:
   - Click "Delete" button next to any template
   - Confirm deletion

### For Developers

The system automatically uses database templates when generating reports. The `generate_report.php` file now includes:

```php
// Function to get program templates from database
function get_program_templates_from_db() {
    // ... database query logic with fallback
}

// Get program templates from database
$program_templates = get_program_templates_from_db();
```

## Security

- **Access Control**: Only users with `admin` and `super` privileges can access the management panel
- **Input Validation**: All form inputs are validated and sanitized
- **SQL Injection Protection**: Uses prepared statements for all database queries

## Supported Programs

### Programs with Phases
- **SBP** (Sexual Behavior Program): 3 phases (0, 1, 2)
- **AM** (Anger Management): 2 phases (0, 1)
- **DV** (Domestic Violence): 2 phases (0, 1)
- **SPCP** (Supportive Parenting and Childcare Program): 2 phases (0, 1)

### Programs without Phases
- **ACP** (Addiction Counseling Program)
- **CRCP** (Conflict Resolution Counseling Program)
- **GCP** (Gambling Counseling Program)
- **GMH** (General Mental Health Counseling Program)
- **DDCP** (Drinking Driving Counseling Program)
- **AC** (Addictions Counseling Program)

## Troubleshooting

### Database Connection Issues
If the database is unavailable, the system will automatically fall back to the original hardcoded templates and log an error.

### Permission Issues
Ensure the user has the required ACL permissions:
- `admin` and `super` privileges for accessing the management panel

### Template Not Found
If a program/phase combination is not found in the database, the system will return an empty string, which may result in incomplete reports.

## File Structure

```
├── sql/
│   ├── program_templates.sql          # Table creation and initial data
│   └── run_program_templates.sql      # Installation script
├── interface/
│   ├── others/
│   │   └── program_templates.php      # Admin management panel
│   └── patient_file/report/
│       └── generate_report.php        # Updated to use database
└── interface/main/tabs/menu/menus/
    └── standard.json                  # Updated with menu option
```

## Migration from Hardcoded Templates

The system automatically migrates from hardcoded templates to database storage. The original hardcoded templates are preserved as fallback data in case of database issues.

## Future Enhancements

- Template versioning
- Template categories/tags
- Bulk import/export functionality
- Template preview functionality
- Audit logging for template changes 