# Plan: High Settings & Template Settings Admin Screens

**Status:** Implemented — 2026-06-17

---

## Context

Two features were fully specified in `docs/ai/admin/` but had zero implementation.
This plan covers building both from scratch.

---

## Feature 1 — High Settings (DevOps Panel)

A complete server/codebase management panel at `/admin/high-settings`.
Lets admins control the server directly from the UI without SSH access.

### Files Created / Modified

| Action | File |
|--------|------|
| **Created** | `app/Http/Controllers/Admin/System/HighSettingsController.php` |
| **Created** | `resources/views/dashboard/admin/system/high_settings/index.blade.php` |
| **Modified** | `routes/admin.php` — added `high-settings` route group |

### Routes (`routes/admin.php`)

Group prefix: `high-settings`, name prefix: `admin.high-settings.`

**GET (read-only info):**
| Name | Action |
|------|--------|
| `server-info` | `serverInfo()` |
| `git-status` | `gitStatus()` |
| `git-log` | `gitLog()` |
| `git-diff` | `gitDiff()` |
| `view-logs` | `viewLogs()` |
| `git-conflicts` | `gitConflicts()` |

**POST (actions):**
| Name | Action |
|------|--------|
| `clear-cache` | `clearCache()` |
| `run-migrate` | `runMigrate()` |
| `optimize` | `optimizeApp()` |
| `storage-link` | `storageLink()` |
| `set-git-config` | `setGitConfig()` |
| `git-init` | `gitInit()` |
| `git-remote` | `setGitRemote()` |
| `set-git-token` | `setGitToken()` — saves `GIT_TOKEN` to `.env` |
| `git-stash` | `gitStash()` |
| `git-stash-pop` | `gitStashPop()` |
| `git-pull` | `gitPull()` — uses `GIT_TOKEN` for HTTPS auth |
| `git-commit` | `gitCommit()` |
| `git-push` | `gitPush()` — uses `GIT_TOKEN` for HTTPS auth |
| `git-resolve-conflict` | `gitResolveConflict()` — ours/theirs/mark |
| `git-abort-merge` | `gitAbortMerge()` |
| `set-environment` | `setEnvironment()` |
| `set-debug-mode` | `setDebugMode()` |
| `generate-thumbnails` | `generateThumbnails()` |

### Controller Design

Private helpers:
- `runProcess(array $cmd)` — Symfony Process, 120 s timeout, returns `{success, output}`
- `updateEnvValue(string $key, string $value)` — regex-replace in `.env`
- `formatBytes(int $bytes)` — human-readable KB/MB/GB

### View & JS Module

- 11 rows of Bootstrap cards, full-width layout
- **Auto-loads on page open:** Server Info card, Git Status card
- **SweetAlert2 confirmations:** Clear Cache, Run Migrations, Git Push, Git Stash, Git Stash Pop, Abort Merge
- **Copy-to-clipboard** on all output `<pre>` blocks
- **FormValidation** on Git Config form (name required, email format)
- **Commit modal** (Bootstrap modal) for commit message entry
- **Conflict resolver** inline: per-file Use Ours / Use Theirs / Mark Resolved buttons
- JS architecture: `KTHighSettings` IIFE module with `KTUtil.onDOMContentLoaded` init

---

## Feature 2 — Template Settings (Admin UI Customization)

Customise the Keen 3.5 theme from the admin UI.
Settings are stored in DB, cached, and applied globally on every admin page.

### Files Created / Modified

| Action | File |
|--------|------|
| **Created** | `database/migrations/2026_05_16_000001_create_template_settings_table.php` |
| **Created** | `app/Models/TemplateSettings.php` |
| **Created** | `app/Http/Controllers/Admin/System/TemplateSettingsController.php` |
| **Created** | `resources/views/dashboard/admin/system/template_settings/index.blade.php` |
| **Modified** | `routes/admin.php` — added `template-settings` route group |
| **Modified** | `app/Providers/AppServiceProvider.php` — View::composer |
| **Modified** | `resources/views/dashboard/admin/layouts/master.blade.php` — dynamic body attrs |
| **Modified** | `resources/views/dashboard/admin/layouts/head.blade.php` — CSS vars + font |

