# Architecture and Database

Status: **implemented** (Phases 1 to 7). This document describes the code as built.

## 1. Environment

| Item | Version | Note |
|---|---|---|
| PHP | 8.2.12 (XAMPP) | Laravel 13 needs PHP 8.3+, so this project uses Laravel 12 |
| Laravel | 12.74 | |
| Database | MariaDB 10.4 locally (MySQL 8 / MariaDB 10.4+ in production) | tests use SQLite in memory |
| Node / npm | 22 / 10 | asset build only |
| Frontend | Blade, Tailwind CSS 4 (Vite), vanilla JS, Swiper 14 | |
| Tests | PHPUnit (Laravel), Playwright 1.64 for browser checks | |

## 2. Request flow

```
Visitor -> routes/web.php -> Site\PageController -> PageRenderer
        -> sections (published content) + MediaLibrary preload -> site/page.blade.php
        -> one Blade view per section type (resources/views/sections/**)

Enquiry form -> POST /enquiries (CSRF, throttle, honeypot, timing token)
        -> StoreEnquiryRequest -> EnquiryService (save in transaction)
        -> SendEnquiryNotifications job (database queue) -> 2 emails
```

## 3. Folder map

```
app/Sections/                 Fixed CMS section types
  Field.php                   field definition, validation rules, sanitising
  SectionType.php             base class
  SectionRegistry.php         page list + section types per page (Figma order)
  Types/*.php                 one class per Figma section
app/Services/                 PageRenderer, PublishService, MediaService, EnquiryService, ActivityLogger
app/Support/                  Content (safe text helpers), Icons (SVG allowlist), MediaLibrary
app/Http/Controllers/Site/    PageController, EnquiryController, SitemapController
app/Http/Controllers/Admin/   Dashboard, Page, Section, Media, Enquiry, Setting, User, ActivityLog, Profile, Auth/*
app/Http/Middleware/          SecurityHeaders, EnsureActiveAdmin, AdminIdleTimeout, AdminNoIndex
resources/views/sections/     public section templates
resources/views/admin/        CMS screens
database/seeders/content/     original Figma text and settings
database/seeders/media/       optimised Figma images (WebP) imported by the seeder
public/images/icons/          SVG logos and icons exported from Figma
```

## 4. CMS model

- **Pages** are fixed: home, about, history, campuses, contact. Admins cannot add or delete pages.
- **Sections** are fixed per page (`SectionRegistry`). Each type defines its fields in PHP. The admin form is generated from those fields, so admins change values, never structure.
- **Allowed field types:** text, textarea, lines (one item per line), url, image, video, select, icon (from an allowlist) and repeater (with min and max item counts).
- **Two-part headings:** every heading is stored as `title_regular` + `title_accent` + `title_break`. The template applies the Figma styling.
- **Sanitising:** unknown keys are dropped, strings are trimmed and control characters removed, media ids are cast to integers. All output is escaped with `{{ }}`. No CMS field is rendered as raw HTML.
- **Links:** only `/path`, `#anchor`, `http(s)`, `mailto:` and `tel:` are accepted (`App\Rules\SafeUrl`). A button whose link is empty is not rendered, so there are no dead buttons.

### Draft and publish
| Column on `page_sections` | Draft | Live |
|---|---|---|
| content | `draft_content` | `published_content` |
| visibility | `is_enabled` | `published_is_enabled` |
| order | `sort_order` | `published_sort_order` |

- Edits, hide/show and reordering change the draft columns and set `pages.has_unpublished_changes`.
- **Publish** copies draft to live for the whole page in one transaction.
- **Discard draft** copies live back to draft.
- **Preview** (`/admin/pages/{id}/preview`) renders the draft. It is `noindex` and needs a login.
- Only the latest version is stored. There is no revision history.
- The home page cannot be unpublished. Other pages can be hidden (404 for visitors).

## 5. Database schema

| Table | Purpose | Key columns and constraints |
|---|---|---|
| `users` | CMS accounts | `email` unique; `role` enum(`super_admin`,`admin`) indexed; `is_active`; `last_login_at`. Role and status are not mass assignable. |
| `pages` | 5 fixed pages | `key` unique, `slug` unique, `status` enum indexed, SEO fields, `og_image_id` FK media (null on delete), `updated_by` FK users (null on delete), `has_unpublished_changes` |
| `page_sections` | sections of a page | FK `page_id` (cascade); unique (`page_id`,`type`); index (`page_id`,`sort_order`); JSON `draft_content` and `published_content` |
| `media` | uploaded files | `path` (random UUID name), `variants` JSON (600 and 1200 px WebP), `mime_type` indexed, size, dimensions, `alt_text`, `uploaded_by` FK users (null on delete) |
| `enquiries` | admission enquiries | parent and student names, email (indexed), phone, grade, campus, message, `consent_at`, `source_page`, `status` enum indexed, notes, `follow_up_date` indexed, mail sent timestamps, `ip_address`, `archived_at` indexed |
| `settings` | site-wide values | `key` unique, JSON `value`; cached and cleared on save |
| `activity_logs` | security and content events | FK `user_id` (null on delete), `action` indexed, morph subject, JSON `properties`, `ip_address`, `created_at` indexed |
| Laravel defaults | | `sessions`, `password_reset_tokens`, `cache`, `jobs`, `failed_jobs` |

Relationships: `Page hasMany PageSection`, `Page belongsTo Media (og image)`, `Media belongsTo User`, `ActivityLog belongsTo User`, `ActivityLog morphTo subject`. Section content references media by id inside JSON. `MediaController` checks those references before deleting a file.

## 6. Routes

| Area | Routes |
|---|---|
| Public | `/`, `/about-us`, `/history-and-journey`, `/campuses`, `/contact-us`, `POST /enquiries`, `/sitemap.xml`, `/robots.txt`, `/up` |
| Admin guest | `/admin/login`, `/admin/forgot-password`, `/admin/reset-password/{token}` |
| Admin (any active user) | dashboard, pages, sections, preview, publish, media, enquiries (+ CSV export), profile |
| Super Admin only | users, settings, activity log, permanent enquiry delete |

## 7. Frontend

- Tailwind 4 tokens in `resources/css/app.css` (`@theme`) match the Figma colours, fonts and sizes. Headings scale fluidly from the 1920 px design down to mobile.
- Fonts are self-hosted: General Sans and Boska (Fontshare), Outfit and Inter (Fontsource).
- JS (`resources/js/site/*`): mobile menu, accessible tabs, hero and timeline sliders (Swiper, loaded only where needed), video dialog, scroll reveal (off with reduced motion), form validation.
- Images: WebP with `srcset` (600, 1200 and original), `width`/`height` set, lazy loading below the fold, `fetchpriority=high` for the hero.
- Built asset URLs are relative, so the site works from a sub-folder (XAMPP) and from a domain root.

## 8. Deferred features

| Feature | Reason |
|---|---|
| Scheduled publishing | needs a server cron; hosting not chosen yet |
| Authenticator-app 2FA | optional in the spec; can be added later without schema changes to existing tables |
| Menu management | the menu has four fixed links that match the Figma pages |
| Rich text editor | Figma has only plain paragraphs; plain text removes XSS risk |
| News, events, gallery, faculty, testimonials, FAQ, contact-only form | not in Figma |