### Database Table: `template_settings` (single row)

| Column | Type | Default |
|--------|------|---------|
| `app_layout` | string | `dark-sidebar` |
| `theme_mode` | string | `light` |
| `rtl_mode` | boolean | `false` |
| `width_mode` | string | `default` |
| `icon_style` | string | `duotone` |
| `page_loader` | string | `none` |
| `sidebar_fixed` | boolean | `true` |
| `sidebar_minimize_mode` | string | `hoverable` |
| `sidebar_bg_color` | string | `#1e1e2d` |
| `sidebar_active_color` | string | `#3e97ff` |
| `header_content_type` | string | `menu` |
| `header_title_layout` | string | `column` |
| `toolbar_enabled` | boolean | `true` |
| `toolbar_fixed_desktop` | boolean | `false` |
| `toolbar_fixed_mobile` | boolean | `false` |
| `color_primary/success/info/warning/danger/dark` | string | Keen defaults |
| `font_family` | string | `Inter` |
| `custom_css` | text | null |
| `login_image` / `register_image` / `default_image` | string | null |

### Model

- `TemplateSettings::getCached()` — `Cache::rememberForever('template_settings', ...)` 
- `TemplateSettings::clearCache()` — `Cache::forget('template_settings')` — called on save & image delete
- Uses `$casts` for 5 boolean columns

### Controller

- `index()` — `firstOrCreate([])` → view
- `update(Request $request)` — validates, handles 3 image uploads via `ImageProcessing` trait alias `removeImageFile`, saves, clears cache
- `removeImage(string $field)` — deletes one image field, clears cache

Trait alias to avoid name collision: `use ImageProcessing { deleteImage as protected removeImageFile; }`

### View: 7-Tab Form

1. **Layout** — app_layout, theme_mode, width_mode, icon_style, page_loader, rtl_mode
2. **Sidebar** — minimize_mode, sidebar_fixed, sidebar_bg_color, sidebar_active_color
3. **Header** — header_content_type, header_title_layout
4. **Toolbar** — toolbar_enabled, toolbar_fixed_desktop, toolbar_fixed_mobile
5. **Colors** — 6 color pickers with Reset buttons
6. **Images** — login/register/default with current image preview + delete button
7. **Font & CSS** — font family selector with live preview + custom CSS textarea

### Global Application

**`AppServiceProvider::boot()`:**
```php
View::composer('dashboard.admin.layouts.*', function ($view) {
    $view->with('templateSettings', TemplateSettings::getCached());
});
```

**`master.blade.php` body tag:** reads `$ts->app_layout`, `$ts->sidebar_fixed`, `$ts->sidebar_minimize_mode`, `$ts->toolbar_enabled`, `$ts->theme_mode`, `$ts->rtl_mode` → sets `data-kt-*` attributes dynamically.

**`head.blade.php`:**
- Dynamic Google Fonts link for `$ts->font_family`
- `<style>` block with `:root` CSS variable overrides for all 6 colors
- Sidebar color overrides if non-default
- Custom CSS injection (via `{!! $ts->custom_css !!}`)

---

## Verification Steps

### High Settings
1. Visit `/admin/high-settings` → Server Info and Git Status auto-populate
2. Click "Clear All Cache" → SweetAlert confirm appears, then caches are cleared
3. Click "Run Migrations" → SweetAlert confirm appears, then migrations run
4. Click "Push to GitHub" → SweetAlert confirm
5. Click "Load History" → table of 15 commits
6. Click "Show Diff" → `git diff --stat` in scrollable `<pre>` with Copy button
7. Click "Load Recent Logs" → last 100 lines of `laravel.log`
8. Git Config form → FormValidation rejects invalid email before submit
9. Save GitHub Token → `GIT_TOKEN` written to `.env`

### Template Settings
1. `php artisan migrate` → `template_settings` table created
2. Visit `/admin/template-settings` → 7-tab form with defaults
3. Change layout + save → body `data-kt-app-layout` changes on next load
4. Change colors + save → CSS variables applied across admin
5. Upload login image → image preview appears, DELETE button removes it
6. Change font → font changes in preview and across admin panel
7. Enter Custom CSS → injected in `<head>` on all admin pages
